0% found this document useful (0 votes)
20 views75 pages

Linked List

Uploaded by

Ak221197
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)
20 views75 pages

Linked List

Uploaded by

Ak221197
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/ 75

Data Structures Notes

by

Farhan Sufyan
Contents

1 Arrays 1
1.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Need of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Advantages of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Applications of Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Indexing of the array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.6 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 Disadvantages of using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.9 Types of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 One Dimensional (1-D) Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10.1 1-D Array Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.10.1.1 Array declaration by specifying size . . . . . . . . . . . . . . . . . . . . . . . 5
1.10.1.2 Array declaration by initializing elements . . . . . . . . . . . . . . . . . . . . 5
1.10.1.3 Array declaration by specifying size and initializing elements . . . . . . . . . . 5
1.10.1.4 Some Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.10.2 Accessing 1-D Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.10.2.1 Some Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.10.3 Traversal Operation on 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.10.4 Insertion Operations on 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.10.4.1 Inserting the Elements in Array . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.10.4.2 Inserting the Element at the Specific Location of Array . . . . . . . . . . . . . 10
1.10.4.3 Inserting the Element at the First Position of an Array . . . . . . . . . . . . . . 11
1.10.4.4 Inserting the Element at the Last Position of an Array . . . . . . . . . . . . . . 11
1.10.5 Time Complexity for Array insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.10.5.1 When maintaining given order of elements . . . . . . . . . . . . . . . . . . . . 12
1.10.5.2 When not maintaining given order of elements . . . . . . . . . . . . . . . . . . 12
1.10.6 Deletion Operation on 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.10.6.1 Deletion of the Element at the Specific Location of Array (while maintaining order) 13
1.10.6.2 Deletion of the Element at the First Position of an Array . . . . . . . . . . . . . 14
1.10.6.3 Deleting the Element from the Last Position of an Array . . . . . . . . . . . . . 14
1.10.7 Time Complexity for Array Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.10.7.1 While maintaining given order . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.10.7.2 while not maintaining the given order . . . . . . . . . . . . . . . . . . . . . . . 15
1.10.8 Updation Operation on 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.10.9 Home Assignment of 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.10.10 Videos Lectures on 1-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.11 Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.11.1 2-D Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11.2 Initializing Two – Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11.3 Accessing Elements of Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 20
1.11.4 Storing 2D array into Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.11.4.1 Row Major ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.11.4.2 Column Major ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.11.5 Home Assignment of 2-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.11.6 Videos Lectures on 2-D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1
1.12 Sparse Matrix and its representation using Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.12.1 Representation of Sparse Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.12.2 Array representation of the sparse matrix . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.13 Sample Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Chapter 1

Linked List

1.1 Linked List


• A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
• Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.
• A node contains two fields i.e. data stored at that particular address and the pointer which contains the
address of the next node in the memory.
• Each node holds its own data and the address of the next node hence forming a chain like structure

• Array gets memory allocated in the Stack section. Whereas, linked list gets memory allocated in Heap
section.

1.1.1 Why use linked list over array?


Linked list is the data structure which can overcome all the limitations of an array. Linked list is useful because:

• The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance.
Linked List allocates the memory dynamically.
• 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.

1.1.2 Advantages over Arrays


• Dynamic size.
• Ease of insertion/deletion: Insertion and deletion of elements doesn’t requires movement of all elements
when compared to an array.
• Efficient Memory Utilization: Memory is not pre-allocated. Memory is allocated when required and
de-allocated when no longer needed.

1
1.1.3 Drawbacks
• Random access is not allowed. We have to access elements sequentially starting from the first node.
• Extra memory space for a pointer is required with each element of the list, which means an additional
space of O(n) for every n node linked list

• Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not
there in case of linked lists.

1.1.4 Applications of Linked Lists


• Linked lists are used to implement stacks, queues, graphs, etc.

1.1.5 Basic Operations


• Traversal - Visit each element in the Linked List only once.
• Insertion - Insert an element in the Linked List.
• Deletion - Delete an element from the Linked List.
• Display - Display all the elements of the Linked List.

• Search - Search an element in Linked List using the given key.


• Sort - Sort the elements of Linked List in some logical order.

1.1.6 Types of Linked List


1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

4. Circular Doubly linked lists

2
1.2 Singly Linked List or One way chain
• Singly linked list is a collection of nodes linked together in a sequential way where each node of singly
linked list contains a data field and an address field which contains the reference of the next node.
• A node in the singly linked list consist of two parts: data part and link part. Data part of the node stores
actual information that is to be represented by the node while the link part of the node stores the address of
its immediate successor.

• To perform any operation on a linked list we must keep track/reference of the first node which may be
referred by head pointer variable.

• In singly linked list address field of last node must contain a NULL value specifying end of the list. The last
node in the list is identified by the null pointer which is present in the address part of the last node
• Singly linked list can contain multiple data fields but should contain at least single address field pointing
to its connected next node.

