CH 5 - Lists
CH 5 - Lists
Lists
Introduction
● Add( 1 ,X,L)
● Set(2,Z,L) -
● Remove(Z,L)
● Get(2,L) = C
● IndexOf(X,L) = 1
Example of operation
List Implementation
● In dynamic list, the size of the list is not fixed and can be modified during the
operations performed on it.
● Dynamic data structures are designate to facilitate the change of data
structures in the run time.
● Example of dynamic list: Linked list.
Array Implementation of list
Limitations
● A linked list is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers.
● Each node contains two parts:
○ The first part contains the information of the element, and
○ The second part contains the address of the next node in the list.
Advantages / disadvantages of Linked List
Advantages
● Dynamic Size
● Ease of insertion/deletion.
Disadvantages
● Linked list can be visualized as a chain of nodes, where every node in the list
points to the next node.
● The first node is called head. If the linked list is empty, then the value of head
is NULL.
● To represent a linked list, we require a link element called first.
● Each node includes a data field and a link field called next.
● Each node is linked with its next node using the next field.
● The last node has NULL in its next field to mark the end of the list.
Linked list as an ADT
A linked list L is a collection of nodes which has a link to the next node in the list
and having the following operations:
SEARCH - search for an item in linked list ADD - add element into linked list (
start, end or specific position)
COUNT - count the number of items in linked list
DELETE - remove item from linked
list (start, end or specific position)
P = HEAD
while(p!=NULL)
P = p->next;
Counting the number of nodes
● Traverse the linked list from start to last node and print each node.
}
Search element
Step1: Start
Step2: Assume that the two linked list has two different heads as- HEAD1
& HEAD2
Step 3: If the first linked list is empty, return HEAD2,
Step 4: If the second linked list is empty, return HEAD1
Step 5: Store the address of HEAD1 in pointer variable say P.
Step 6: Start traversing first linked list using HEAD1 and go to the last node
whose next field is NULL and replace that NULL with HEAD2.
Step 7: Return HEAD1.
Step 8: Stop
Types of Linked List
● We can create a structure for the representation of singly linked list where
each node has two members - the data item and the address of next node.
Operation of Singly linked list
P = (Node*)malloc(sizeof(Node));
P->data = 501;
p->next = NULL;
Operation of Singly linked list[2]
Inserting nodes
● To insert an element or a node in a linked list, the following steps are done:
○ Allocate a node
○ Assign a data into the data field of the node
○ Adjust the pointer
● Insertion can be done in 3 ways:
○ Inserting at beginning of the list
○ Inserting at the end of the list
○ Inserting at specific position in the list.
Operation of Singly linked list[2]
● To insert an element or a node at the beginning of the linked list, the following
steps are done:
○ Create a new node (newNode) with given value
○ Check whether the list is empty (HEAD == NULL)
○ If it is empty then, set newNode->next = NULL and HEAD = newNode.
○ If it is not empty then, set newNode->next = HEAD and head = newNode.
Operation of Singly linked list[3]
● To insert an element or a node at the end of the linked list, the following steps
are done:
○ Create a new node (newNode) with given value and newNode->next = NULL
○ Check whether the list is empty (HEAD == NULL)
○ If it is empty then, set newNode->next = NULL and HEAD = newNode.
○ If it is not empty then, go to the last node of the linked list and set p->next= newNode.
Operation of Singly linked list[4]
Deleting a node
● To delete a node from the linked list, the following steps are done:
○ Find the previous node of the node to be deleted
○ Change the next of previous node
○ Free the memory for the node to be deleted.
● Deletion can be performed in 3 ways:
○ Deleting from beginning of the list
○ Deleting from the end of the list
○ Deleting a specific node
Operation of Singly linked list[6]
● To delete a node from the begining of the linked list, the following steps are
done:
○ Check whether the linked list is empty
○ If it is empty then, display ‘List is empty, delete is not possible’ and exit
○ If it is not empty then define a node pointer ‘p’ and initialize it with HEAD.
○ Check whether the list is having only one node (p->next == NULL)
○ If so, then set HEAD = NULL and delete p ( free )
○ If not, then set HEAD = p->next and delete p.
Operation of Singly linked list[7]
● To delete a node from the begining of the linked list, the following steps are
done:
Operation of Singly linked list[8]
● To delete a node from the begining of the linked list, the following
steps are done:
○ Check whether the linked list is empty
○ If it is empty then, display ‘List is empty, delete is not possible’ and exit
○ If it is not empty then define two node pointers ‘p’ and ‘q’ and and initialize p
with HEAD.
○ Check whether the list is having only one node (p->next == NULL)
○ If so, then set HEAD = NULL and delete p ( free )
○ If not, then set q=p and move p to its next node. Repeat the same until it
reaches to the last node in the list ie, p->next == NULL.
○ Finally, set q->next = NULL and delete p.
Operation of Singly linked list[8]
● To delete a node from the begining of the linked list, the following
steps are done:
Operation of Singly linked list[8]
Deleting a specific node of the linked list:-
● To delete a specific node of the linked list, the following steps are done:
○ Check whether the linked list is empty
○ If it is empty then, display ‘List is empty, delete is not possible’ and exit
○ If it is not empty then define two node pointers ‘p’ and ‘q’ and and initialize p with
HEAD.
○ Keep moving the p until it reaches the exact node to be deleted or to the last node.
At every loop, set q=p before moving p to its next node.
○ If it reached to the exact node, which we want to delete, then check whether list is
having only one node or not
○ If the list has only one node and that is the node to be deleted, then set HEAD =
NULL and delete p.
○ If the list contains multiple nodes, then check whether p is the first node in the list,
if so then move the head to the next node
○ If p is not the first node then check whether it is last node in the list
○ If p is not first and not last node then set q->next = p->next and delete p.
Operation of Singly linked list[8]
● To delete a specific node of the linked list, the following steps are
done:
Doubly linked list
● Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence.
● Therefore, in a doubly linked list, a node consists of three parts:
○ node data
○ pointer to the next node in sequence (next pointer)
○ pointer to the previous node (previous pointer).
● Pointers exist between adjacent nodes in both directions.The list can be
traversed either forward or backward.
● Usually two pointers are maintained to keep track of the list, head and tail.
Doubly linked list - Representation
Advantages of DLL over SLL
● Advantages
○ Entire list can be traversed from any node
○ Helps to access in a circle or loop
○ Despite using single linked list, we can traverse to its previous node.
● Disadvantages
○ Complex compared to linear LL
○ Reversing operations is also complex
○ If not traversed carefully, then we could end in an infinite loop.
○ Only support sequential access of elements.
Operations on Circular Linked List
● Push(value, stack)
○ Create a newNode with given value
○ Check whether the stack is EMPTY (top == NULL)
○ If it is EMPTY, then set newNode->next = NULL
○ If it is not EMPTY, then set newNode->next = top
○ Finally, set top = newNode
Stack as Linked List[3]
● Pop(stack)
○ Check whether stack is EMPTY (top ==NULL)
○ If it is EMPTY, then display “Stack is Empty, cant pop” and exit.
○ If it is Not EMPTY, then define a node pointer “p” and set it to “top”
○ Then set top = top->next.
○ Delete the node “p”.
Stack as Linked List[4]
● Display(stack)
○ Check whether stack is EMPTY (top ==NULL)
○ If it is EMPTY, then display “Stack is Empty” and exit.
○ If it is Not EMPTY, then define a node pointer “p” and set it to “top”
○ Display p->data and move to next node
○ Repeat the same until p reaches to the first node in the stack ie: p->next != NULL
Queue as Linked List
● Operation Enqueue(x,q)
○ Create a newNode with given value and set newNode->next to NULL
○ Check whether queue is EMPTY (rear == NULL)
○ If it is EMPTY then, set front = newNode & rear = newNode
○ If it is not EMPTY then, set rear->next = newNode and rear = newNode.
● Dequeue()
○ Check whether the queue is EMPTY (front == NULL)
○ If it is EMPTY, then display “Queue is empty, dequeue is not possible” and exit.
○ If it is not EMPTY then, define a node pointer “p” and set it to “front” ie p=front.
○ Set front = front->next and delete “p”
Queue as Linked List [3]
● Operation Display(q)
○ Check whether the queue is EMPTY (front == NULL)
○ If it is EMPTY, then display “Queue is empty, no elements to display” and exit.
○ If it is not EMPTY then, define a node pointer “p” and set it to “front” ie p=front.
○ Display p->data and move it to the next node ie p=p->next.
○ Repeat the process until p reaches to rear ie (p->next != NULL).