Unit 3 CDS
Unit 3 CDS
o Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.
o 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.
o The last node of the list contains pointer to the null.
Till now, we were using array data structure to organize the group of elements that are to be stored individually in
the memory. However, Array has several advantages and disadvantages which must be known in order to decide
the data structure which will be used throughout the program.
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run
time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting any element in the array
needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using linked list is useful
because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored in the memory and
linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of declaration. List grows as per
the program's demand and limited to the available memory space.
Singly linked list can be defined as the collection of ordered set of elements. The number of elements may vary
according to need of the program. 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.
One way chain or singly linked list can be traversed only in one 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.
Consider an example where the marks obtained by the student in three subjects are stored in a linked list as shown
in the figure.
In the above figure, the arrow represents the links. The data part of every node contains the marks obtained by the
student in the different subject. The last node in the list is identified by the null pointer which is present in the
address part of the last node. We can have as many elements we require, in the data part of the list.
Complexity
Data Time Complexity Space
Structure Compleity
Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked List
There are various operations which can be performed on singly linked list. A list of all such operations is
given below.
Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
Insertion:
The insertion into a singly linked list can be performed at different positions. Based on the position of the
new node being inserted, the insertion is categorized into the following categories.
S.No Operation Description
1 Insertion at It involves inserting any element at the front of the list. We just need to
beginning a few link adjustments to make the new node as the head of the list.
2 Insertion at end It involves insertion at the last of the linked list. The new node can be
of the list inserted as the only node in the list or it can be inserted as the last one.
Different logics are implemented in each scenario.
3 Insertion after It involves insertion after the specified node of the linked list. We need
specified node to skip the desired number of nodes in order to reach the node after
which the new node will be inserted. .
The Deletion of a node from a singly linked list can be performed at different positions. Based on the position
of the node being deleted, the operation is categorized into the following categories.
1 Deletion at It involves deletion of a node from the beginning of the list. This is the simplest
beginning operation among all. It just need a few adjustments in the node pointers.
2 Deletion at the It involves deleting the last node of the list. The list can either be empty or full.
end of the list Different logic is implemented for the different scenarios.
3 Deletion after It involves deleting the node after the specified node in the list. we need to skip the
specified node desired number of nodes to reach the node after which the node will be deleted. This
requires traversing through the list.
4 Traversing In traversing, we simply visit each node of the list at least once in order to perform
some specific operation on it, for example, printing data part of each node present
in the list.
5 Searching In searching, we match each element of the list with the given element. If the element
is found on any of the location then location of that element is returned otherwise
null is returned. .
Inserting Elements to a Linked List
We will see how a new node can be added to an existing linked list in the following cases.
Consider the linked list shown in the figure. Suppose we want to create a new node with data 24 and add it as the
first node of the list. The linked list will be modified as follows.
Allocate memory for new node and initialize its DATA part to 24.
Add the new node as the first node of the list by pointing the NEXT part of the new node to HEAD.
Make HEAD to point to the first node of the list.
Algorithm: InsertAtBeginning
Allocate memory for new node and initialize its DATA part to 24.
Traverse to last node.
Point the NEXT part of the last node to the newly created node.
Make the value of next part of last node to NULL.
Algorithm: InsertAtEnd
The last case is when we want to add a new node after a given node. Suppose we want to add a new node with
value 24 after the node having data 9. These changes will be done in the linked list.
Allocate memory for new node and initialize its DATA part to 24.
Traverse the list until the specified node is reached.
Change NEXT pointers accordingly.
Algorithm: InsertAfterAnElement
Algorithm: DeleteFromBeginning
if(head == NULL)
{
printf("Underflow");
}
else
{
ptr = head;
head = head -> next;
free(ptr);
}
if(head == NULL)
{
printf("Underflow");
}
else
{
struct node* ptr =
head; struct node*
preptr = ptr; while(ptr-
>data!=num){ preptr =
ptr;
ptr = ptr->next;
}
struct node* temp = ptr;
preptr -> next = ptr ->
next; free(temp);
}
Search
Finding an element is similar to a traversal operation. Instead of displaying data, we have to check whether the data matches
with the item to find.
Initialize PTR with the address of HEAD. Now the PTR points to the first node of the linked list.
A while loop is executed which will compare data of every node with item.
If item has been found then control goes to last step.
Algorithm: Search
5. int data;
6. struct node *next;
7. };
8. struct node *head;
9.
10. void beginsert ();
11. void lastinsert ();
12. void randominsert();
13. void begin_delete();
14. void last_delete();
15. void random_delete();
16. void display();
17. void search();
18. void main ()
19. {
20. int choice =0;
21. while(choice != 9)
22. {
23. printf("\n\n*********Main Menu*********\n");
24. printf("\nChoose one option from the following list ...\n");
25. printf("\n===============================================\n");
26. printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginni
ng\n
27. 5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\n9.Exit\n
");
28. printf("\nEnter your choice?\n");
29. scanf("\n%d",&choice);
30. switch(choice)
31. {
32. case 1:
33. beginsert();
34. break;
35. case 2:
36. lastinsert();
37. break;
38. case 3:
39. randominsert();
40. break;
41. case 4:
42. begin_delete();
43. break;
44. case 5:
45. last_delete();
46. break;
47. case 6:
48. random_delete();
49. break;
50. case 7:
51. search();
52. break;
53. case 8:
54. display();
55. break;
56. case 9:
57. exit(0);
58. break;
59. default:
60. printf("Please enter valid choice..");
61. }
62. }
63. }
64. void beginsert()
65. {
66. struct node *ptr;
67. int item;
68. ptr = (struct node *) malloc(sizeof(struct node *));
69. if(ptr == NULL)
70. {
71. printf("\nOVERFLOW");
72. }
73. else
74. {
75. printf("\nEnter value\n");
76. scanf("%d",&item);
77. ptr->data = item;
78. ptr->next = head;
79. head = ptr;
80. printf("\nNode inserted");
81. }
82.
83. }
84. void lastinsert()
85. {
86. struct node *ptr,*temp;
87. int item;
88. ptr = (struct node*)malloc(sizeof(struct node));
89. if(ptr == NULL)
90. {
91. printf("\nOVERFLOW");
92. }
93. else
94. {
95. printf("\nEnter value?\n");
96. scanf("%d",&item);
97. ptr->data = item;
98. if(head == NULL)
99. {
100. ptr -> next = NULL;
101. head = ptr;
102. printf("\nNode inserted");
103. }
104. else
105. {
106. temp = head;
107. while (temp -> next != NULL)
108. {
109. temp = temp -> next;
110. }
111. temp->next = ptr;
112. ptr->next = NULL;
113. printf("\nNode inserted");
114.
115. }
116. }
117. }
118. void randominsert()
119. {
120. int i,loc,item;
121. struct node *ptr, *temp;
122. ptr = (struct node *) malloc (sizeof(struct node));
123. if(ptr == NULL)
124. {
125. printf("\nOVERFLOW");
126. }
127. else
128. {
129. printf("\nEnter element value");
130. scanf("%d",&item);
131. ptr->data = item;
132. printf("\nEnter the location after which you want to insert ");
133. scanf("\n%d",&loc);
134. temp=head;
135. for(i=0;i<loc;i++)
136. {
137. temp = temp->next;
138. if(temp == NULL)
139. {
140. printf("\ncan't insert\n");
141. return;
142. }
143.
144. }
145. ptr ->next = temp ->next;
146. temp ->next = ptr;
147. printf("\nNode inserted");
148. }
149. }
150. void begin_delete()
151. {
152. struct node *ptr;
153. if(head == NULL)
154. {
155. printf("\nList is empty\n");
156. }
157. else
158. {
159. ptr = head;
160. head = ptr->next;
161. free(ptr);
162. printf("\nNode deleted from the begining ...\n");
163. }
164. }
Output:
*********Main Menu*********
===============================================
1. Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter value
1
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter value?
2
Node inserted
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
printing values . . . . .
1
2
1
*********Main Menu*********
===============================================
5. Delete from last
6. Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter value?
123
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter value
1234
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
Enter your choice?
4
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter the location of the node after which you want to perform deletion
1
Deleted node 2
*********Main Menu*********
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
printing values . . . . .
1
1
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
*********Main Menu*********
Algorithm: Traverse
}
Linked list implementation of stack
Instead of using array, we can also use linked list to implement stack. Linked list allocates the memory dynamically.
However, time complexity in both the scenario is same for all the operations i.e. push, pop andpeek.
In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each nodecontains a
pointer to its immediate successor node in the stack. Stack is said to be overflown if the space leftin the memory heap is
not enough to create a node.
The top most node in the stack always contains null in its address field. Lets discuss the way in which, each
operation is performed in linked list implementation of stack.
20. {
21. ptr->val = val;
22. ptr->next = head;
23. head=ptr;
24.
25. }
26. printf("Item pushed");
27.
28. }
29. }
Deleting a node from the top of stack is referred to as pop operation. Deleting a node from the linked list
implementation of stack is different from that in the array implementation. In order to pop an element from the
stack, we need to follow the following steps :
1. Check for the underflow condition: The underflow condition occurs when we try to pop from an already
empty stack. The stack will be empty if the head pointer of the list points to null.
2. Adjust the head pointer accordingly: In stack, the elements are popped only from one end, therefore, the
value stored in the head pointer must be deleted and the node must be freed. The next node of the head nodenow
becomes the head node.
C implementation
1. void pop()
2. {
3. int item;
4. struct node *ptr;
5. if (head ==
NULL)6. {
7.
printf("Underflow");
8. }
9. else
10. {
11. item = head->val;
12. ptr = head;
13. head = head->next;
14. free(ptr);
15. printf("Item popped");
16.
17. }
18. }
Display the nodes (Traversing)
Displaying all the nodes of a stack needs traversing all the nodes of the linked list organized in the formof stack.
For this purpose, we need to follow the following steps.
C Implementation
1. void display()
2. {
3. int i;
4. struct node *ptr;
5. ptr=head;
6. if(ptr == NULL)
7. {
8. printf("Stack is
empty\n");9. }
10. else
11. {
12. printf("Printing Stack elements \n");
13. while(ptr!=NULL)
14. {
15. printf("%d\n",ptr->val);
16. ptr = ptr->next;
17. }
18. }
19. }
Menu Driven program in C implementing all the stack operations usinglinked list :
1. #include <stdio.h>
2. #include <stdlib.h>
3. void push();
4. void pop();
5. void display();
6. struct node
7. {
8. int val;
9. struct node *next;
10. };
11. struct node *head;
12.
13. void main ()
14. {
15. int choice=0;
16. printf("\n*********Stack operations using linked list*********\n");
17. printf("\n \n");
18. while(choice != 4)
19. {
20. printf("\n\nChose one from the below options...\n");
21. printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
22. printf("\n Enter your choice \n");
23. scanf("%d",&choice);
24. switch(choice)
25. {
26. case 1:
27. {
28. push();
29. break;
30. }
31. case 2:
32. {
33. pop();
34. break;
35. }
36. case 3:
37. {
38. display();
39. break;
40. }
41. case 4:
42. {
43. printf("Exiting.
");
44. break;
45. }
default:
46.
47. {
48. printf("Please Enter valid choice
");
49. }
50. };
51. }
52. }
53. void push ()
54. {
55. int val;
56. struct node *ptr = (struct node*)malloc(sizeof(struct node));
57. if(ptr == NULL)
58. {
59. printf("not able to push the element");
60. }
61. else
62. {
63. printf("Enter the value");
64. scanf("%d",&val);
65. if(head==NULL)
66. {
67. ptr->val = val;
68. ptr -> next = NULL;
69. head=ptr;
70. }
71. else
72. {
73. ptr->val = val;
74. ptr->next = head;
75. head=ptr;
76.76.
77. }
78. printf("Item
pushed");79.
80. }
81.
}82.
83. void pop()
84. {
85. int item;
The storage requirement of linked representation of a queue with n elements is o(n) while the timerequirement
for operations is o(1).
In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Eachelement of the
queue points to its immediate next element in the memory.
1. In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. The front
pointer contains the address of the starting element of the queue while the rear pointer contains the address of
the last element of the queue. rear -> next = ptr;
2. rear = ptr;
3. rear->next = NULL;
In this way, the element is inserted into the queue. The algorithm and the C implementation is givenas follows.
Algorithm
o Step 1: Allocate the space for the new node PTR
o Step 2: SET PTR -> DATA = VAL
o Step 3: IF FRONT =
NULLSET FRONT =
REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT =
NULLELSE
SET REAR -> NEXT =
PTRSET REAR = PTR
SET REAR -> NEXT =
NULL[END OF IF]
o Step 4: END
C Function
1. void insert(struct node *ptr, int item;
)2. {
3.
4.
5. ptr = (struct node *) malloc (sizeof(struct node));
6. if(ptr == NULL)
7. {
8. printf("\nOVERFLOW\n");
9. return;
10. }
11. else
12. {
13. ptr -> data = item;
14. if(front == NULL)
15. {
16. front = ptr;
17. rear = ptr;
18. front -> next = NULL;
19. rear -> next = NULL;
20. }
21. else
22. {
23. rear -> next = ptr;
24. rear = ptr;
25. rear->next = NULL;
26. }
27. }
28. }
Deletion
Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we need to check
either the list is empty or not. The condition front == NULL becomes true if the list is empty,in this case , we simply
write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the nodepointed by
the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node and free the node pointed by
the node ptr. This is done by using the following statements.
1. ptr = front;
2. front = front -> next;
3. free(ptr);
The algorithm and C function is given as follows.
Algorithm
o Step 1: IF FRONT =
NULLWrite " Underflow
"
Go to Step 5
[END OF
IF]
o Step 2: SET PTR = FRONT
o Step 3: SET FRONT = FRONT -> NEXT
o Step 4: FREE PTR
o Step 5: END
C Function
1. void delete (struct node *ptr)2.
{
3. if(front ==
NULL)4. {
5. printf("\nUNDERFLOW\n");
6. return;7.
}
8. else
9. {
10. ptr = front;
11. front = front -> next;
12. free(ptr);
13. }
14. }
Menu-Driven Program implementing all the operations on
Linked Queue
1. #include<stdio.h>
2. #include<stdlib.h>
3. struct node
4. {
5. int data;
6. struct node *next;
7. };
8. struct node *front;
9. struct node *rear;
10. void insert();
11. void delete();
12. void display();
13. void main ()
14. {
15. int choice;
16. while(choice != 4)
17. {
18. printf("\n*************************Main Menu*****************************\n");
19. printf("\n===========================================================
======\n");
20. printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
21. printf("\nEnter your choice ?");
102. }
103. }
104. }
Output:
***********Main Menu**********
==============================
1. insertan element
2.Delete an element
3.Display the queue
4.Exit
Enter value?
123
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter value?90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Example,
Input :
p1= 13x8 + 7x5 + 32x2 +
54 p2= 3x12 + 17x5 + 3x3
+ 98
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). A sample node in a doubly linked list is
shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following
image.
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;6.
}
The prev part of the first node and the next part of the last node will always contain null indicating end ineach
direction.
Node Creation
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. };
7. struct node *head;
All the remaining operations regarding doubly linked list are described in the following table.
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified Adding the node into the linked list after the specified node.
node
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node Removing the node which is present just after the node
having given data containing the given data.
7 Searching Comparing each node data with the item to be searched and
return the location of the item in the list if the item found else
return null.
8 Traversing Visiting each node of the list at least once in order to perform
some specific operation like searching, sorting, display, etc.
As in doubly linked list, each node of the list contain double pointers therefore we have to maintain morenumber of
pointers in doubly linked list as compare to singly linked list.
There are two scenarios of inserting any element into doubly linked list. Either the list is empty or it contains at least
one element. Perform the following steps to insert a node in doubly linked list at beginning.
o Allocate the space for the new node in the memory. This will be done by using the following statement.
1. ptr->next = NULL;
2. ptr->prev=NULL;
3. ptr->data=item;
4. head=ptr;
o In the second scenario, the condition head == NULL become false and the node will be inserted in beginning. The
next pointer of the node will point to the existing head pointer of the node. The prev pointer of the existing head will
point to the new node being inserted.
o This will be done by using the following statements.
1. ptr->next = head;
2. head→prev=ptr;
Since, the node being inserted is the first node of the list and therefore it must contain NULL in its prevpointer.
Hence assign null to its previous part and make the head point to this node.
1. ptr→prev =NULL
2. head = ptr
Algorithm :
Step 1: IF ptr = NULL
Write
OVERFLOWGo
to Step 9 [END
OF IF]
Step 2: SET NEW_NODE = ptr
Step 3: SET ptr = ptr -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET head -> PREV = NEW_NODE
Step 8: SET head = NEW_NODE
Step 9: EXIT
In order to insert a node in doubly linked list at the end, we must make sure whether the list is empty or itcontains any
element. Use the following steps in order to insert the node in doubly linked list at the end.
o Allocate the memory for the new node. Make the pointer ptr point to the new node being inserted.
1. ptr->next = NULL;
2. ptr->prev=NULL;
3. ptr->data=item;
4. head=ptr;
o In the second scenario, the condition head == NULL become false. The new node will be inserted as the last node of
the list. For this purpose, we have to traverse the whole list in order to reach the last node of the list. Initialize the
pointer temp to head and traverse the list by using this pointer.
1. Temp = head;
2. while (temp !=
NULL)3. {
4. temp = temp → next;
5. }
the pointer temp point to the last node at the end of this while loop. Now, we just need to make a few pointer
adjustments to insert the new node ptr to the list. First, make the next pointer of temp point to thenew node being
inserted i.e. ptr.
1. temp→next =ptr;
make the previous pointer of the node ptr point to the existing last node of the list i.e. temp.
make the next pointer of the node ptr point to the null as it will be the new last node of the list.
In order to insert a node after the specified node in the list, we need to skip the required number of nodesin order to
reach the mentioned node and then make the pointer adjustments as required.
o Allocate the memory for the new node. Use the following statements for this.
1. temp=head;
2. for(i=0;i<loc;i++)
3. {
4. temp = temp->next;
5. if(temp == NULL) // the temp will be //null if the list doesn't last long //up to mentioned location
6. {
7.
return;8.
}
9. }
o The temp would point to the specified node at the end of the for loop. The new node needs to be inserted after this
node therefore we need to make a fer pointer adjustments here. Make the next pointer of ptr point to the next node
of temp.
make the next pointer of temp point to the new node ptr.
make the previous pointer of the next node of temp point to the new node.
1. temp → next → prev = ptr;
Algorithm
o Step 1: IF PTR = NULL
Write
OVERFLOWGo to
Step 15 [END OF
IF]
GOTO STEP
15[END OF IF]
[END OF LOOP]
Deletion at beginning
Deletion in doubly linked list at the beginning is the simplest operation. We just need to copy the headpointer to
pointer ptr and shift the head pointer to its next.
1. Ptr = head;
2. head = head → next;
now make the prev of this new head node point to NULL. This will be done by using the followingstatements.
1. free(ptr)
Algorithm
o STEP 1: IF HEAD = NULL
WRITE
UNDERFLOWGOTO
STEP 6
Deletion of the last node in a doubly linked list needs traversing the list in order to reach the last node ofthe list and
then make pointer adjustments at that position.
In order to delete the last node of the list, we need to follow the following steps.
o If the list is already empty then the condition head == NULL will become true and therefore the operation
can not be carried on.
o If there is only one node in the list then the condition head → next == NULL become true. In this case, we
just need to assign the head of the list to NULL and free head in order to completely delete the list.
o Otherwise, just traverse the list to reach the last node of the list. This will be done by using the following
statements.
1. ptr = head;
2. if(ptr->next != NULL)
3. {
4. ptr = ptr -> next;
5. }
o The ptr would point to the last node of the ist at the end of the for loop. Just make the next pointer of the
previous node of ptr to NULL.
1. free(ptr)
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
[END OF LOOP]
In order to delete the node after the specified data, we need to perform the following steps.
1. temp = head
o Traverse the list until we find the desired data value.
Write UNDERFLOW
Go to Step 9
[END OF IF]
[END OF LOOP]
We just need traverse the list in order to search for a specific element in the list. Perform followingoperations in
order to search a specific operation.
o Copy head pointer into a temporary pointer variable ptr.
1. ptr = head
o declare a local variable I and assign it to 0.
1. i=0
o Traverse the list until the pointer ptr becomes null. Keep shifting pointer to its next and increasing i by +1.
o Compare each element of the list with the item which is to be searched.
o If the item matched with any node value then the location of that value I will be returned from the function else
NULL is returned.
Algorithm
o Step 1: IF HEAD == NULL
WRITE
"UNDERFLOW"GOTO
STEP 8
[END OF IF]
return i
[END OF IF]
o Step 6: i = i + 1
o Step 7: PTR = PTR → next
o Step 8: Exit
Traversing is the most common operation in case of each data structure. For this purpose, copy the headpointer in any
of the temporary pointer ptr.
1. Ptr = head
then, traverse through the list by using while loop. Keep shifting value of pointer variable ptr untilwe find
the last node. The last node contains null in its next part.
1. while(ptr !=
NULL)2. {
3. printf("%d\n",ptr->data);
4. ptr=ptr->next;
5. }
Although, traversing means visiting each node of the list once to perform some specific operation. Here, weare
printing the data associated with each node of the list.
Algorithm
o Step 1: IF HEAD == NULL
WRITE
"UNDERFLOW"GOTO
STEP 6
[END OF IF]
o Step 6: Exit
In a circular Singly linked list, the last node of the list contains a pointer to the first node of the list. We can have
circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The circular singlyliked list
has no beginning and no ending. There is no null value present in the next part of any of the nodes.
1 Insertion at beginning Adding a node into circular singly linked list at the beginning.
2 Insertion at the end Adding a node into circular singly linked list at the end.
2 Deletion at the Removing the node from circular singly linked list at the end.
end
3 Searching Compare each element of the node with the given item and return the locationat
which the item is present in the list otherwise return null.
4 Traversing Visiting each element of the list at least once in order to perform some specific
operation.
There are two scenario in which a node can be inserted in circular singly linked list at beginning. Either the
node will be inserted in an empty list or the node is to be inserted in an already filled list.
Firstly, allocate the memory space for the new node by using the malloc method of C language.
In the first scenario, the condition head == NULL will be true. Since, the list in which, we are inserting the
node is a circular singly linked list, therefore the only node of the list (which is just inserted into the list) will
point to itself only. We also need to make the head pointer point to this node. This will be done by using the
following statements.
1. if(head ==
NULL)2. {
3. head = ptr;
4. ptr -> next = head;5.
}
In the second scenario, the condition head == NULL will become false which means that the list contains at
least one node. In this case, we need to traverse the list in order to reach the last nodeof the list. This will be
done by using the following statement.
1. temp = head;
2. while(temp->next != head)
3. temp = temp->next;
At the end of the loop, the pointer temp would point to the last node of the list. Since, in a circularsingly linked
list, the last node of the list contains a pointer to the first node of the list. Therefore, we need to make the
next pointer of the last node point to the head node of the list and the new node which is being inserted into
the list will be the new head node of the list therefore the next pointer of temp will point to the new node ptr.
Now, make the new node ptr, the new head node of the circular singly linked list.
1. head = ptr;
in this way, the node ptr has been inserted into the circular singly linked list at beginning.
Algorithm
o Step 1: IF PTR = NULL
Write
OVERFLOWGo to
Step 11 [END OF
IF]
[END OF LOOP]
There are two scenario in which a node can be inserted in circular singly linked list at beginning. Either thenode will be
inserted in an empty list or the node is to be inserted in an already filled list.
Firstly, allocate the memory space for the new node by using the malloc method of C language.
In the first scenario, the condition head == NULL will be true. Since, the list in which, we are inserting
the node is a circular singly linked list, therefore the only node of the list (which is just inserted into the
list) will point to itself only. We also need to make the head pointer point to this node. This will be done by
using the following statements.
1. if(head ==
NULL) 2. {
3. head = ptr;
4. ptr -> next = head;
5. }
In the second scenario, the condition head == NULL will become false which means that the list contains
at least one node. In this case, we need to traverse the list in order to reach the last node of the list. This
will be done by using the following statement.
1. temp = head;
2. while(temp->next != head)
3. temp = temp->next;
At the end of the loop, the pointer temp would point to the last node of the list. Since, the new node which
is being inserted into the list will be the new last node of the list. Therefore the existing last node
i.e. temp must point to the new node ptr. This is done by using the following statement.
The new last node of the list i.e. ptr will point to the head node of the list.
In this way, a new node will be inserted in a circular singly linked list at the beginning.
Algorithm
o Step 1: IF PTR = NULL
Write
OVERFLOWGo
to Step 1 [END OF
IF]
o Step 2: SET NEW_NODE = PTR
o Step 3: SET PTR = PTR -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET NEW_NODE -> NEXT = HEAD
o Step 6: SET TEMP = HEAD
o Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
o Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
In order to delete a node in circular singly linked list, we need to make a few pointer adjustments.There are three
If the list is empty then the condition head == NULL will become true, in this case, we just need toprint
underflow on the screen and make exit.
1. if(head ==
NULL)2. {
3. printf("\nUNDERFLOW");
4. return;
5. }
Scenario 2: (The list contains single node)
If the list contains single node then, the condition head → next == head will become true. In this case, weneed to
delete the entire list and make the head pointer free. This will be done by using the following statements.
if(head->next == head)
1. {
2. head = NULL;
3. free(head);
4. }
Scenario 3: (The list contains more than one node)
If the list contains more than one node then, in that case, we need to traverse the list by using thepointer ptr
to reach the last node of the list. This will be done by using the following statements.
1. ptr = head;
2. while(ptr -> next != head)
3. ptr = ptr -> next;
At the end of the loop, the pointer ptr point to the last node of the list. Since, the last node of the list pointsto the head
node of the list. Therefore this will be changed as now, the last node of the list will point to thenext of the head node.
1. ptr->next = head->next;
Now, free the head pointer by using the free() method in C language.
1. free(head);
Make the node pointed by the next of the last node, the new head of the list.
1. head = ptr->next;
In this way, the node will be deleted from the circular singly linked list from the beginning.
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
o Step 2: SET PTR = HEAD
o Step 3: Repeat Step 4 while PTR → NEXT != HEAD
o Step 4: SET PTR = PTR → next
[END OF LOOP]
There are three scenarios of deleting a node in circular singly linked list at the end.
If the list is empty then the condition head == NULL will become true, in this case, we just need toprint
underflow on the screen and make exit.
1. if(head ==
NULL)2. {
3. printf("\nUNDERFLOW");
4. return;5.
}
Scenario 2(the list contains single element)
If the list contains single node then, the condition head → next == head will become true. In thiscase, we
need to delete the entire list and make the head pointer free. This will be done by using the following
statements.
1. if(head->next == head)
2. {
3. head = NULL;
4. free(head);
5. }
Scenario 3(the list contains more than one element)
If the list contains more than one element, then in order to delete the last element, we need to reach the last
node. We also need to keep track of the second last node of the list. For this purpose,the two pointers ptr and
preptr are defined. The following sequence of code is used for this purpose.
now, we need to make just one more pointer adjustment. We need to make the next pointer of preptr point to
the next of ptr (i.e. head) and then make pointer ptr free.
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
o Step 6: SET PREPTR -> NEXT = HEAD
o Step 7: FREE PTR
o Step 8: EXIT
Searching in circular singly linked list needs traversing across the list. The item which is to be searched in the list is
matched with each node data of the list once and if the match found then the location of that item is returned otherwise
-1 is returned.
Algorithm
o Step 1: SET PTR = HEAD
o Step 2: Set I = 0
o STEP 3: IF PTR = NULL
WRITE "EMPTY
LIST"GOTO STEP 8
END OF IF
[END OF IF]
o STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head
o STEP 6: if ptr → data = item
write i+1
RETUR
N
End of IF
o STEP 7: I = I + 1
o STEP 8: PTR = PTR → NEXT
[END OF LOOP]
o STEP 9: EXIT
Traversing in circular singly linked list can be done through a loop. Initialize the temporary pointer variable temp to
head pointer and run the while loop until the next pointer of temp becomes head. The algorithm and the c function
implementing the algorithm is described as follows.
Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL
WRITE "EMPTY
LIST"GOTO STEP 8
END OF IF
[END OF LOOP]
There are various operations which can be performed on circular doubly linked list. The node
structure of a circular doubly linked list is similar to doubly linked list. However, the operations
on circular doubly linkedlist is described in the following table.
Operation Description
1 Insertion at beginning Adding a node in circular doubly linked list at the beginning.
2 Insertion at end Adding a node in circular doubly linked list at the end.
3 Deletion at beginning Removing a node in circular doubly linked list from beginning.
4 Deletion at end Removing a node in circular doubly linked list at the end.
Traversing and searching in circular doubly linked list is similar to that in the circular
singly linked list.