1.2.1 Advantages of Singly Linked List


• Requires less memory when compared to doubly, circular or doubly circular linked list.

1.2.2 Disadvantages of Singly Linked List


• Singly linked list can be traversed only in forward direction. In other words, we can say that each node
contains only next pointer, therefore we can not traverse the list in the reverse direction.

3
1.2.3 Defining a Linked List
• Defining node structure

struct node {
int data;
struct node *next;
};

• Where data is the data you want to store in list. *next is pointer to the same structure type.
• The *next will store location of next node if exists otherwise NULL.

• Linked list can have more than one data fields, but there is always single link filed pointing to the next node.

struct employee {
char name[50];
int code;
int salary;
struct employee *link;
};

4
1.2.4 Self Referential Structures
• Linked list are Self Referential structures.
• Self Referential structures are those structures that have one or more pointers which point to the same type
of structure, as their member.

• In other words, structures pointing to the same type of structures are self-referential in nature.

• Types of Self Referential Structures:


1. Self Referential Structure with Single Link - Singly Linked List.
2. Self Referential Structure with Multiple Links - Doubly Linked List.

5
1.2.5 Traversal of Singly Linked List
Displaying the contents of a linked list is very simple.

1. If the head is NULL, then linked list is empty.


2. Else we keep moving the temp node to the next one and display its contents.
3. When temp is NULL, we know that we have reached the end of the linked list so we get out of the while loop.

struct node *temp = head;

if (head == NULL){
printf("Empty List \n");
}
else{
printf("Elements of the list are :");

while(temp != NULL){
printf("%d --->",temp->data);
temp = temp->next;
}
}

6
1.2.6 Insertion in a Linked List
1. Insert in the beginning of the linked list
2. At the end of the linked list.
3. Insert at the specific location.

malloc() returns a pointer to the allocated memory, or NULL if the memory cannot be allocated to the new
node.

7
1.2.6.1 Insert at the Beginning
1. Create a newNode with given value.
2. Step 2 - Check whether list is Empty (head == NULL)
(a) Step 3 - If it is Empty then, assign NULL to newNode  next and newNode to head.
(b) Step 4 - Else (If it is not Empty then), assign head to newNode  next and newNode to head.

if(head == NULL){
head = newNode;
newNode->next = NULL;
} else {
newNode->next = head;
head = newNode;
}

8
1.2.6.2 Insert at the End
1. Create a newNode with given value and newNode  next as NULL.
2. Check whether list is Empty (head == NULL)
(a) If it is Empty, then assign newNode to head.
(b) Else (If it is not Empty then), define a node pointer temp and initialize with head.
i. Keep moving the temp to its next node until it reaches to the last node in the list (until temp 
next is equal to NULL).

ii. Assign newNode to temp next.

while(temp != NULL && temp->next != NULL){


temp = temp->next;
}
temp->next = newNode;

9
1.2.6.3 Insert a Node at a Specific Position
1. Create a newNode with given value.
2. Input the position of insertion of the new node of the list (scanf("%d", &pos);)
3. Check if the position is valid or not for insertion.
(Here, we are discussing only the case when the list is not empty and position of insertion is valid. (Refer the
Linked List code in lab for more detail.))
4. Define and initialize temp pointer with head (temp=head).
5. Traverse to the node just before the required position of new node.

6. Finally, Set ’newNode  next = temp  next’ and ’temp  next = newNode’

while(temp->next !=NULL) {
if(i < pos-1){
temp = temp->next;
i++;
}
else{
newNode->next = temp->next;
temp->next = newNode;
return;
}
}

10
1.2.7 Deletion in a Linked List
1. Delete from beginning of the Linked List
2. Delete from the end of the linked list.
3. Delete from the specific location.

The free() function in C library allows you to release or deallocate the memory blocks which are previously
allocated by calloc(), malloc() or realloc() functions.

11
1.2.7.1 Deletion from the Beginning
1. Check whether list is Empty (head == NULL)
2. If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.

if(head == NULL){
printf("List is Empty!!! \n");
return;
}


3. Else (If it is Not Empty then), define a Node pointer ’temp’ and initialize with head and Check whether list is
having only one node (temp next == NULL)
(a) If it is TRUE then, set head = NULL and delete temp (Setting Empty list conditions)

if(head->next == NULL){
free(head);
printf(" List is now Empty!!!\n");
}

(b) Else (If it is FALSE then) set head = temp  next, and delete temp.

temp = head;
head = temp->next;
free(temp);
printf("First node deleted!!!\n");

12
1.2.7.2 Deletion from the End
1. Check whether list is Empty (head == NULL)
2. If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
3. If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize ’temp1’ with head.

4. Check whether list has only one Node (temp1  next == NULL)
5. If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function. (Setting Empty list
condition)


