top of page
vector1.jpg

LINKED LIST

In this article, we will see the introduction of linked list.

Linked list is a linear data structure that includes a series of connected nodes. Linked list can be defined as the nodes that are randomly stored in the memory. A node in the linked list contains two parts, i.e., first is the data part and second is the address part. The last node of the list contains a pointer to the null. After array, linked list is the second most used data structure. In a linked list, every link contains a connection to another link.

51791-link.gif

REPRESENTATION

Linked list can be represented as the connection of nodes in which each node points to the next node of the list. The representation of the linked list is shown below -

​

Till now, we have been 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 that must be known to decide the data structure that will be used throughout the program.

Now, the question arises why we should use linked list over array?

ds-linked-list.png

WHY TO USE LINKED LIST INSTEAD OF ARRAY ?

Linked list is a data structure that overcomes the limitations of arrays. Let's first see some of the limitations of arrays -

  • The size of the array must be known in advance before using it in the program.

  • Increasing the size of the array is a time taking process. It is almost impossible to expand the size of the array at run time.

  • All the elements in the array need to be contiguously stored in the memory. Inserting an element in the array needs shifting of all its predecessors.

Linked list is useful because -

  • It allocates the memory dynamically. All the nodes of the linked list are non-contiguously stored in the memory and linked together with the help of pointers.

  • In linked list, size 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.

LINK LIST DECLERATION !

It is simple to declare an array, as it is of single type, while the declaration of linked list is a bit more typical than array. Linked list contains two parts, and both are of different types, i.e., one is the simple variable, while another is the pointer variable. We can declare the linked list by using the user-defined data type structure.

The declaration of linked list is given as follows -

  1. struct node  

  2. {  

  3. int data;  

  4. struct node *next;  

  5. }  

In the above declaration, we have defined a structure named as node that contains two variables, one is data that is of integer type, and another one is next that is a pointer which contains the address of next node.

Now, let's move towards the types of linked list.

singly-linked-list-traversing.gif

TYPES OF LINKED LIST-

Linked list is classified into the following types -

  • Singly-linked list - Singly linked list can be defined as the collection of an ordered set of elements. A node in the singly linked list consists 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.

  • Doubly linked list - Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly-linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer), and pointer to the previous node (previous pointer).

  • Circular singly linked list - 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.

  • Circular doubly linked list - Circular doubly linked list is a more complex type of data structure in which a node contains pointers to its previous node as well as the next node. Circular doubly linked list doesn't contain NULL in any of the nodes. The last node of the list contains the address of the first node of the list. The first node of the list also contains the address of the last node in its previous pointer.

Now, let's see the benefits and limitations of using the Linked list.

1645011431318-Article_182.png

ADVANTAGES OF LINKED LIST

The advantages of using the Linked list are given as follows -

  • Dynamic data structure - The size of the linked list may vary according to the requirements. Linked list does not have a fixed size.

  • Insertion and deletion - Unlike arrays, insertion, and deletion in linked list is easier. Array elements are stored in the consecutive location, whereas the elements in the linked list are stored at a random location. To insert or delete an element in an array, we have to shift the elements for creating the space. Whereas, in linked list, instead of shifting, we just have to update the address of the pointer of the node.

  • Memory efficient - The size of a linked list can grow or shrink according to the requirements, so memory consumption in linked list is efficient.

  • Implementation - We can implement both stacks and queues using linked list.

Image by Den Harrson

DISADVANTAGE OF LINKED LIST

The limitations of using the Linked list are given as follows -

  • Memory usage - In linked list, node occupies more memory than array. Each node of the linked list occupies two types of variables, i.e., one is a simple variable, and another one is the pointer variable.

  • Traversal - Traversal is not easy in the linked list. If we have to access an element in the linked list, we cannot access it randomly, while in case of array we can randomly access it by index. For example, if we want to access the 3rd node, then we need to traverse all the nodes before it. So, the time required to access a particular node is large.

  • Reverse traversing - Backtracking or reverse traversing is difficult in a linked list. In a doubly-linked list, it is easier but requires more memory to store the back pointer.

