0% found this document useful (0 votes)
26 views70 pages

CH 5 - Lists

List is a sequential data structure in which addition and removal of data items can be done from any position.

Uploaded by

dital38028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views70 pages

CH 5 - Lists

List is a sequential data structure in which addition and removal of data items can be done from any position.

Uploaded by

dital38028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

CH 5

Lists
Introduction

● List is a sequential data structure in which addition and removal of


data items can be done from any position.
● List is a collection of nodes.
● The List is an :
○ Ordered sequence of data items called elements
○ A1, A2, A3, ...,AN is a list of size N
○ Size of an empty list is 0
○ Ai+i succeeds Ai
○ Ai-1 precedes Ai
○ Position of Ai, is i
○ First element is A1 called "head"
○ Last element is AN called "tail"
List as an ADT

A List L is defined as the collection of items of type T with the


following operations:
● Initialize: Creates a new empty List
● Add : adds a new node to the list
● Set : update the contents of a node
● Remove : removes a node
● IsEmpty : reports whether the list is empty
● IsFull : reports whether the list is full
● Destroy : deletes the contents of the list
List operations

● Initialize(L) - Create a new empty List named L


● Add( 1 ,X,L) - Adds the value X to list L at position 1 (the start of the
list is position 0), shifting subsequent elements up
● Set(2,Z,L) - Updates the values at position 2 to be Z
● Remove(Z,L) - Removes the element with value Z
● Get(2,L) - returns the value of the third node
● IndexOf(X,L) - returns the index of the node with value X
List operations[2]

● Add( 1 ,X,L)

● Set(2,Z,L) -

● Remove(Z,L)
● Get(2,L) = C
● IndexOf(X,L) = 1
Example of operation
List Implementation

● Static List Implementation


● Dynamic List Implementation
Static List
● Static list implementation is done by storing the items in an array.
● The position of the element is given by an index starting with 0 to n-1, where
n is the number of elements.
● Static list is of fixed size.
● Operations on static list are:
○ Given an index, the element of that position can be accessed at constant time, without any
relation with the size of the list.
○ To add an element at the end of the list, it can also be done at constant time. Adding an
element ar any other position involves shifting up one position of all the subsequent elements,
so the time complexity depends on the size of the list.
○ Removing an item from the list involves shifting all the elements down by one position, so the
time complexity depends on the size of the list.
Dynamic List

● 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

● Supposing a list of fixed size N, ie,


index ranges from 0 to N-1.

Insertion of Node at kth Position.


Array Implementation of list [2]

● Supposing a list of fixed size N, ie,


index ranges from 0 to N-1.

Deletion of Node at kth Position.


Array Implementation of list [3]

Advantages of array implementation

● Variables are stored in contiguous memory locations


● Easy to handle data

Limitations

● Hard to add/remove elements in place.


● Cannot be dynamically resized
● Memory loss might be a problem.
Linked List

● 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

● Random access is not allowed. Elements need to be accessed sequentially


starting from the first node.
● Extra memory space for next pointer is required.
Array vs Linked List
Array vs Linked List[2]
Representation of Linked list

● 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:

INITIALIZE - initialize an empty linked list CONCAT - concatenation of two


TRAVERSE - traverse the linked list linked lists

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)

UPDATE - update specific element


Creating linked list

● Linked list is created using Dynamic Memory Allocation


● First define linked list node as a structure:
Creating linked list[2]

Then create function is defined as:


Traversing linked list

● Traversing of linked list always starts with first node.


● Initially pointer type variable is assigned to first node ie, HEAD.
P = HEAD;
● To travel the list in the forward direction, pointer is travelled through all the
nodes.
● Stop at the node where the value of next is NULL.

P = HEAD
while(p!=NULL)
P = p->next;
Counting the number of nodes

● Start by taking a variable count and initialize to 0.


● Traverse the linked list from strat node to last node and for each node,
increment count.
Printing all nodes

● Traverse the linked list from start to last node and print each node.

Void print(node *p)


{
while(p!=NULL)
{
printf(“%d”,p->data);
p=p->next;
}

}
Search element

● In order to search an element in linked list, we