6. If it is FALSE. Then, set ’temp2 = temp1 ’ and move temp1 to its next node. Repeat the same until it reaches
to the last node in the list. (until temp1 next == NULL)
7. Finally, Set temp2  next = NULL and delete temp1.
struct node* temp = head, temp2;
while(temp->next->next != NULL){
temp2 = temp1;
temp = temp->next;
}
temp2->next = NULL;
free(temp1);

13
1.2.7.3 Deletion from a specific Location
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize ’temp1’ with
head.
• Step 4 - Input the position of deletion of the node in the list
• Step 5 - Check if the position is valid.
• Step 6 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every
time set ’temp2 = temp1’ before moving the ’temp1’ to its next node.

– Step 7 - If it is reached to the last node then display ’Given node not found in the list! Deletion not
possible!!!’. And terminate the function.
– Step 8 - If it is reached to the exact node which we want to delete, then check whether list is having
only one node or not.
* Step 8.1 - If list has only one node and that is the node to be deleted, then set head = NULL and
delete temp1 (free(temp1)).
* Step 8.2 - Else list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 == head).
· Step 8.2.1 - If temp1 is the first node then move the head to the next node (head = head 
next) and delete temp1.

· Step 8.2.2 - Else temp1 is not first node then check whether it is last node in the list (temp1
next == NULL).
1. Step 8.2.2.1 - If temp1 is last node then set temp2 -> next = NULL and delete temp1
(free(temp1)).


2. Step 8.2.2.2 - Else temp1 is not first node and not last node then set temp2 next = temp1
next and delete temp1 (free(temp1)).

14
1.2.8 Reverse a Singly Linked List

• Step 1 - Check whether list is Empty (head == NULL)



– Step 1.1 - If it is Empty then, display ’List is Empty!!! Reverse is not possible’ and terminate the
function.
• Step 2 ELSE IF - Check whether list is having only one node or not (head -> next == NULL)
– Step 2.1 - If list has only one node, then it cannot be reversed. Display ’List contains only one Node !!!
Reverse is not possible’ and terminate the function.
• Step 3 ELSE - Create two more pointers other than head namely prevNode and curNode that will hold the
reference of previous node and current node respectively.
• Step 4
– Make sure that prevNode points to first node i.e. prevNode = head.
– head should now point to its next node i.e. the second node head = head  next.
– curNode should also points to the second node i.e. curNode = head

• Step 5 - Now, disconnect the previous node i.e. the first node from others. We will make sure that it points to
none. As this node is going to be our last node. Perform operation prevNode->next = NULL.

• Step 6 - Move head node to its next node i.e. head = head->next.

15
• Step 7 - Now, re-connect the current node to its previous node i.e. curNode->next = prevNode;

• Step 8 - Point the previous node to current node and current node to head node. Means they should now point
to prevNode = curNode; and curNode = head.

• Step 9 - Repeat steps 5-8 till head pointer becomes NULL.

• Step 10 - Now, after all nodes has been re-connected in the reverse order. Make the last node as the first node.
Means the head pointer should point to prevNode pointer. Perform head = prevNode;. Finally you end up
with a reversed linked list of its original.

16
if(head == NULL) {
printf("List is empty. \n");
return;
}
else if (head->next == NULL) {
printf("Single Element in the Linked List, cannot reverse a single element list. \n");
return;
}
else {
struct node *prevNode, *curNode;;

if(head != NULL) {

prevNode = head;
curNode = head->next;
head = head->next;

prevNode->next = NULL; // Make first node as last node

while(head != NULL) {
head = head->next;
curNode->next = prevNode;

prevNode = curNode;
curNode = head;
}
head = prevNode; // Make last node as head

printf("SUCCESSFULLY REVERSED LIST\n");


display();
}
}

17
1.2.9 Home Assignment
• Search an element in a Singly Linked List
• Sort the Singly Linked List in ascending order
• Remove duplicates from a Singly Linked List.

1.2.10 Video Lectures for Singly Linked List


• https://www.youtube.com/watch?v=dmb1i4oN5oE&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=7
• https://www.youtube.com/watch?v=DWpVGpNfDmM&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=8
• https://www.youtube.com/watch?v=qauEA64G1Ds&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=9
• https://www.youtube.com/watch?v=6wXZ_m3SbEs&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=10
• https://www.youtube.com/watch?v=dq3F3e9o2DM&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=11
• https://www.youtube.com/watch?v=ClvYytk5Rlg&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=12
• https://www.youtube.com/watch?v=SbGRuk38MvI&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=13
• https://www.youtube.com/watch?v=SbGRuk38MvI&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=14

18
1.3 Doubly Linked List (DLL)
• Doubly linked list is a variation of Singly 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 field(s).


– pointer to the next node in sequence (next pointer)
– pointer to the previous node (previous pointer)

Figure 1.1: DLL Node

Figure 1.2: DLL

• The first node of the DLL has its previous link pointing to NULL
• The last node of the DLL has its next node pointing to NULL.

