CH 4 Static and Dynamic List
CH 4 Static and Dynamic List
Overview
4. Static and Dynamic List (8hrs)
1.Definition and Array implementation of lists
2.Queues as a list
3.Link List Definition and link list as an ADT
4.Dynamic implementation
5.Basic operations in linked list
6.Doubly linked lists and its advantages
7.Implementation of Doubly Linked List
8.Linked Implementation of stacks and Queues
List
• A list is a finite, ordered sequence of data items known as elements.
• “Ordered” means each element has a position in the list
• “Ordered” does not mean the elements are sorted
• A list is a linear data structure in which
• elements can be inserted into any position in the list
• removed from any position in the list.
• Any element can be accessed from any position in the list.
• Examples:
• A Shopping list • A list of payroll records
• A list of integers • A list of students
• A list of fruits • A list of lists
List
• A list differs from the stack and queue data structure:
• In Array implementations:
• all elements of the list are usually assumed to have the same data
type
• In other implementation:
List
head tail
11 22 33 44
0 1 2 3 4
List: Insertion
• Insert an ITEM at the beginning: ITEM = 55
• Algorithm: head tail
FUNCTION INSERT-BEGINNING(item, head, tail)
IF tail == MaxSize - 1, then
PRINT “list is full” and
11 22 33 44
EXIT
i = tail 0 1 2 3 4
WHILE (i >= 0)
list[i + 1] = list[i]
head tail
i = i - 1
END WHILE
LIST[head] = ITEM
tail = tail + 1 55 11 22 33 44
END FUNCTION 0 1 2 3 4
List: Insertion
• Insert an ITEM at the end: ITEM = 55
• Algorithm: head tail
0 1 2 3 4
List: Insertion
• Insert an ITEM at the given position: ITEM = 55. Position = 2
• Algorithm: head tail
FUNCTION INSERT-at-POSITION(item, Position)
IF tail == MaxSize - 1, then
PRINT “list is full” and 11 22 33 44
EXIT
i = tail 0 1 2 3 4
WHILE (i >= Position)
list[i + 1] = list[i]
head
i = i - 1
END WHILE tail
LIST[Position] = ITEM
tail = tail + 1 11 22 33 33 44
END FUNCTION
0 1 2 3 4
List: Deletion
• Delete an ITEM from the beginning:
• Algorithm:
head tail
RETURN-TYPE FUNCTION DELETE-BEGINNING()
IF head == - 1, then
PRINT “list is empty” and 11 22 33 44
EXIT
item = list[head] 0 1 2 3 4
i = head
WHILE (i <= tail)
list[i] = list[i + 1] head tail
i = i + 1 11
END WHILE
tail = tail - 1
22 33 44
RETURN item
END FUNCTION
0 1 2 3 4
List: Deletion
• Delete an ITEM from the end:
• Algorithm: head tail
0 1 2 3 4
List: Deletion
• Delete an ITEM from the given position: Position = 2
• Algorithm:
head tail
RETURN-TYPE FUNCTION DELETE-at-POSITION(Position)
IF head == - 1 or tail = -1, then
PRINT “list is empty” and 11 22 33 44
EXIT
item = list[Position] 0 1 2 3 4
i = Position
WHILE (i <= tail)
list[i] = list[I + 1] head tail
i = i + 1 33
END WHILE
tail = tail - 1
11 22 44
RETURN item
END FUNCTION
0 1 2 3 4
List: Classwork
Write algorithms:
• To insert an item in sorted list
• To delete a given ITEM from the list
head tail
Item = 66
11 22 33 44
0 1 2 3 4
List: Classwork
Write algorithms:
• To concatenate two lists into a single third list
head tail
33 66 head tail
0 1 Concatenate
33 66 55 44 11
head tail
0 1 2 3 4
55 44 11
0 1 2
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 18
List: Classwork
Write algorithms
• To merge two lists into a single third list
head tail
33 66 head tail
0 1 Merge
11 33 44 55 66
head tail
0 1 2 3 4
11 44 55
0 1 2
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 19
Array and List
• In a list, the missing spot is filled in when something is deleted.
• In an array, an empty variable is left behind when something is
deleted.
Linked List
• Linked list is the dynamic implementation of list
• Linked List addresses some of the limitations of Arrays
• It should be noted:
• For some applications, Arrays are the best data type to use.
• The decision to use an array or linked list depends on the nature
of application
head A B C null
Linked List
• A linked list is a collection of objects linked together by references
from an object to another object.
• By convention these objects are named as nodes.
• So the basic linked list (commonly called singly linked list) consists of
nodes where each node contains
• one or more data fields and
• a reference to the next node
• The last node contains a null reference to indicate the end of the
list.
Fig. a singly linked list
head A B C null
Linked List
• Unlike arrays where memory is allocated as a contiguous block of
memory, the memory required for each node can be allocated as
needed thereby providing a better way to manage free memory.
• The entry point into a linked list is always the head of the list.
• The head is NOT a separate node, but a reference to the first Node
in the list.
head A B C null
Linked List
• If the list is empty then the head has the value null.
• Unlike Arrays, nodes cannot be accessed by an index.
• One must begin from the head and traverse the list to access the
Nodes in the list.
• Insertions of new Nodes and deletion of existing nodes are fairly easy
to handle (once you find the node)
head A B C null
Linked List
• The name of a node is itself a reference (pointer) to itself.
• There can be one or more data fields in a node
• The data field contains the actual data needed to be stored
• The link field points to the next node (it actually contains the
address of the next node to which it points to)
node2 Pointer to
Data field Link field next node
node2
Linked List
• Coding a node:
{
node2
int info;
node *link; node2
};
• Dynamic implementation
• To better understand linked list and its dynamic nature, let's use the analogy
of something we are all familiar with; contact list on our phone. Imagine will
have five names in our phone contact which are implemented using a static
data structure such as arrays like [Abby, Demi, Gina, Rita, Ursula]. In a
structure such this, if another name 'Bobby' is to be inserted between Abby
and Demi, it means the size of the structure has to be increased by one, and
the data items from Demi has to be shifted one position to the right. While
that may not seem like much work given the number of contacts in this case,
imagine a case of 10,000 or 1,000,000 contacts, shifting and repositioning
that much data would amount to enormous strain on the computer processing
resources. That is where dynamic data structures such as linked list come in.
Rather than representing data contiguously next to each other in the
computer memory, linked list employs the concept of nodes and pointers. A
node holds the data item in the particular memory block and a pointer links
from one node to another and references the memory address of the next and
previous nodes (in the case of doubly linked list). So, we can represent our
contacts list using a linked list as follows:
• —>[Abby]-->[Demi]-->[Gina]-->[Rita]-->[Ursula]-->NULL
• [Abby] is known as the head (first) node and the arrows represent the
pointers. [Ursula] is known as the tail (last) node and it references to
NULL to indicate the end of the list. Notice the first arrow has no
previous node, it is known as the local pointer variable without which
we can not traverse the linked list.
• To add [Bobby] in between [Abby] and [Demi], all we need to do is to set
[Bobby]'s next pointer to reference [Demi] and [Abby]'s next pointer to
reference [Bobby]. As you can see, there is no need to re-adjust the
positions of the other elements in the structure. In the same way,
deleting a node such as [Gina] would require that we disconnect its next
pointer from [Rita] while the next pointer of [Demi] is then set to
reference [Rita]. Regardless of whether the number of nodes is just a
few or in the millions, the insertion and removal operations is the same
and proves to be more effective than in other data structure types.
1. One or more data fields which stores the data in the list
• The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage.
• Inserting a new element in an array of elements is expensive because the room has to
be created for the new elements and to create room existing elements have to be
shifted. Deletion is also expensive with arrays due to same reason.
• Pointers in linked list overcome the above limitations at the cost of extra space for pointers.
• The list is not required to be contiguously present in the memory. The node can reside any
where in the memory and linked together to make a list. This achieves optimized utilization
of space.
• list size is limited to the memory size and doesn't need to be declared in advance.
• Insert an element
• Delete an element
• Traversing an element
• Searching an element
• Multi-linked list
head 11 22 33 null
Algorithm:
1.Create a newNode
2.Assign value to newNode
3.Check whether list is Empty (head == NULL)
4.If it is Empty then,
set newNode→next = NULL and
head = newNode.
5.If it is Not Empty then,
set newNode→next = head and
head = newNode.
head
he
=
d=
e
od
hea
wN
ne
44 node1
Algorithm:
1.Create a newNode
2.Assign value to newNode
3.Set newNode → next as NULL.
4.Check whether list is Empty (head == NULL).
5.If it is Empty then, set head = newNode.
6.If it is Not Empty then,
a.de ne a node pointer temp and ini alize
with head.
7.Keep moving the temp to its next node un l it
reaches to the last node in the list (un l temp →
next is equal to NULL).
8.Set temp → next = newNode.
ti
ti
ti
Inserting a node at the end
head = node1
head
Singly Linked List
head
11 node2 22 node3 33 newNode
44 null
newNode
Displaying the contents of a linked list is very simple. We keep moving the temp
Singly Linked List
Algorithm:
//C implementation
1. Check whether list is Empty (head == NULL) struct node *temp = head;
2. If it is Empty then, display 'List is Empty.' and printf("\n\nList elements are - \n");
terminate the function.
while(temp != NULL) {
3. If it is Not Empty then, define a Node
printf("%d --->",temp->data);
pointer 'temp' and initialize with head.
temp = temp->next;
4. Keep displaying temp → data with an arrow (--->)
until temp reaches to the last node }
• A doubly linked list is a list that has two references from each node,
• one to the next node and
• another to the previous node.
• Doubly linked lists also starts from head node, but provide access both ways.
• The flexibility of a doubly linked list is that one can traverse forward or
backward from any node.
• A doubly linked list may be suitable for applications that require frequent
updates to nodes that are in a close range.
TAIL
• A doubly linked list allows convenient access from a list node to the next node
and also to the preceding node on the list.
• The doubly linked list node accomplishes this in the obvious way by storing two
pointers:
• one to the node following it (as in the singly linked list), and
• A node is represented as
class node
{ node node1;
int data;
node *next;
node *prev;
}
node1
1. Insertion
Doubly Linked List
Node *newNode;
1.Create a newNode newNode = new Node();
2.Assign value to newNode newNode->data = 33;
3.Assign newNode → previous = NULL newNode->prev = null;
if(head == null){
4.Check whether list is Empty (head == NULL)
newNode->next = null;
5.If it is Empty then,
head = newNode;
set newNode→next = NULL and }
head = newNode. else {
6.If it is Not Empty then, newNode->next = head;
set newNode→next = head and head = newNode;
head = newNode. }
head
fi
ti
ti
ti
Traversing
• In a doubly linked list, we have two types of traversals as we have two pointers with
different directions in the doubly linked list.
1. Forward traversal –
a. Traversal is done using the next pointer which is in the forward direction.
b. Start with the front node and visit all the nodes until the node becomes
NULL.
2. Backward traversal –
b. Start with the end node and visit all the nodes until the node becomes
NULL.
Assignment
Assignment
• Dynamic size: the size in linked list can grow or shrink in run time
• Fast and efficient insertion and deletion (since it does not require
shifting operations)
1. Check whether stack is Empty (top ==
NULL).
2. If it is Empty, then display "Stack is
Empty" and terminate the function
3. If it is Not Empty, then define
a Node pointer 'temp' and set it to 'top'.
4. Then set 'top = top → next'.
5. Finally, delete 'temp'.
89