Linked List
Linked List
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
• Array gets memory allocated in the Stack section. Whereas, linked list gets memory allocated in Heap
section.
• 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.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.
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.
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.
5
1.2.5 Traversal of Singly Linked List
Displaying the contents of a linked list is very simple.
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.
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 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 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;
while(head != NULL) {
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
head = prevNode; // Make last node as head
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.
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:
• 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.
struct node
{
struct node *prev;
int data;
struct node *next;
};
• 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:
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
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.
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 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
– 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
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.
• 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.
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.
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 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:
• 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.
– 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:
• 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
41
1.5 Circular Doubly Linked List
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.
• 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:
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
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.
– 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:
• 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
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
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
56
• free(temp) - Free the memory allocated to node pointed by temp
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
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
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
• 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
• 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.:
– 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 -
• 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