19
1.3.1 Advantages of Doubly Linked List over Singly Linked List
• 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.
• We can quickly insert a new node before a given node.
In singly linked list, 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.

1.3.2 Disadvantages of Doubly Linked List over Singly Linked List


• Every node of DLL require extra space for an previous pointer.
• All operations require an extra pointer previous to be maintained.

1.3.3 Applications/Uses of doubly linked list in real life


• It is used by browsers to implement backward and forward navigation of visited web pages i.e. back and
forward button.
• It is also used by various application to implement Undo and Redo functionality.

1.3.4 Structure of a node in doubly linked list


Structure of a node in doubly linked list can be define using the following code (shown above also ??)

struct node
{
struct node *prev;
int data;
struct node *next;
};

1.3.5 Operations on Double Linked List


In a double linked list, we perform the following operations:
• Traversal

• Insertion
• Deletion

20
1.3.6 Traversal in a Double Linked List

21
1.3.7 Insertion in a Double Linked List
• Inserting at Beginning of the DLL
• Inserting at End of the DLL
• Inserting at Specific location in the DLL

22
1.3.7.1 Inserting at Beginning of the DLL
We can use the following steps to insert a new node at beginning of the double linked list

• Step 1 - Create a newNode with given value and newNode  prev = NULL.
• Step 2 - Check whether list is Empty (head == NULL)
• Step 3 - If it is Empty then, assign newNode  next = NULL and head = newNode.
• Step 4 - Else (If it is not Empty then), assign newNode  next = head and head = newNode.

23
1.3.7.2 Inserting at End of the DLL
We can use the following steps to insert a new node at end of the double linked list

• Step 1 - Create a newNode with given value and newNode  next == NULL.
• Step 2 - Check whether list is Empty (head == NULL)
• Step 3 - If it is Empty, then assign newNode  prev = NULL and head = newNode.
• Step 4 - Else (If it is not Empty then), define a node pointer temp and initialize with head.

• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (i.e temp  next ==
NULL).
• Step 6 - Assign temp  next = newNode and newNode  prev = temp.

24
1.3.7.3 Inserting At Specific location in the list
• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL)
• Step 3 - If it is Empty then, assign NULL to both newNode  previous & newNode  next and set newNode
to head.
• Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head.
• Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the
newNode

• Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then
display ’Given node is not found in the list!!! Insertion not possible!!!’ and terminate the function. Otherwise
move the temp1 to next node.
• Step 7 - Assign
– temp2 = temp1  next
– temp1  next = newNode
– newNode  previous = temp1
– newNode  next = temp2
– temp2  previous = newNode

25
1.3.8 Deletion
In a double linked list, the deletion operation can be performed in three ways as follows:

• Deleting from Beginning of the DLL


• Deleting from End of the DLL
• Deleting a Specific Node

26
1.3.8.1 Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the double linked list

• Step 1 - Check whether list is Empty (head == NULL)


• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is not Empty then, define a Node pointer ’temp’ and initialize with head.
• Step 4 - Check whether list is having only one node (temp  previous == temp  next or temp  next ==
NULL)
• Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
• Step 6 - If it is FALSE, then assign
– head = temp  next
– head = temp  next
– head  previous = NULL
– temp  next = NULL
• delete or free temp.

27
1.3.8.2 Deleting from End of the list
We can use the following steps to delete a node from end of the double linked list.

• Step 1 - Check whether list is Empty (head == NULL)


• Step 2 - If it is Empty, then display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is not Empty then, define a Node pointer ’temp’ and initialize with head.
 previous == NULL and temp  next == NULL or

• Step 4 - Check whether list has only one Node (temp
temp next == NULL)
• Step 5 - If it is TRUE, then assign head == NULL and delete temp. (Setting Empty list condition)
• Step 6 - If it is FALSE, then keep moving the temp until it reaches to the last node in the list (temp  next
== NULL).

• Step 7 - Assign temp  previous  next == NULL and delete temp.

28
1.3.8.3 Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the double linked list

• Step 1 - Check whether list is Empty (head == NULL)


• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is not Empty, then define a Node pointer ’temp’ and initialize with head.
• Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.

• Step 5 - If it is reached to the last node, then display ’Given node not found in the list! Deletion not possible!!!’
and terminate the function.
• Step 6 - If it is reached to the exact node which we want to delete, then

– temp  previous  next = temp  next

– temp  next  previous = temp  previous

– Finally delete the current node from memory and you are done (free(temp)).

29
1.3.9 Home Assignment - Doubly Linked List
• Reverse a given Doubly Linked List

1.3.10 Video Lectures for Doubly Linked List


• https://www.youtube.com/watch?v=nquQ_fYGGA4&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=15
• https://www.youtube.com/watch?v=H8-IuKKiQeo&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=16
• https://www.youtube.com/watch?v=v4szCPs9yEY&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=17
• https://www.youtube.com/watch?v=7yNUXcOcHwE&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=18
• https://www.youtube.com/watch?v=7yNUXcOcHwE&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=18
• https://www.youtube.com/watch?v=_6JI9XdO8nM&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=19