download.jpeg

APPLICATIONS OF L.L-

The applications of the Linked list are given as follows -

  • With the help of a linked list, the polynomials can be represented as well as we can perform the operations on the polynomial.

  • A linked list can be used to represent the sparse matrix.

  • The various operations like student's details, employee's details, or product details can be implemented using the linked list as the linked list uses the structure data type that can hold different data types.

  • Using linked list, we can implement stack, queue, tree, and other various data structures.

  • The graph is a collection of edges and vertices, and the graph can be represented as an adjacency matrix and adjacency list. If we want to represent the graph as an adjacency matrix, then it can be implemented as an array. If we want to represent the graph as an adjacency list, then it can be implemented as a linked list.

  • A linked list can be used to implement dynamic memory allocation. The dynamic memory allocation is the memory allocation done at the run-time.

fotor_2023-1-27_11_9_55.png

OPERATIONS PERFORMED ON L.L

The basic operations that are supported by a list are mentioned as follows -

  • Insertion - This operation is performed to add an element into the list.

  • Deletion - It is performed to delete an operation from the list.

  • Display - It is performed to display the elements of the list.

  • Search - It is performed to search an element from the list using the given key.

130031-it-operations (1).gif

COMPLEXITY OF L.L

Now, let's see the time and space complexity of the linked list for the operations search, insert, and delete.

1. Time Complexity

Operations              Average case time complexity                        Worst-case time complexity

Insertion                                      O(1)                                                                       O(1)

Deletion                                       O(1)                                                                       O(1)

Search                                          O(n)                                                                      O(n)

Where 'n' is the number of nodes in the given tree.

2. Space Complexity

Operations                         Space complexity

Insertion                                     O(n)

Deletion                                      O(n)

Search                                         O(n)

​

The space complexity of linked list is O(n).

So, that's all about the introduction of linked list. Hope the article will be helpful and informative to you.

TYPES OF LINKED LIST

7031-colourfull-number-1-unscreen.gif

SINGLY LINKED LIST

It is the commonly used linked list in programs. If we are talking about the linked list, it means it is a singly linked list. The singly linked list is a data structure that contains two parts, i.e., one is the data part, and the other one is the address part, which contains the address of the next or the successor node. The address part in a node is also known as a pointer.

Suppose we have three nodes, and the addresses of these three nodes are 100, 200 and 300 respectively. The representation of three nodes as a linked list is shown in the below figure:

We can observe in the above figure that there are three different nodes having address 100, 200 and 300 respectively. The first node contains the address of the next node, i.e., 200, the second node contains the address of the last node, i.e., 300, and the third node contains the NULL value in its address part as it does not point to any node. The pointer that holds the address of the initial node is known as a head pointer.

The linked list, which is shown in the above diagram, is known as a singly linked list as it contains only a single link. In this list, only forward traversal is possible; we cannot traverse in the backward direction as it has only one link in the list.

Representation of the node in a singly linked list

  1. struct node  

  2. {  

  3.    int data;  

  4.    struct node *next;  

  5. }  

In the above representation, we have defined a user-defined structure named a node containing two members, the first one is data of integer type, and the other one is the pointer (next) of the node type.

linked-list.png
ds-types-of-linked-list.png

                      Singly linked list or One way chain

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 Structure                                        Time Complexity                                                 Space Compleity

                                                                                                    Average                                                                 WorstWorst

                                                                                   Access Search Insertion Deletion                    Access Search Insertion Deletion

                       Singly Linked List                                 θ(n)          θ(n)        θ(1)          θ(1)                             O(n)     O(n)        O(1)           O(1)

Operations on Singly 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.

​

    1Insertion at beginningIt involves inserting any element at the front of the list. We just need to a few link adjustments to make the new node as the head of the list.

    2Insertion at end of the listIt involves insertion at the last of the linked list. The new node can be inserted as the only node in the list or it can be inserted as the last one. Different logics are implemented in each scenario.

 

     3Insertion after specified nodeIt involves insertion after the specified node of the linked list. We need to skip the desired number of nodes in order to reach the  node after which the new node will be inserted. . 