start traversing from the first node.
● Traversal ends with success if element is found,
if not it ends with failure.
Concatenating two linked lists

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

1. Singly Linked list


2. Doubly linked list
3. Circular Linked list
a. Singly circular linked list
b. Doubly circular linked list
Singly Linked list

● The individual element is termed “Node”.


● Every Node consists of two fields, data & next.
● The data field is used to store actual value of that node while the next field is
used to store the address of the next node in the sequence.
● Only one link is present in each node. The link of the last node has a NULL
pointer.
Representation of Singly 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

● Creation of singly linked list - Use malloc


● After creation of the node, store the new item in the node using pointer.
Node *p

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]

Inserting nodes at beginning of the linked list

● 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]

Inserting nodes at last of the linked list

● 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]

Inserting nodes in place after a node

● To insert an element or a node at a position 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 specific node after which we want to insert the newNode
of the linked list.
○ Everytime check whether p has reached to the last node or not. If the last node is
reached display ‘Given node is not found in the list’ and exit.
○ Finally set newNode->next = p->next and p->next= newNode
Operation of Singly linked list[5]

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]

Deleting a node from beginning of the linked list:-

● 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]

Deleting a node from beginning of the linked list:-

● To delete a node from the begining of the linked list, the following steps are
done:
Operation of Singly linked list[8]

Deleting a node from end of the linked list:-

● 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]

Deleting a node from end of the linked list:-

● 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]

Deleting a specific node of the linked list:-

● 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

● A DLL can be traversed in both forward and backward


direction.
● The delete operation in DLL is more efficient if pointer to the
node to be deleted is given.
○ In DLL, to delete a node, pointer to the previous node is needed.
○ To get this previous node, sometimes the list is traversed. In DLL, we can
get the previous node using previous pointer.
Disadvantages of DLL over SLL

● Every node of DLL Require extra space for an previous pointer.


● All operations require an extra pointer previous to be
maintained.
● For example, in insertion, we need to modify previous pointers
together with next pointers.
Operation on DLL

Insertion of a new node at beginning of DLL


1. Create a newNode with given value and newNode —> previous as
NULL.
2. Check whether list is Empty (head = NULL)
3. If it is Empty then, assign NULL to newNode —> next and newNode
to head.
4. If it is not Empty then, assign head to newNode —> next and
newNode to head.
Operation on DLL[2]

Insertion of a new node at beginning of DLL


Adding a node with value 6 at the begining of a DLL:
Operation on DLL[3]

Insertion of a new node at beginning of DLL


Adding a node with value 6 at the begining of a DLL:
1. Create a new node
2. Set prev and next pointers
3. Make new node as head node
Operation on DLL[4]
Insertion of a new node after a node in DLL

1. Create a newNode with given value.


2. Check whether list is Empty (head = NULL)
3. If it is Empty then, assign NULL to newNode previous & newNode —> next and
newNode to head.
4. If it is not Empty then, define two node pointers tempi & temp2 and initialize tempi with
head.
5. Keep moving the tempt to its next node until it reaches to the node after which we want
to insert the newNode (until tempi->data is equal to location, here location is the node
value after which we want to insert the newNode).
6. Every time check whether tempi is reached to the last node. If it is reached to the last
node then display 'Given node not found!!! Insertion not possible!!!' and exit. Otherwise
move the temp1 to next node.
7. Assign tempi —+ next to temp2, newNode to tempi —* next, tempi to newNode
Operation on DLL[5]
Insertion of a new node after a node in DLL

1. Create a new node


2. Set the next pointer of new node and previous node
3. Set the previous pointer of new node and the next node
Operation on DLL[6]
Insertion of a new node after a node in DLL
Operation on DLL[7]
Insertion of a new node at the end in DLL
Operation on DLL[8]
Delete a node from the beginning in DLL

1. Check whether the list is EMPTY


2. If it is empty, then display ‘List is empty, cant delete’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Check whether the list is having only one element, (p->next = p->previous = NULL)
5. If it is true, then set HEAD to NULL and delete p.
6. If it is false, then assign p->next to head, NULL to head->previous and delete p.
Operation on DLL[9]
Delete a node from the endend in DLL