30
1.4 Circular Linked List or Singly Circular Linked List
• A circular linked list is a sequence of elements in which every element has a link to its next element in the
sequence and the last element has a link to the first element.
• There is no NULL pointer present in the next part of the end node.

1.4.1 Advantages of Circular Linked List


• Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to
stop when the first visited node is visited again.

• Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers,
FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required.

1.4.2 Disadvantages of Circular Linked List


• If we are at a given node and want to go back to the previous node, then we can not do it in single step.
Instead we have to complete the entire circle by going through the in between nodes and then we will reach
the required node.

1.4.3 Applications of Circular Linked List


• Circular linked list are mostly used in task maintenance in operating systems.
• Circular lists are used in applications where the entire list is accessed one-by-one in a loop.
• For example, when multiple applications are running on a PC, it is common for the operating system to put
the running applications on a list and then to cycle through them, giving each of them a slice of time to
execute, and then making them wait while the CPU is given to another application. It is convenient for the
operating system to use a circular list so that when it reaches the end of the list it can cycle around to the
front of the list.
• It is also used by Operating system to share time for different users, generally uses Round-Robin time sharing
mechanism.

31
1.4.4 Examples of Circular Linked List
• Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving
forward as a player’s chance ends.

1.4.5 Operations on Circular Linked List


• In a circular linked list, we perform the following operations:
– Traversal
– Insertion in Circular Linked List
* Inserting At Beginning of the list
* Inserting At End of the list
* Inserting At Specific location in the list
– Deletion
* Deleting from Beginning of the list
* Deleting from End of the list
* Deleting a Specific Node

32
1.4.6 Traversal of the Circular Linked List
In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In
a circular linked list, we stop traversal when we reach the first node again. Time Complexity: O(n)

• Step 1 - Check whether list is Empty (head == NULL)


• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is not Empty, then define a Node pointer ’temp’ and initialize with head (temp = head).

• Step 4 - Now, keep increment temp while temp is not equal to the head of the list. In every iteration, print the
temp’s data.
– print temp data
– temp = temp  next
– temp != head

33
1.4.7 Insertion in Circular Linked List
In a circular linked list, the insertion operation can be performed in three ways. They are as follows:

• Inserting at Beginning of the list


• Inserting at End of the list
• Inserting at Specific location in the list

1.4.7.1 Inserting at Beginning of the Circular Linked List


• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL)
• Step 3 - If it is Empty then, set head = newNode and newNode  next = head.
• Step 4 - If it is Not Empty then, define a pointer ’temp’ and initialize with ’head’ (temp = head).

• Step 5 - Keep moving the ’temp’ to its next node until it reaches to the last node (until ’temp  next ==
head’).
• Step 6 - Set ’newNode  next =head’, ’head = newNode’ and ’temp  next = head’.

34
1.4.7.2 Inserting at End of the Circular Linked List
• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If it is Empty then, set head = newNode and newNode  next = head.
• Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (temp  next ==
head).
• Step 6 - Set temp  next = newNode and newNode  next = head.

35
1.4.7.3 Inserting at Specific Location in the Circular Linked List
• Step 1 - Create a newNode with given value.
• Step 2 - Input the position ’pos’ from the user.
• Step 3 - If pos < 1, then print ’wrong position’

• Step 4 - If pos = 1. If the list is Empty or not then, perform ’insert at the beginning of CLL’.
• Step 5 - If the list is Empty and pos > 1, then print ’Empty List, cannot insert at the given location’.
• Step 6 - If the list is not Empty and invalid pos is input by the user, then print ’Invalid location’.
• Step 7 - If the list is not Empty and valid pos is input by the user, then Traverse to (N-1) position in the list.

• Step 8 -
– newNode  next = temp  next;
– temp  next = newNode;

36
• Suppose we want to insert a new node in the circular linked list at 3 position i.e. just after 2nd position. The
list initially contains 3 nodes. We will follow below steps to insert node at 2nd position in the list.

– Create a newNode and assign some data to its data field.

– Traverse to N-1 position in the list, in our case since we want to insert node at 3rd position therefore we
would traverse to 3-1 = 2nd position in the list. Say current pointer points to N-1th node.

– Link the next pointer field of newNode with the node pointed by the next pointer field of current(N-1)
node. Which means newNode.next = current.next.

– Connect the next pointer field of current node with the newly created node which means now next
pointer field of current node will point to newNode and you are done.

37
1.4.8 Deletion in Circular Linked List
In a circular linked list, the deletion operation can be performed in three ways those are as follows:

• Deleting from Beginning of the list


• Deleting from End of the list
• Deleting a Specific Node

1.4.9 Deleting from Beginning of the list


• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.

• Step 3 - If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize both ’temp1’
and ’temp2’ with head.
• Step 4 - Check whether list is having only one node (temp1  next == head)
• Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list conditions)

• Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1  next == head )
• Step 7 - Then set head = temp2  next, temp1  next = head and delete temp2.

38
1.4.10 Deleting from End of the list
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize ’temp1’ with
head.
• Step 4 - Check whether list has only one Node (temp1  next == head)
• Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function. (Setting
Empty list condition)


• Step 6 - If it is FALSE. Then, set ’temp2 = temp1 ’ and move temp1 to its next node. Repeat the same until
temp1 reaches to the last node in the list. (until temp1 next == head)
• Step 7 - Set temp2  next = head and delete temp1.

39
1.4.11 Deleting a Specific Node
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize ’temp1’ with
head.
• Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every
time set ’temp2 = temp1’ before moving the ’temp1’ to its next node.
• Step 5 - If temp1 is not first node and not last node then set temp2  next = temp1  next and delete temp1
(free(temp1)).

40
1.4.12 Self-Study
• Josephus Circle Problem

1.4.13 Videos Lectures for Circular Linked List


• https://www.youtube.com/watch?v=fmfx1C4TTxw&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=20
• https://www.youtube.com/watch?v=jsTybZ5qSNE&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=21
• https://www.youtube.com/watch?v=ReGglEXEH08&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=22
• https://www.youtube.com/watch?v=EkE6RHuMx3I&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=23

41
1.5 Circular Doubly Linked List

1.5.1 Advantages of Circular Doubly Linked List


• List can be traversed from both the directions i.e. from head to tail or from tail to head.
• Jumping from head to tail or from tail to head is done in constant time O(1).

1.5.2 Disadvantages of Circular Doubly Linked List


• It takes extra memory in each node to accommodate previous pointer.
• Lots of pointers involved while implementing or doing operations on a list. So, pointers should be handled
carefully otherwise data of the list may get lost.

1.5.3 Applications of Circular Doubly Linked List


• Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.
• Cache Memory Management: To maintain track of the most recently used cache blocks, circular doubly
linked lists are employed in cache memory management.

1.5.4 Examples of Circular doubly linked list


• Managing songs playlist in media player applications.

1.5.4.1 Operations on Circular Doubly Linked List


• In a Circular Doubly Linked List, we perform the following operations:
– Traversal
– Insertion
* Inserting At Beginning of the list
* Inserting At End of the list
* Inserting At Specific location in the list
– Deletion
* Deleting from Beginning of the list
* Deleting from End of the list
* Deleting a Specific Node

42
1.5.5 Traversing the Circular Doubly Linked List
The traverse is the process of visiting the nodes once from the beginning to the end of the list. Below are the steps
on how to traverse the list.
• Start iterating from beginning to end of the list using a temporary pointer that stores the address of the first
node.

• Iterate through the list until the head node comes again.
• Print the data that every node contains.
• Step 1 - Check whether list is Empty (head == NULL)

• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is not Empty, then define a Node pointer ’temp’ and initialize with head (temp = head).
• Step 4 - Now, keep increment temp while temp is not equal to the head of the list. In every iteration, print the
temp’s data.

– print temp data


– temp = temp  next
– temp != head
• Time Complexity: The time complexity of circular doubly linked list is O(n), here we are traversing the list
from the beginning to the end. Here, n is the size of the list.

• Space Complexity: Space is constant O(1) due to only the temporary variables taken to traverse the list.

43
1.5.6 Insertion in Circular Doubly Linked List
In a circular doubly linked list, the insertion operation can be performed in three ways. They are as follows:

• Inserting at Beginning of the list


• Inserting at End of the list
• Inserting at Specific location in the list

44
1.5.6.1 Inserting at Beginning of the Circular Doubly Linked List
• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If it is Empty then, set

– head = newNode
– newNode  next = head
– newNode  prev = head
• Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (temp  next ==
head).

• Step 6 - Set

– temp  next = newNode


– newNode  prev = temp
– newNode  next = head
– head  prev = newNode
– head = newNode

45
46
1.5.6.2 Inserting at End of the Circular Doubly Linked List
• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If it is Empty then, set

– head = newNode
– newNode  next = head
– newNode  prev = head
• Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (temp  next ==
head).

47
• Step 6 - Set
– newnode  next = head
– temp  next = newnode
– newNode  prev = temp
– head  prev = newNode

48
1.5.6.3 Inserting at Specific Location in the Circular Doubly Linked List
• Step 1 - Create a newNode with given value.
• Step 2 - Input the position ’pos’ from the user.
• Step 3 - If pos < 1, then print ’wrong position’

• Step 4 - If pos = 1. If the list is Empty or not then, perform ’insert at the beginning of CDLL’.
• Step 5 - If the list is Empty and pos > 1, then print ’Empty List, cannot insert at the given location’.
• Step 6 - If the list is not Empty and invalid pos is input by the user, then print ’Invalid location’.
• Step 7 - If the list is not Empty and valid pos is input by the user, then Traverse to (N − 1) position in the list.