DELETION & TRAVERSING

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.

SNOperationDescription

1Deletion at beginningIt involves deletion of a node from the beginning of the list. This is the simplest operation among all. It just need a few adjustments in the node pointers.

2Deletion at the end of the listIt involves deleting the last node of the list. The list can either be empty or full. Different logic is implemented for the different scenarios.

3Deletion after specified nodeIt involves deleting the node after the specified node in the list. we need to skip the desired number of nodes to reach the node after which the node will be deleted. This requires traversing through the list.

4TraversingIn 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.

5SearchingIn 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. .

Linked List in C: Menu Driven Program

​

  1. #include<stdio.h>  

  2. #include<stdlib.h>  

  3. struct node   

  4. {  

  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 Beginning\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. }  

  165. void last_delete()  

  166. {  

  167.     struct node *ptr,*ptr1;  

  168.     if(head == NULL)  

  169.     {  

  170.         printf("\nlist is empty");  

  171.     }  

  172.     else if(head -> next == NULL)  

  173.     {  

  174.         head = NULL;  

  175.         free(head);  

  176.         printf("\nOnly node of the list deleted ...\n");  

  177.     }  

  178.           

  179.     else  

  180.     {  

  181.         ptr = head;   

  182.         while(ptr->next != NULL)  

  183.         {  

  184.             ptr1 = ptr;  

  185.             ptr = ptr ->next;  

  186.         }  

  187.         ptr1->next = NULL;  

  188.         free(ptr);  

  189.         printf("\nDeleted Node from the last ...\n");  

  190.     }     

  191. }  

  192. void random_delete()  

  193. {  

  194.     struct node *ptr,*ptr1;  

  195.     int loc,i;    

  196.     printf("\n Enter the location of the node after which you want to perform deletion \n");  

  197.     scanf("%d",&loc);  

  198.     ptr=head;  

  199.     for(i=0;i<loc;i++)  

  200.     {  

  201.         ptr1 = ptr;       

  202.         ptr = ptr->next;  

  203.               

  204.         if(ptr == NULL)  

  205.         {  

  206.             printf("\nCan't delete");  

  207.             return;  

  208.         }  

  209.     }  

  210.     ptr1 ->next = ptr ->next;  

  211.     free(ptr);  

  212.     printf("\nDeleted node %d ",loc+1);  

  213. }  

  214. void search()  

  215. {  

  216.     struct node *ptr;  

  217.     int item,i=0,flag;  

  218.     ptr = head;   

  219.     if(ptr == NULL)  

  220.     {  

  221.         printf("\nEmpty List\n");  

  222.     }  

  223.     else  

  224.     {   

  225.         printf("\nEnter item which you want to search?\n");   

  226.         scanf("%d",&item);  

  227.         while (ptr!=NULL)  

  228.         {  

  229.             if(ptr->data == item)  

  230.             {  

  231.                 printf("item found at location %d ",i+1);  

  232.                 flag=0;  

  233.             }   

  234.             else  

  235.             {  

  236.                 flag=1;  

  237.             }  

  238.             i++;  

  239.             ptr = ptr -> next;  

  240.         }  

  241.         if(flag==1)  

  242.         {  

  243.             printf("Item not found\n");  

  244.         }  

  245.     }     

  246.           

  247. }  

  248.   

  249. void display()  

  250. {  

  251.     struct node *ptr;  

  252.     ptr = head;   

  253.     if(ptr == NULL)  

  254.     {  

  255.         printf("Nothing to print");  

  256.     }  

  257.     else  

  258.     {  

  259.         printf("\nprinting values . . . . .\n");   

  260.         while (ptr!=NULL)  

  261.         {  

  262.             printf("\n%d",ptr->data);  

  263.             ptr = ptr -> next;  

  264.         }  

  265.     }  

  266. }    

singly-link-list.png

*********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 Enter your choice?