1. Check whether the list is EMPTY


2. If it is empty, then display ‘List is empty, cant delete’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Check whether the list is having only one element, (p->next = p->previous = NULL)
5. If it is true, then set HEAD to NULL and delete p.
6. If it is false, then keep moving p until it reaches to the last node in the list.
7. Assign q= p->previous and q->next = NULL and then delete p.
8. Set q= p->previous and q->next = NULL and delete p.
Operation on DLL[10]
Delete of a specific node in DLL

1. Check whether the list is EMPTY


2. If it is empty, then display ‘List is empty, cant delete’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Check whether the list is having only one element, (p->next = p->previous = NULL)
5. If it is true, then set HEAD to NULL and delete p.
6. If it is false, then keep moving p until it reaches to the last node in the list.
7. Set q= p->previous and q->next = NULL and delete p.
Operation on DLL[9]
Delete of a node a the beginning in DLL

1. Check whether the list is EMPTY


2. If it is empty, then display ‘List is empty, cant delete’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Check whether the list is having only one element, (p->next = p->previous = NULL)
5. If it is true, then set HEAD to NULL and delete p.
6. If it is false, then assign p->next to head, NULL to head->previous and delete p.
Operation on DLL[10]
Delete of a specific node in DLL
1. Check whether the list is EMPTY
2. If it is empty, then display ‘List is empty, cant delete’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Keep moving p until it reaches to the exact node to be deleted or to the last node
5. If it reached to the last node, then display ‘Given node not found’ and exit.
6. If it reached to the exact node, then check whether list is having only one node.
7. If list has only one node and that is the node to be deleted, then set HEAD to NULL and
delete p.
8. If list contains multiple nodes, then check whether p is the first node in the list.
9. If p is the first node, then move the head to the next node, set head of previous to
NULL and delete p.
10. If p is not the first node, check whether it is the last node in the list.
11. If p is the last node then set temp of previous of next to NULL and delete p.
12. If p is not the first node nor the last node, then set p of previous of next to p of next: ie
p->previous->next = p->next, p of next of previous to p of previous and delete p.
Operation on DLL[10] …
Operation on DLL[11] …
Displaying the elements of a DLL:

1. Check whether the list is EMPTY


2. If it is empty, then display ‘List is empty’ and exit.
3. If it is not empty, then define a Node pointer “p” and initialize with head.
4. Keep moving p to the next node until p reaches to the last node
5. At each step display p->data.
DLL Applications

● Redo and undo functionality in editor softwares.


● Forward and backward navigation in browsers
● For navigation systems where forward and backward navigation is required.
Circular Linked List
● A circular linked list is basically a linear LL that may be single or double
linked.
● The only difference from linear one is that there is no any NULL value
terminating the list.
● In fact every node in the list points to the next node and the last node points
to the first node, thus forming a circle. Since it forms a circle with no end to
stop, it is called circular LL.
● In circular LL, there can be no starting or ending node, whole list can be
traversed from any node.
● In order to traverse, we only need to traverse the entire list until the starting
node is not traversed again.
● A circular LL can be implemented using both singly LL and DLL.
Circular Linked List [1]
Circular Linked List [2]

● 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

● Insert - insert an element at the start of the circular linked list.


● Delete - delete an element from the start of the circular linked list.
● Display - display the elements in the list.
Stack as Linked List

● Problem with array implementation => fixed size


● The stack implemented using Linked list can work for an unlimited number of
data.
● In linked list implementation of a stack, every new element is inserted as “top”
element.
● Removing an element from the stack will remove the node pointed to be top
and top is moved to the next node in the list.
● The next field of the first element will always be NULL.
Stack as Linked List[2]

● 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

● Problem with array implementation => fixed size


● The queue implemented using Linked list can work for an unlimited number of
data.
● In linked list implementation of a queue, every new element is inserted as
“rear” element and the first node is always pointed to by “front”.
● Removing an element from the stack will remove the node pointed to be front
and front is moved to the next node in the list.
● The next field of the last inserted element will always be NULL.
Queue as Linked List [2]

● 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).

You might also like