49
• Suppose we want to insert a new node in the circular linked list at 3 position i.e. just after 2nd position. The
list initially contains 3 nodes. We will follow below steps to insert node at 2nd position in the list.

– Create a newNode and assign some data to its data field.

– Traverse to N-1 position in the list, in our case since we want to insert node at 3rd position therefore we
would traverse to 3-1 = 2nd position in the list. Say current pointer points to (N-1)th node.

50
– Change the pointers as follows:

newnode next = temp next

*
newnode prev = temp
 
*
temp next prev = newnode

*
* temp next = newnode

51
1.5.7 Deletion in Circular Doubly Linked List
In a circular doubly linked list, the deletion operation can be performed in three ways. They are as follows:

• Deletion at Beginning of the list


• Deletion at End of the list
• Deletion at Specific location of the list

1.5.8 Deleting at Beginning of the list


• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.

• Step 3 - If it is Not Empty then, define Node pointers ’temp’ and initialize ’temp’ with head.
• Step 4 - Check whether list is having only one node (temp  next == head)
• Step 5 - If it is TRUE then, set head = NULL and delete temp (Setting Empty list conditions)
• Step 6 - Else (If it is FALSE) move the temp until it reaches to the last node (temp  next == head)

52
• Step 7 - Set
– temp  next = head  next
– head  next  prev = temp

• Free the memory allocated to current head

– delete head (free(head))

• Change head to temp’s next as it points to the new head


– head = temp  next

53
1.5.9 Deleting at End of the list
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is Not Empty then, define Node pointers ’temp’ and initialize ’temp’ with head.

• Step 4 - Check whether list is having only one node (temp  next == head)
• Step 5 - If it is TRUE then, set head = NULL and delete temp (Setting Empty list conditions)
• Step 6 - Else (If it is FALSE) move the temp until it reaches to the last node (temp  next == head)

54
• Step 7 - Set
– temp  prev  next = head
– head  prev = temp  prev

• Free the memory allocated to current head

– delete temp (free(temp))

55
1.5.10 Deleting at a Specific Location
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display ’List is Empty!!! Deletion is not possible’ and terminate the function.
• Step 3 - If it is Not Empty then, define two Node pointers ’temp1’ and ’temp2’ and initialize ’temp1’ with
head.
• Suppose we want to delete a node in the circular doubly linked list at 3 position i.e. just after 2nd position.
The list initially contains 4 nodes. We will follow below steps to delete node at 3rd position in the list.

• Step 4 - Make a variable temp2 to point to temp1’s next i.e., the node to be deleted

• Change the pointers as follows:


– temp2  next  prev = temp1
– temp1  next = temp2  next

56
• free(temp) - Free the memory allocated to node pointed by temp

• The modified list:

57
1.5.11 Videos Lectures for Circular Doubly Linked List
• https://www.youtube.com/watch?v=eBCTtS_sptM&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=25
• https://www.youtube.com/watch?v=Fa958fGdgx0&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=26
• https://www.youtube.com/watch?v=ElQxT6hDeNE&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU&
index=28

58
1.6 Polynomial Representing and Manipulating using Linked List
1.6.1 Polynomial Representation
• It is a mathematical expression consisting of variables and constants.
• A polynomial p(x) is the expression in variable x which is in the form

(a0 xn + a1 xn−1 + a2 xn−2 .... + a)

where a0 , a1 , a2 ...., a fall in the category of real numbers and n is non negative integer, which is called the
degree of polynomial.

• An essential characteristic of the polynomial is that each term in the polynomial expression consists of two
parts
– One is the coefficient
– Other is the exponent

• Linked representation of polynomials, each term considered as a node, therefore these node contains three
fields.
– Coefficient Field - The coefficient field holds the value of the coefficient of a term
– Exponent Field - The Exponent field contains the exponent value of the term
– The linked field contains the address of the next term in the polynomial

59
• Consider a polynomial P(x) = 7x4 + 15x3 − 2x2 + 9. Here 7, 15, -2, and 9 are the coefficients, and 4,3,2,0 are
the exponents of the terms in the polynomial. On representing this polynomial using a linked list, we have

• Examples: 4x3 + 6x2 + 10x + 6

• Example- 4x7 + 12x2 + 45

60
61
1.6.2 Polynomial Addition
• To add two polynomials, we traverse the list P and Q.
• We take corresponding terms of the list P and Q and compare their exponents.
– If the two exponents are equal, the coefficients are added to create a new coefficient and append them
to the resultant list.
– If one of the exponents is larger than the other, the corresponding term is immediately placed into the
resultant list, and the term with the smaller exponent is held to be compared with the next term from the
other list.
– If the new coefficient is equal to 0, then the term is dropped.
– If one list ends before the other, the rest of the terms of the longer list is inserted at the end of the new
linked list containing the resulting list.