1 Enter value

1 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 Enter your choice? 2 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 Enter your choice? 3 Enter element value1 Enter the location after which you want to insert 1 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 Enter your choice? 8 printing values . . . . . 1 2 1 *********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 Enter your choice? 2 Enter value? 123 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 Enter your choice? 1 Enter value 1234 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 Enter your choice? 4 Node deleted from the begining ... *********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 Enter your choice? 5 Deleted Node from the last ... *********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 Enter your choice? 6 Enter the location of the node after which you want to perform deletion 1 Deleted node 2 *********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 Enter your choice? 8 printing values . . . . . 1 1 *********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 Enter your choice? 7 Enter item which you want to search? 1 item found at location 1 item found at location 2 *********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 Enter your choice? 9

DOUBLY LINKED LIST

7032-colourfull-number-2-unscreen.gif

As the name suggests, the doubly linked list contains two pointers. We can define the doubly linked list as a linear data structure with three parts: the data part and the other two address part. In other words, a doubly linked list is a list that has three parts in a single node, includes one data part, a pointer to its previous node, and a pointer to the next node.

Suppose we have three nodes, and the address of these nodes are 100, 200 and 300, respectively. The representation of these nodes in a doubly-linked list is shown below:

​

​

​

​

​

​

As we can observe in the above figure, the node in a doubly-linked list has two address parts; one part stores the address of the next while the other part of the node stores the previous node's address. The initial node in the doubly linked list has the NULL value in the address part, which provides the address of the previous node.

Representation of the node in a doubly linked list

  1. struct node  

  2. {  

  3.   int data;  

  4.   struct node *next;  

  5.  struct node *prev;   

  6. }   

In the above representation, we have defined a user-defined structure named a node with three members, one is data of integer type, and the other two are the pointers, i.e., next and prev of the node type. The next pointer variable holds the address of the next node, and the prev pointer holds the address of the previous node. The type of both the pointers, i.e., next and prev is struct node as both the pointers are storing the address of the node of the struct node type.

DOUBLY.png

DOUBLY LINKED LIST

​

Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). 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.


 

In C, structure of a node in doubly linked list can be given as :

  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 in each direction.

In a singly linked list, we could traverse only in one direction, because each node contains address of the next node and it doesn't have any record of its previous nodes. However, doubly linked list overcome this limitation of singly linked list. Due to the fact that, each node of the list contains the address of its previous node, we can find all the details about the previous node as well by using the previous address stored inside the previous part of each node.

​

Memory Representation of a doubly linked list

​

Memory Representation of a doubly linked list is shown in the following image. Generally, doubly linked list consumes more space for every node and therefore, causes more expansive basic operations such as insertion and deletion. However, we can easily manipulate the elements of the list since the list maintains pointers in both the directions (forward and backward).

In the following image, the first element of the list that is i.e. 13 stored at address 1. The head pointer points to the starting address 1. Since this is the first element being added to the list therefore the prev of the list contains null. The next node of the list resides at address 4 therefore the first node contains 4 in its next pointer.

​

​

​

​

​

​

​

​

​

We can traverse the list in this way until we find any node containing null or -1 in its next part.

 

Operations on doubly linked list

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.

SNOperationDescription

​

1Insertion at beginning                                         Adding the node into the linked list at beginning.

2Insertion at end                                                     Adding the node into the linked list to the end.

3Insertion after specified node                          Adding the node into the linked list after the specified node.

4Deletion at beginning                                         Removing the node from beginning of the list

5Deletion at the end                                               Removing the node from end of the list.

6Deletion of the node having given data         Removing the node which is present just after the node containing the given data.

7Searching                                                              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.

8Traversing                                                           Visiting each node of the list at least once in order to perform some specific operation like searching, sorting, display, etc.

​

Menu Driven Program in C to implement all the operations of doubly linked list

​

  1. #include<stdio.h>  

  2. #include<stdlib.h>  

  3. struct node  

  4. {  

  5.     struct node *prev;  

  6.     struct node *next;  

  7.     int data;  

  8. };  

  9. struct node *head;  

  10. void insertion_beginning();  

  11. void insertion_last();  

  12. void insertion_specified();  

  13. void deletion_beginning();  

  14. void deletion_last();  

  15. void deletion_specified();  

  16. void display();  

  17. void search();  

  18. void main ()  

  19. {  

  20. int choice =0;  

  21.     while(choice != 9)  

  22.     {  

  23.         printf("\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 Beginning\n  

  27.         5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n");  

  28.         printf("\nEnter your choice?\n");  

  29.         scanf("\n%d",&choice);  

  30.         switch(choice)  

  31.         {  

  32.             case 1:  

  33.             insertion_beginning();  

  34.             break;  

  35.             case 2:  

  36.                     insertion_last();  

  37.             break;  

  38.             case 3:  

  39.             insertion_specified();  

  40.             break;  

  41.             case 4:  

  42.             deletion_beginning();  

  43.             break;  

  44.             case 5:  

  45.             deletion_last();  

  46.             break;  

  47.             case 6:  

  48.             deletion_specified();  

  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 insertion_beginning()  

  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 Item value");  

  76.     scanf("%d",&item);  

  77.       

  78.    if(head==NULL)  

  79.    {  

  80.        ptr->next = NULL;  

  81.        ptr->prev=NULL;  

  82.        ptr->data=item;  

  83.        head=ptr;  

  84.    }  

  85.    else   

  86.    {  

  87.        ptr->data=item;  

  88.        ptr->prev=NULL;  

  89.        ptr->next = head;  

  90.        head->prev=ptr;  

  91.        head=ptr;  

  92.    }  

  93.    printf("\nNode inserted\n");  

  94. }  

  95.      

  96. }  

  97. void insertion_last()  

  98. {  

  99.    struct node *ptr,*temp;  

  100.    int item;  

  101.    ptr = (struct node *) malloc(sizeof(struct node));  

  102.    if(ptr == NULL)  

  103.    {  

  104.        printf("\nOVERFLOW");  

  105.    }  

  106.    else  

  107.    {  

  108.        printf("\nEnter value");  

  109.        scanf("%d",&item);  

  110.         ptr->data=item;  

  111.        if(head == NULL)  

  112.        {  

  113.            ptr->next = NULL;  

  114.            ptr->prev = NULL;  

  115.            head = ptr;  

  116.        }  

  117.        else  

  118.        {  

  119.           temp = head;  

  120.           while(temp->next!=NULL)  

  121.           {  

  122.               temp = temp->next;  

  123.           }  

  124.           temp->next = ptr;  

  125.           ptr ->prev=temp;  

  126.           ptr->next = NULL;  

  127.           }  

  128.              

  129.        }  

  130.      printf("\nnode inserted\n");  

  131.     }  

  132. void insertion_specified()  

  133. {  

  134.    struct node *ptr,*temp;  

  135.    int item,loc,i;  

  136.    ptr = (struct node *)malloc(sizeof(struct node));  

  137.    if(ptr == NULL)  

  138.    {  

  139.        printf("\n OVERFLOW");  

  140.    }  

  141.    else  

  142.    {  

  143.        temp=head;  

  144.        printf("Enter the location");  

  145.        scanf("%d",&loc);  

  146.        for(i=0;i<loc;i++)  

  147.        {  

  148.            temp = temp->next;  

  149.            if(temp == NULL)  

  150.            {  

  151.                printf("\n There are less than %d elements", loc);  

  152.                return;  

  153.            }  

  154.        }  

  155.        printf("Enter value");  

  156.        scanf("%d",&item);  

  157.        ptr->data = item;  

  158.        ptr->next = temp->next;  

  159.        ptr -> prev = temp;  

  160.        temp->next = ptr;  

  161.        temp->next->prev=ptr;  

  162.        printf("\nnode inserted\n");  

  163.    }  

  164. }  

  165. void deletion_beginning()  

  166. {  

  167.     struct node *ptr;  

  168.     if(head == NULL)  

  169.     {  

  170.         printf("\n UNDERFLOW");  

  171.     }  

  172.     else if(head->next == NULL)  

  173.     {  

  174.         head = NULL;   

  175.         free(head);  

  176.         printf("\nnode deleted\n");  

  177.     }  

  178.     else  

  179.     {  

  180.         ptr = head;  

  181.         head = head -> next;  

  182.         head -> prev = NULL;  

  183.         free(ptr);  

  184.         printf("\nnode deleted\n");  

  185.     }  

  186.   

  187. }  

  188. void deletion_last()  

  189. {  

  190.     struct node *ptr;  

  191.     if(head == NULL)  

  192.     {  

  193.         printf("\n UNDERFLOW");  

  194.     }  

  195.     else if(head->next == NULL)  

  196.     {  

  197.         head = NULL;   

  198.         free(head);   

  199.         printf("\nnode deleted\n");  

  200.     }  

  201.     else   

  202.     {  

  203.         ptr = head;   

  204.         if(ptr->next != NULL)  

  205.         {  

  206.             ptr = ptr -> next;   

  207.         }  

  208.         ptr -> prev -> next = NULL;   

  209.         free(ptr);  

  210.         printf("\nnode deleted\n");  

  211.     }  

  212. }  

  213. void deletion_specified()  

  214. {  

  215.     struct node *ptr, *temp;  

  216.     int val;  

  217.     printf("\n Enter the data after which the node is to be deleted : ");  

  218.     scanf("%d", &val);  

  219.     ptr = head;  

  220.     while(ptr -> data != val)  

  221.     ptr = ptr -> next;  

  222.     if(ptr -> next == NULL)  

  223.     {  

  224.         printf("\nCan't delete\n");  

  225.     }  

  226.     else if(ptr -> next -> next == NULL)  

  227.     {  

  228.         ptr ->next = NULL;  

  229.     }  

  230.     else  

  231.     {   

  232.         temp = ptr -> next;  

  233.         ptr -> next = temp -> next;  

  234.         temp -> next -> prev = ptr;  

  235.         free(temp);  

  236.         printf("\nnode deleted\n");  

  237.     }     

  238. }  

  239. void display()  

  240. {  

  241.     struct node *ptr;  

  242.     printf("\n printing values...\n");  

  243.     ptr = head;  

  244.     while(ptr != NULL)  

  245.     {  

  246.         printf("%d\n",ptr->data);  

  247.         ptr=ptr->next;  

  248.     }  

  249. }   

  250. void search()  

  251. {  

  252.     struct node *ptr;  

  253.     int item,i=0,flag;  

  254.     ptr = head;   

  255.     if(ptr == NULL)  

  256.     {  

  257.         printf("\nEmpty List\n");  

  258.     }  

  259.     else  

  260.     {   

  261.         printf("\nEnter item which you want to search?\n");   

  262.         scanf("%d",&item);  

  263.         while (ptr!=NULL)  

  264.         {  

  265.             if(ptr->data == item)  

  266.             {  

  267.                 printf("\nitem found at location %d ",i+1);  

  268.                 flag=0;  

  269.                 break;  

  270.             }   

  271.             else  

  272.             {  

  273.                 flag=1;  

  274.             }  

  275.             i++;  

  276.             ptr = ptr -> next;  

  277.         }  

  278.         if(flag==1)  

  279.         {  

  280.             printf("\nItem not found\n");  

  281.         }  

  282.     }     

  283.           

  284. }  

doubly-linked-list.png
doubly-linked-list2.png
doubly-linked-list-memory-representation.png

​*********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value12 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value123 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value1234 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 2 Enter value89 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 3 Enter the location1 Enter value12345 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12345 12 89 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 4 node deleted *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 5 node deleted *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 12345 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 7 Enter item which you want to search? 123 item found at location 1 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 Can't delete *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 9 Exited..

CIRCULAR LINKED LIST

7033-colourfull-number-3-unscreen.gif

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 singly liked list has no beginning and no ending. There is no null value present in the next part of any of the nodes.

The following image shows a circular singly linked list.


 

Circular linked list are mostly used in task maintenance in operating systems. There are many examples where circular linked list are being used in computer science including browser surfing where a record of pages visited in the past by the user, is maintained in the form of circular linked lists and can be accessed again on clicking the previous button.

Memory Representation of circular linked list:

In the following image, memory representation of a circular linked list containing marks of a student in 4 subjects. However, the image shows a glimpse of how the circular list is being stored in the memory. The start or head of the list is pointing to the element with the index 1 and containing 13 marks in the data part and 4 in the next part. Which means that it is linked with the node that is being stored at 4th index of the list.

However, due to the fact that we are considering circular linked list in the memory therefore the last node of the list contains the address of the first node of the list.


 

We can also have more than one number of linked list in the memory with the different start pointers pointing to the different start nodes in the list. The last node is identified by its next part which contains the address of the start node of the list. We must be able to identify the last node of any linked list so that we can find out the number of iterations which need to be performed while traversing the list.

Operations on Circular Singly linked list:

Insertion

SNOperationDescription

1Insertion at beginningAdding a node into circular singly linked list at the beginning.

2Insertion at the endAdding a node into circular singly linked list at the end.

Deletion & Traversing

SNOperationDescription

1Deletion at beginningRemoving the node from circular singly linked list at the beginning.

2Deletion at the endRemoving the node from circular singly linked list at the end.

3SearchingCompare each element of the node with the given item and return the location at which the item is present in the list otherwise return null.

4TraversingVisiting each element of the list at least once in order to perform some specific operation.

Menu-driven program in C implementing all operations

on circular singly linked list

  1. #include<stdio.h>  

  2. #include<stdlib.h>  

  3. struct node   

  4. {  

  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 != 7)   

  22.     {  

  23.         printf("\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.Delete from Beginning\n4.Delete from last\n5.Search for an element\n6.Show\n7.Exit\n");  

  27.         printf("\nEnter your choice?\n");         

  28.         scanf("\n%d",&choice);  

  29.         switch(choice)  

  30.         {  

  31.             case 1:  

  32.             beginsert();      

  33.             break;  

  34.             case 2:  

  35.             lastinsert();         

  36.             break;  

  37.             case 3:  

  38.             begin_delete();       

  39.             break;  

  40.             case 4:  

  41.             last_delete();        

  42.             break;  

  43.             case 5:  

  44.             search();         

  45.             break;  

  46.             case 6:  

  47.             display();        

  48.             break;  

  49.             case 7:  

  50.             exit(0);  

  51.             break;  

  52.             default:  

  53.             printf("Please enter valid choice..");  

  54.         }  

  55.     }  

  56. }  

  57. void beginsert()  

  58. {  

  59.     struct node *ptr,*temp;   

  60.     int item;   

  61.     ptr = (struct node *)malloc(sizeof(struct node));  

  62.     if(ptr == NULL)  

  63.     {  

  64.         printf("\nOVERFLOW");  

  65.     }  

  66.     else   

  67.     {  

  68.         printf("\nEnter the node data?");  

  69.         scanf("%d",&item);  

  70.         ptr -> data = item;  

  71.         if(head == NULL)  

  72.         {  

  73.             head = ptr;  

  74.             ptr -> next = head;  

  75.         }  

  76.         else   

  77.         {     

  78.             temp = head;  

  79.             while(temp->next != head)  

  80.                 temp = temp->next;  

  81.             ptr->next = head;   

  82.             temp -> next = ptr;   

  83.             head = ptr;  

  84.         }   

  85.         printf("\nnode inserted\n");  

  86.     }  

  87.               

  88. }  

  89. void lastinsert()  

  90. {  

  91.     struct node *ptr,*temp;   

  92.     int item;  

  93.     ptr = (struct node *)malloc(sizeof(struct node));  

  94.     if(ptr == NULL)  

  95.     {  

  96.         printf("\nOVERFLOW\n");  

  97.     }  

  98.     else  

  99.     {  

  100.         printf("\nEnter Data?");  

  101.         scanf("%d",&item);  

  102.         ptr->data = item;  

  103.         if(head == NULL)  

  104.         {  

  105.             head = ptr;  

  106.             ptr -> next = head;    

  107.         }  

  108.         else  

  109.         {  

  110.             temp = head;  

  111.             while(temp -> next != head)  

  112.             {  

  113.                 temp = temp -> next;  

  114.             }  

  115.             temp -> next = ptr;   

  116.             ptr -> next = head;  

  117.         }  

  118.           

  119.         printf("\nnode inserted\n");  

  120.     }  

  121.   

  122. }  

  123.   

  124. void begin_delete()  

  125. {  

  126.     struct node *ptr;   

  127.     if(head == NULL)  

  128.     {  

  129.         printf("\nUNDERFLOW");    

  130.     }  

  131.     else if(head->next == head)  

  132.     {  

  133.         head = NULL;  

  134.         free(head);  

  135.         printf("\nnode deleted\n");  

  136.     }  

  137.       

  138.     else  

  139.     {   ptr = head;   

  140.         while(ptr -> next != head)  

  141.             ptr = ptr -> next;   

  142.         ptr->next = head->next;  

  143.         free(head);  

  144.         head = ptr->next;  

  145.         printf("\nnode deleted\n");  

  146.   

  147.     }  

  148. }  

  149. void last_delete()  

  150. {  

  151.     struct node *ptr, *preptr;  

  152.     if(head==NULL)  

  153.     {  

  154.         printf("\nUNDERFLOW");  

  155.     }  

  156.     else if (head ->next == head)  

  157.     {  

  158.         head = NULL;  

  159.         free(head);  

  160.         printf("\nnode deleted\n");  

  161.   

  162.     }  

  163.     else   

  164.     {  

  165.         ptr = head;  

  166.         while(ptr ->next != head)  

  167.         {  

  168.             preptr=ptr;  

  169.             ptr = ptr->next;  

  170.         }  

  171.         preptr->next = ptr -> next;  

  172.         free(ptr);  

  173.         printf("\nnode deleted\n");  

  174.   

  175.     }  

  176. }  

  177.   

  178. void search()  

  179. {  

  180.     struct node *ptr;  

  181.     int item,i=0,flag=1;  

  182.     ptr = head;   

  183.     if(ptr == NULL)  

  184.     {  

  185.         printf("\nEmpty List\n");  

  186.     }  

  187.     else  

  188.     {   

  189.         printf("\nEnter item which you want to search?\n");   

  190.         scanf("%d",&item);  

  191.         if(head ->data == item)  

  192.         {  

  193.         printf("item found at location %d",i+1);  

  194.         flag=0;  

  195.         }  

  196.         else   

  197.         {  

  198.         while (ptr->next != head)  

  199.         {  

  200.             if(ptr->data == item)  

  201.             {  

  202.                 printf("item found at location %d ",i+1);  

  203.                 flag=0;  

  204.                 break;  

  205.             }   

  206.             else  

  207.             {  

  208.                 flag=1;  

  209.             }  

  210.             i++;  

  211.             ptr = ptr -> next;  

  212.         }  

  213.         }  

  214.         if(flag != 0)  

  215.         {  

  216.             printf("Item not found\n");  

  217.         }  

  218.     }     

  219.           

  220. }  

  221.   

  222. void display()  

  223. {  

  224.     struct node *ptr;  

  225.     ptr=head;  

  226.     if(head == NULL)  

  227.     {  

  228.         printf("\nnothing to print");  

  229.     }     

  230.     else  

  231.     {  

  232.         printf("\n printing values ... \n");  

  233.           

  234.         while(ptr -> next != head)  

  235.         {  

  236.           

  237.             printf("%d\n", ptr -> data);  

  238.             ptr = ptr -> next;  

  239.         }  

  240.         printf("%d\n", ptr -> data);  

  241.     }  

  242.               

  243. }  

bottom of page