62
• Example: Let us consider an example an example to show how the addition of two polynomials is performed

P(x) = 3x4 + 2x3 − 4x2 + 7


Q(x) = 5x3 + 4x2 − 5

• These polynomials are represented using a linked list in order of decreasing exponents as follows:

• To generate a new linked list for the resulting polynomials that is formed on the addition of given polynomials
P(x) and Q(x), we perform the following steps:
– Traverse the two lists P and Q and examine all the nodes.
– We compare the exponents of the corresponding terms of two polynomials. The first term of polynomials
P and Q contain exponents 4 and 3, respectively. Since the exponent of the first term of the polynomial
P is greater than the other polynomial Q, the term having a larger exponent is inserted into the new list.
The new list initially looks as shown below:

63
– We then compare the exponent of the next term of the list P with the exponents of the present term of
list Q. Since the two exponents are equal, so their coefficients are added and appended to the new list as
follows:

– Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both
these terms are equal and after addition of their coefficients, we get 0, so the term is dropped, and no
node is appended to the new list after this

– Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the
same exponents equal to 0. We add their coefficients and append them to the new list for the resulting
polynomial as shown below:

64
• Example: Let us consider an example an example to show how the addition of two polynomials is performed

P(x) = 30x2 + 20x + 100


Q(x) = 60x3 + 50x2 + 60x

• These polynomials are represented using a linked list in order of decreasing exponents as follows:

• To generate a new linked list for the resulting polynomials that is formed on the addition of given polynomials
P(x) and Q(x), we perform the following steps:
– Traverse the two lists P and Q and examine all the nodes.
– We compare the exponents of the corresponding terms of two polynomials here
expo(p) < expo(q)
so, added the terms pointer q to the resultant list and now advanced the q pointer.

65
– Next we compare the exponents of the current items between given P and Q.
expo(p) = expo(q),
Therefore, add the coefficients of these two terms and link this to the resultant list and advance the
pointers p and q to their next nodes.:

– furthermore, we compare the exponents of the current terms again


expo(p) = expo(q)
Therefore, we add the coefficients of these two terms and link this to the resultant linked list and
advance the pointers to their next.

– You will notice that nodes Q reaches the NULL and P points the last node. So the last node in the first
polynomial is added to the end of the resultant linked list. Therefore, the display the resultant linked
list, the resultant linked list is the pointed to by the pointer.
– Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the
same exponents equal to 0. We add their coefficients and append them to the new list for the resulting
polynomial as shown below:

66
67
68
69
1.7 Sparse Matrix and its Representation using Linked List
Let’s understand the linked list representation of sparse matrix with the help of the example given below -

• Consider the sparse matrix -

• In the above figure, we can observe a 4x4 sparse matrix containing 5 non-zero elements and 11 zero elements.
Above matrix occupies 4x4 = 16 memory space. Increasing the size of matrix will increase the wastage
space.
• The linked list representation of the above matrix is given below -

• In linked list, each node has four fields. These four fields are defined as:
– Row: Index of row, where non-zero element is located
– Column: Index of column, where non-zero element is located
– Value: Value of the non zero element located at index – (row,column)
– Next node: Address of the next node
• In the above figure, the sparse matrix is represented in the linked list form. In the node, the first field
represents the index of the row, the second field represents the index of the column, the third field represents
the value, and the fourth field contains the address of the next node.
• In the above figure, the first field of the first node of the linked list contains 0, which means 0th row, the
second field contains 2, which means 2nd column, and the third field contains 1 that is the non-zero element.
So, the first node represents that element 1 is stored at the 0th row-2nd column in the given sparse matrix. In
a similar manner, all of the nodes represent the non-zero elements of the sparse matrix.

70
• Example:

71
1.8 Sample Questions
• 1. Illustrate with a diagram how a linked list is represented in memory.

• 2. Enumerate the advantages of a doubly linked list compared to a singly linked list.
• 3. Describe the advantage of a linked list over an array in brief.
• 4. Develop a C program to reverse a singly linked list.
• 5. Write a C function that deletes the Kth element from a two-way circular header list, given an integer K.
Please make suitable assumptions.
• 6. Demonstrate how a polynomial can be represented using a linked list. Additionally, provide an algorithm
to add two polynomials, each containing a minimum of four terms.
• 7. Create a C program to delete a specific element in a singly linked list. Discuss the conditions under
which a doubly linked list would be more beneficial than a singly linked list, despite its additional space
requirements.
• 9. Explain the concept of a circular linked list and write functions to perform the following operations on a
circular linked list:
– i) Creation of a list of nodes.
– ii) Insertion after a specific location.
– iii) Deletion of a node at a given position.
– iv) Displaying the list from the beginning to the end.
• 10. Outline the implementation of a singly linked list and provide C functions to perform the following
operations:
– Count the number of nodes.
– Reverse the direction of links.
– Delete alternate nodes (first, third, fifth, etc.)

72

You might also like