Circular Doubly Linked List

Circular Doubly Linked List in Data Structure

A circular doubly linked list is a more complicated data structure in which each node has references to both the previous and next node. There are no NULLs in any of the nodes in a circular doubly linked list. The address of the first node of the list is included in the last node of the list. In its previous pointer, the first node in the list also contains the address of the last node.
  • The following diagram depicts a circular doubly linked list.

  • Because a circular doubly linked list's structure is made up of three components, it requires more space per node and more costly fundamental operations. 
  • A circular doubly linked list, on the other hand, allows for easy manipulation of the pointers and makes searching twice as fast.
Circular Doubly linked list memory management

  • The allocation of memory for a circular doubly linked list is depicted in the diagram below. 
  • The variable head includes the address of the list's first element, i.e. 1, therefore the list's first node contains data A, which is stored at address 1. 
  • Because each node of the list is expected to have three pieces, the first node of the list comprises the addresses of the last node, 8, and the following node, 4, respectively. 
  • As seen in the picture, the last node of the list, which is stored at address 8 and includes data as 6, carries the address of the first node of the list, which is 1.
  • The last node of a circular doubly linked list is identified by the address of the first node, which is stored in the next section of the last node, therefore the node that holds the address of the first node is really the list's last node.
C program to implement all the operations on a circular doubly linked list

#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    struct node *prev;  
    struct node *next;  
    int data;  
};  
struct node *head;  
void insertion_beginning();  
void insertion_last();  
void deletion_beginning();  
void deletion_last();  
void display();  
void search();  
void main ()  
{  
int choice =0;  
    while(choice != 9)  
    {  
        printf("\n*********Main Menu*********\n");  
        printf("\nChoose one option from the following list ...\n");  
        printf("\n===============================================\n");  
        printf("\n1.Insert in Beginning\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search\n6.Show\n7.Exit\n");  
        printf("\nEnter your choice?\n");  
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insertion_beginning();  
            break;  
            case 2:  
                    insertion_last();  
            break;  
            case 3:  
            deletion_beginning();  
            break;  
            case 4:  
            deletion_last();  
            break;  
            case 5:  
            search();  
            break;  
            case 6:  
            display();  
            break;  
            case 7:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}  
void insertion_beginning()  
{  
   struct node *ptr,*temp;   
   int item;  
   ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
    printf("\nEnter Item value");  
    scanf("%d",&item);  
    ptr->data=item;  
   if(head==NULL)  
   {  
      head = ptr;  
      ptr -> next = head;   
      ptr -> prev = head;   
   }  
   else   
   {  
       temp = head;   
    while(temp -> next != head)  
    {  
        temp = temp -> next;   
    }  
    temp -> next = ptr;  
    ptr -> prev = temp;  
    head -> prev = ptr;  
    ptr -> next = head;  
    head = ptr;  
   }  
   printf("\nNode inserted\n");  
}  
     
}  
void insertion_last()  
{  
   struct node *ptr,*temp;  
   int item;  
   ptr = (struct node *) malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
       printf("\nEnter value");  
       scanf("%d",&item);  
        ptr->data=item;  
       if(head == NULL)  
       {  
           head = ptr;  
           ptr -> next = head;   
           ptr -> prev = head;   
       }  
       else  
       {  
          temp = head;  
          while(temp->next !=head)  
          {  
              temp = temp->next;  
          }  
          temp->next = ptr;  
          ptr ->prev=temp;  
          head -> prev = ptr;  
      ptr -> next = head;  
        }  
   }  
     printf("\nnode inserted\n");  
}  
  
void deletion_beginning()  
{  
    struct node *temp;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == head)  
    {  
        head = NULL;   
        free(head);  
        printf("\nnode deleted\n");  
    }  
    else  
    {  
        temp = head;   
        while(temp -> next != head)  
        {  
            temp = temp -> next;  
        }  
        temp -> next = head -> next;  
        head -> next -> prev = temp;  
        free(head);  
        head = temp -> next;  
    }  
  
}  
void deletion_last()  
{  
    struct node *ptr;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == head)  
    {  
        head = NULL;   
        free(head);   
        printf("\nnode deleted\n");  
    }  
    else   
    {  
        ptr = head;   
        if(ptr->next != head)  
        {  
            ptr = ptr -> next;   
        }  
        ptr -> prev -> next = head;  
        head -> prev = ptr -> prev;    
        free(ptr);  
        printf("\nnode deleted\n");  
    }  
}  
  
void display()  
{  
    struct node *ptr;  
    ptr=head;  
    if(head == NULL)  
    {  
        printf("\nnothing to print");  
    }     
    else  
    {  
        printf("\n printing values ... \n");  
          
        while(ptr -> next != head)  
        {  
          
            printf("%d\n", ptr -> data);  
            ptr = ptr -> next;  
        }  
        printf("%d\n", ptr -> data);  
    }  
              
}  
  
void search()  
{  
    struct node *ptr;  
    int item,i=0,flag=1;  
    ptr = head;   
    if(ptr == NULL)  
    {  
        printf("\nEmpty List\n");  
    }  
    else  
    {   
        printf("\nEnter item which you want to search?\n");   
        scanf("%d",&item);  
        if(head ->data == item)  
        {  
        printf("item found at location %d",i+1);  
        flag=0;  
        }  
        else   
        {  
        while (ptr->next != head)  
        {  
            if(ptr->data == item)  
            {  
                printf("item found at location %d ",i+1);  
                flag=0;  
                break;  
            }   
            else  
            {  
                flag=1;  
            }  
            i++;  
            ptr = ptr -> next;  
        }  
        }  
        if(flag != 0)  
        {  
            printf("Item not found\n");  
        }  
    }     
          
}  

Expected Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
1

Enter Item value123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
2

Enter value234

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
1

Enter Item value90

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
2

Enter value80

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
3

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
6

 printing values ... 
123

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
5

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 Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
7

Circular Singly Linked List

Circular Linked list in Data Structure

In a circular Sinly linked list, The last node of a singly linked list carries a pointer to the list's first node. Circular singly-linked lists and circular doubly linked lists are also possible.

We traverse a circular singly linked list until we reach the starting node. There is no beginning or conclusion to the circular list of singly liked items. In the following part of any of the nodes, there is no null value.

A circular singly linked list is depicted in the image below.


  • Circular linked lists are commonly used in operating systems for task maintenance. 
  • Circular linked lists are used in a variety of ways in computer science, including browser surfing, where a record of pages seen previously by the user is kept in the form of circular linked lists and can be accessed again by clicking the previous button.

Circular linked list memory representation.
  • A memory representation of a circular linked list comprising a student's marks in four disciplines is shown in the image below. 
  • The graphic, on the other hand, depicts how the circular list is stored in memory. 
  • The list's beginning, or head, points to the element with index 1 and 13 marks in the data part and 4 in the following section. 
  • That is, it is linked to the node that is recorded at the fourth index in the list.
  • However, because we are dealing with a circular linked list in memory, the last node of the list contains the address of the first node of the list.

  • We may also have many linked lists in memory, each with its own set of start pointers corresponding to the list's various start nodes. 
  • The next section of the last node, which holds the address of the list's start node, is used to identify it. 
  • Any linked list's last node must be identified in order to determine the number of iterations that must be done when traversing the list.
All operations on a circular singly linked list are implemented using a menu-driven C programme.

#include<stdio.h>  
#include<stdlib.h>  
struct node   
{  
    int data;  
    struct node *next;   
};  
struct node *head;  
  
void beginsert ();   
void lastinsert ();  
void randominsert();  
void begin_delete();  
void last_delete();  
void random_delete();  
void display();  
void search();  
void main ()  
{  
    int choice =0;  
    while(choice != 7)   
    {  
        printf("\n*********Main Menu*********\n");  
        printf("\nChoose one option from the following list ...\n");  
        printf("\n===============================================\n");  
        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");  
        printf("\nEnter your choice?\n");         
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            beginsert();      
            break;  
            case 2:  
            lastinsert();         
            break;  
            case 3:  
            begin_delete();       
            break;  
            case 4:  
            last_delete();        
            break;  
            case 5:  
            search();         
            break;  
            case 6:  
            display();        
            break;  
            case 7:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}  
void beginsert()  
{  
    struct node *ptr,*temp;   
    int item;   
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\nOVERFLOW");  
    }  
    else   
    {  
        printf("\nEnter the node data?");  
        scanf("%d",&item);  
        ptr -> data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;  
        }  
        else   
        {     
            temp = head;  
            while(temp->next != head)  
                temp = temp->next;  
            ptr->next = head;   
            temp -> next = ptr;   
            head = ptr;  
        }   
        printf("\nnode inserted\n");  
    }  
              
}  
void lastinsert()  
{  
    struct node *ptr,*temp;   
    int item;  
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\nOVERFLOW\n");  
    }  
    else  
    {  
        printf("\nEnter Data?");  
        scanf("%d",&item);  
        ptr->data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;    
        }  
        else  
        {  
            temp = head;  
            while(temp -> next != head)  
            {  
                temp = temp -> next;  
            }  
            temp -> next = ptr;   
            ptr -> next = head;  
        }  
          
        printf("\nnode inserted\n");  
    }  
  
}  
  
void begin_delete()  
{  
    struct node *ptr;   
    if(head == NULL)  
    {  
        printf("\nUNDERFLOW");    
    }  
    else if(head->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\nnode deleted\n");  
    }  
      
    else  
    {   ptr = head;   
        while(ptr -> next != head)  
            ptr = ptr -> next;   
        ptr->next = head->next;  
        free(head);  
        head = ptr->next;  
        printf("\nnode deleted\n");  
  
    }  
}  
void last_delete()  
{  
    struct node *ptr, *preptr;  
    if(head==NULL)  
    {  
        printf("\nUNDERFLOW");  
    }  
    else if (head ->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\nnode deleted\n");  
  
    }  
    else   
    {  
        ptr = head;  
        while(ptr ->next != head)  
        {  
            preptr=ptr;  
            ptr = ptr->next;  
        }  
        preptr->next = ptr -> next;  
        free(ptr);  
        printf("\nnode deleted\n");  
  
    }  
}  
  
void search()  
{  
    struct node *ptr;  
    int item,i=0,flag=1;  
    ptr = head;   
    if(ptr == NULL)  
    {  
        printf("\nEmpty List\n");  
    }  
    else  
    {   
        printf("\nEnter item which you want to search?\n");   
        scanf("%d",&item);  
        if(head ->data == item)  
        {  
        printf("item found at location %d",i+1);  
        flag=0;  
        }  
        else   
        {  
        while (ptr->next != head)  
        {  
            if(ptr->data == item)  
            {  
                printf("item found at location %d ",i+1);  
                flag=0;  
                break;  
            }   
            else  
            {  
                flag=1;  
            }  
            i++;  
            ptr = ptr -> next;  
        }  
        }  
        if(flag != 0)  
        {  
            printf("Item not found\n");  
        }  
    }     
          
}  
  
void display()  
{  
    struct node *ptr;  
    ptr=head;  
    if(head == NULL)  
    {  
        printf("\nnothing to print");  
    }     
    else  
    {  
        printf("\n printing values ... \n");  
          
        while(ptr -> next != head)  
        {  
          
            printf("%d\n", ptr -> data);  
            ptr = ptr -> next;  
        }  
        printf("%d\n", ptr -> data);  
    }  
              
}  

Expected Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
1

Enter the node data?10

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
2

Enter Data? 20

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
2

Enter Data?30

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
3

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
5

Enter item which you want to search?
20
item found at location 1
*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
6

 printing values ... 
20

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
7

Circular Linked List

A circular linked list is one in which the nodes are linked in such a way that they form a circle, with the last node pointing to the first node. There are no nodes in a circular linked list that have a null address space. 



There are two types:

1) Singly Linked List: 

  • It establishes a link to the first node by substituting the address of the first node for NULL. 
  • With the help of circular link generation, the Singly Linked List allows you to access the preceding node.

2) DLL (Doubly Linked List): 

  • A DLL (Doubly Linked List) has an extra pointer called the prior pointer.

The Benefits of a Singly and Doubly Linked List
  • Even if traversing ahead from a specific place, aids in obtaining earlier node information.
  • DLL is capable of traversing both forward and backward.
  • Due to the prior pointer, DLL is faster in retrieving previous node information.
  • Because it does not have a preceding pointer, a singly linked list takes up less space than a doubly linked list.

Algorithm

Addition of node at beginning of Circular linked list

  • Step 1. START
  • Step 2. Store the data to create a linked list.
  • Step 3. Enter the element to be at beginning of the list.
  • Step 4. Swap the address (header) of the first node.
  • Step 5. The first node address is swapped with the second node repeat till the last node address is swapped with the new first node address.
  • Step 6. STOP
Program
#include <stdio.h>  
#include <stdlib.h>   
struct node  
{  
    int data;  
    struct node *link;  
};  
   
struct node *head = NULL, *x, *y, *z;  
void ins_at_beg();  
void display();  
  
int main()  
{   int c;  
 x = (struct node*)malloc(sizeof(struct node));  
    printf("\n Enter the data to create linked list:");  
    scanf("%d", &x->data);  
    x->link = x;  
    head = x;  
    printf("\n If you wish to continue press 1 otherwise 0:");  
    scanf("%d", &c);  
    while (c != 0)  
    {  
        y = (struct node*)malloc(sizeof(struct node));  
        printf("\n Enter the data:");  
        scanf("%d", &y->data);  
        x->link = y;  
        y->link = head;  
        x = y;  
        printf("\n If you wish to continue press 1 otherwise 0:");  
        scanf("%d", &c);   
    }  
    ins_at_beg();   
    display();  
}  
  
void ins_at_beg()  
{  
    x = head;  
    y = (struct node*)malloc(sizeof(struct node));  
    printf("\n Enter the data to insert at beggining:");  
    scanf("%d", &y->data);  
    while (x->link != head)  
    {  
        x = x->link;  
    }  
    x->link = y;  
    y->link = head;  
    head = y;  
}  
  
void display()  
{  
       if (head == NULL)  
        printf("\n List is empty");  
    else  
    {  
        x = head;  
        while (x->link !=  head)  
        {   
            printf("%d->", x->data);  
            x = x->link;  
        }  
        printf("%d", x->data);  
    }   
      }  

Output
Enter the data to create a linked list:10                                                                                                                                                                                                                     
 If you wish to continue press 1 otherwise 0:1                                                                                                                                                                                                                     
 Enter the data:15                                                                                                                 
                                                                                                                                   
 If you wish to continue press 1 otherwise 0:1                                                                                                                                                                                                                       
 Enter the data:20                                                                                                                 
                                                                                                                                   
 If you wish to continue press 1 otherwise 0:1                                                                                                                                                                                                                      
 Enter the data:25                                                                                                                 
                                                                                                                                   
 If you wish to continue press 1 otherwise 0:1                                                                                                                                                                                                                        
 Enter the data:30                                                                                                                 
                                                                                                                                   
 If you wish to continue press 1 otherwise 0:1                                                                                                                                                                                                                        
 Enter the data:35                                                                                                                 
                                                                                                                                   
 If you wish to continue press 1 otherwise 0:0                                                                                                                                                                                                                        
 Enter the data to insert at beggining:5                                                                                           
5->10->15->20->25->30->35   

Insertion in singly linked list 2

Insertion in singly linked list after specified Node

  • To insert an element into a linked list after a certain number of nodes, we must skip the desired number of elements in the list and change the pointer to the spot where the node will be put. 
  • The following statements will be used to do this.
emp=head;  
            for(i=0;i<loc;i++)  
            {  
                temp = temp->next;  
                if(temp == NULL)  
                {  
                    return;  
                }  
            }
  • Allocate space for the new node and include the item in the data section. The following statements will be used to do this.
ptr = (struct node *) malloc (sizeof(struct node));  
        ptr->data = item; 
  • Only a few more link adjustments are needed, and our node will be put at the correct location. 
  • Because the loop pointer temp will be pointing to the node when the new node is placed at the end of the loop. 
  • As a result, the address of the next portion of the temp must be included in the next part of the new node ptr (since, ptr will be in between temp and the next of the temp). 
  • The following statements will be used to do this.
ptr→ next = temp → next
  • Now, we just need to make the next part of the temp, point to the new node ptr. This will insert the new node ptr, at the specified position.
temp ->next = ptr;   

Insertion in singly linked list

Algorithm

  • STEP 1: IF PTR = NULL
              WRITE OVERFLOW
              GOTO STEP 12
              END OF IF
  • STEP 2: SET NEW_NODE = PTR
  • STEP 3: NEW_NODE → DATA = VAL
  • STEP 4: SET TEMP = HEAD
  • STEP 5: SET I = 0
  • STEP 6: REPEAT STEP 5 AND 6 UNTIL I
  • STEP 7: TEMP = TEMP → NEXT
  • STEP 8: IF TEMP = NULL
               WRITE "DESIRED NODE NOT PRESENT"
                GOTO STEP 12
                END OF IF
                END OF LOOP
  • STEP 9: PTR → NEXT = TEMP → NEXT
  • STEP 10: TEMP → NEXT = PTR
  • STEP 11:  SET PTR = NEW_NODE
  • STEP 12:  EXIT
C program to insert data(after specific node) in singly linked list.
#include<stdio.h>  
#include<stdlib.h>  
void randominsert(int);  
void create(int);  
struct node  
{  
    int data;  
    struct node *next;  
};  
struct node *head;  
void main ()  
{  
    int choice,item,loc;  
    do   
    {  
        printf("\nEnter the item which you want to insert?\n");  
        scanf("%d",&item);  
        if(head == NULL)  
        {  
            create(item);  
        }  
        else  
        {  
            randominsert(item);  
        }  
        printf("\nPress 0 to insert more ?\n");  
        scanf("%d",&choice);  
    }while(choice == 0);  
}  
void create(int item)  
{  
      
        struct node *ptr = (struct node *)malloc(sizeof(struct node *));  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW\n");  
        }  
        else  
        {  
            ptr->data = item;  
            ptr->next = head;  
            head = ptr;  
            printf("\nNode inserted\n");  
        }  
}  
void randominsert(int item)  
    {  
        struct node *ptr = (struct node *) malloc (sizeof(struct node));  
        struct node *temp;  
        int i,loc;  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");  
        }  
        else  
        {  
              
            printf("Enter the location");  
            scanf("%d",&loc);             
            ptr->data = item;  
            temp=head;  
            for(i=0;i<loc;i++)  
            {  
                temp = temp->next;  
                if(temp == NULL)  
                {  
                    printf("\ncan't insert\n");  
                    return;  
                }  
              
            }  
            ptr ->next = temp ->next;   
            temp ->next = ptr;   
            printf("\nNode inserted");  
        }  
          
    }  

Expected Output:

Enter the item which you want to insert?
12
Node inserted
Press 0 to insert more ?
2

Insertion in singly linked list 1

Insertion in singly linked list (at the end).

There are two cases that need to be described in order to put a node at the end.
  1. The node is being added to a list that is currently empty.
  2. The node is appended to the connected list's end.
The first case:
  • The condition (head == NULL) is satisfied in the first case. As a result, we only need to use the malloc statement in C to allocate space for the node. The following statements are used to build up the data and link parts of the node.
ptr->data = item;  
                ptr -> next = NULL; 
  • Because ptr is the only node that will be added into the list, it must be pointed to by the list's head pointer. The following statements will be used to do this.
Head = ptr     

The second case:

  • Because Head is not null, the condition Head = NULL would fail. In order to traverse the list, we must first declare a temporary pointer temp. temp is set to point to the list's initial node.
Temp = head  
  • Then, traverse through the entire linked list using the statements: 
while (temp→ next != NULL)  
            temp = temp → next; 
  • The temp will point to the last node of the list at the end of the loop. Now, set aside space for the new node and assign the item to its data section. 
  • Because the new node will be the last node in the list, the next component of this node must point to null. The next section of the temp node (which is presently the list's last node) must point to the new node (ptr).
temp = head;  
        while (temp -> next != NULL)  
        {  
            temp = temp -> next;  
        }  
        temp->next = ptr;  
        ptr->next = NULL;

Insertion in singly linked list


Algorithm

  • Step 1: IF PTR = NULL Write OVERFLOW
         Go to Step 1
         [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 - > NEXT = NULL
  • Step 6: SET PTR = HEAD
  • Step 7: Repeat Step 8 while PTR - > NEXT != NULL
  • Step 8: SET PTR = PTR - > NEXT
         [END OF LOOP]
  • Step 9: SET PTR - > NEXT = NEW_NODE
  • Step 10: EXIT
C program to insert data(at the end) in singly linked list.

#include<stdio.h>  
#include<stdlib.h>  
void lastinsert(int);  
struct node  
{  
    int data;  
    struct node *next;  
};  
struct node *head;  
void main ()  
{  
    int choice,item;  
    do   
    {  
        printf("\nEnter the item which you want to insert?\n");  
        scanf("%d",&item);  
        lastinsert(item);  
        printf("\nPress 0 to insert more ?\n");  
        scanf("%d",&choice);  
    }while(choice == 0);  
}  
void lastinsert(int item)  
    {  
        struct node *ptr = (struct node*)malloc(sizeof(struct node));     
        struct node *temp;  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");     
        }  
        else  
        {  
            ptr->data = item;  
            if(head == NULL)  
            {  
                ptr -> next = NULL;  
                head = ptr;  
                printf("\nNode inserted");  
            }  
            else  
            {  
                temp = head;  
                while (temp -> next != NULL)  
                {  
                    temp = temp -> next;  
                }  
                temp->next = ptr;  
                ptr->next = NULL;  
                printf("\nNode inserted");  
              
            }  
        }  
    }  

Expected Output:

Enter the item which you want to insert?
12
Node inserted
Press 0 to insert more ?
0
Enter the item which you want to insert?
23
Node inserted
Press 0 to insert more ?
2

Doubly Linked List

Two-way chain or doubly-linked list

A doubly linked list is a more complicated sort of linked list in which each node has a pointer to both the previous and next node in the sequence. As a result, a node in a doubly linked list has three parts: node data, pointer to the next node in the sequence (next pointer), and pointer to the prior node (previous pointer). The graphic depicts a sample node in a doubly linked list.

mskuthar


Structure of a node in a doubly linked list

struct node   
{  
    struct node *prev;   
    int data;  
    struct node *next;   
}   
  • The prev part of the first node and the next part of the last node will always contain null indicating end in each direction.
  • Because each node contains the address of the next node and has no record of its previous nodes, we can only navigate in one way in a singly linked list. 
  • The limitation of a singly linked list is overcome by a doubly linked list. 
  • We can obtain all the facts about the previous node by using the previous address stored inside the previous section of each node because each node of the list has the address of its previous node.
A doubly linked list's memory representation.
  • The following graphic displays the memory representation of a doubly linked list. 
  • In general, a doubly-linked list takes up more space for each node, making fundamental operations like insertion and deletion take longer. 
  • However, because the list keeps pointers in both directions, we can simply change the list's elements (forward and backward).
  • The first entry of the list, i.e. 13, is stored at address 1 in the figure below. The starting address 1 is indicated by the head pointer. 
  • The prior of the list includes null because this is the first piece added to the list.

Operations on doubly linked list

Node Creation

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

All the operations of a doubly linked list can be implemented using a menu-driven program written in C.

#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    struct node *prev;  
    struct node *next;  
    int data;  
};  
struct node *head;  
void insertion_beginning();  
void insertion_last();  
void insertion_specified();  
void deletion_beginning();  
void deletion_last();  
void deletion_specified();  
void display();  
void search();  
void main ()  
{  
int choice =0;  
    while(choice != 9)  
    {  
        printf("\n*********Main Menu*********\n");  
        printf("\nChoose one option from the following list ...\n");  
        printf("\n===============================================\n");  
        printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n  
        5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n");  
        printf("\nEnter your choice?\n");  
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insertion_beginning();  
            break;  
            case 2:  
                    insertion_last();  
            break;  
            case 3:  
            insertion_specified();  
            break;  
            case 4:  
            deletion_beginning();  
            break;  
            case 5:  
            deletion_last();  
            break;  
            case 6:  
            deletion_specified();  
            break;  
            case 7:  
            search();  
            break;  
            case 8:  
            display();  
            break;  
            case 9:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}  
void insertion_beginning()  
{  
   struct node *ptr;   
   int item;  
   ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
    printf("\nEnter Item value");  
    scanf("%d",&item);  
      
   if(head==NULL)  
   {  
       ptr->next = NULL;  
       ptr->prev=NULL;  
       ptr->data=item;  
       head=ptr;  
   }  
   else   
   {  
       ptr->data=item;  
       ptr->prev=NULL;  
       ptr->next = head;  
       head->prev=ptr;  
       head=ptr;  
   }  
   printf("\nNode inserted\n");  
}  
     
}  
void insertion_last()  
{  
   struct node *ptr,*temp;  
   int item;  
   ptr = (struct node *) malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
       printf("\nEnter value");  
       scanf("%d",&item);  
        ptr->data=item;  
       if(head == NULL)  
       {  
           ptr->next = NULL;  
           ptr->prev = NULL;  
           head = ptr;  
       }  
       else  
       {  
          temp = head;  
          while(temp->next!=NULL)  
          {  
              temp = temp->next;  
          }  
          temp->next = ptr;  
          ptr ->prev=temp;  
          ptr->next = NULL;  
          }  
             
       }  
     printf("\nnode inserted\n");  
    }  
void insertion_specified()  
{  
   struct node *ptr,*temp;  
   int item,loc,i;  
   ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\n OVERFLOW");  
   }  
   else  
   {  
       temp=head;  
       printf("Enter the location");  
       scanf("%d",&loc);  
       for(i=0;i<loc;i++)  
       {  
           temp = temp->next;  
           if(temp == NULL)  
           {  
               printf("\n There are less than %d elements", loc);  
               return;  
           }  
       }  
       printf("Enter value");  
       scanf("%d",&item);  
       ptr->data = item;  
       ptr->next = temp->next;  
       ptr -> prev = temp;  
       temp->next = ptr;  
       temp->next->prev=ptr;  
       printf("\nnode inserted\n");  
   }  
}  
void deletion_beginning()  
{  
    struct node *ptr;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == NULL)  
    {  
        head = NULL;   
        free(head);  
        printf("\nnode deleted\n");  
    }  
    else  
    {  
        ptr = head;  
        head = head -> next;  
        head -> prev = NULL;  
        free(ptr);  
        printf("\nnode deleted\n");  
    }  
  
}  
void deletion_last()  
{  
    struct node *ptr;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == NULL)  
    {  
        head = NULL;   
        free(head);   
        printf("\nnode deleted\n");  
    }  
    else   
    {  
        ptr = head;   
        if(ptr->next != NULL)  
        {  
            ptr = ptr -> next;   
        }  
        ptr -> prev -> next = NULL;   
        free(ptr);  
        printf("\nnode deleted\n");  
    }  
}  
void deletion_specified()  
{  
    struct node *ptr, *temp;  
    int val;  
    printf("\n Enter the data after which the node is to be deleted : ");  
    scanf("%d", &val);  
    ptr = head;  
    while(ptr -> data != val)  
    ptr = ptr -> next;  
    if(ptr -> next == NULL)  
    {  
        printf("\nCan't delete\n");  
    }  
    else if(ptr -> next -> next == NULL)  
    {  
        ptr ->next = NULL;  
    }  
    else  
    {   
        temp = ptr -> next;  
        ptr -> next = temp -> next;  
        temp -> next -> prev = ptr;  
        free(temp);  
        printf("\nnode deleted\n");  
    }     
}  
void display()  
{  
    struct node *ptr;  
    printf("\n printing values...\n");  
    ptr = head;  
    while(ptr != NULL)  
    {  
        printf("%d\n",ptr->data);  
        ptr=ptr->next;  
    }  
}   
void search()  
{  
    struct node *ptr;  
    int item,i=0,flag;  
    ptr = head;   
    if(ptr == NULL)  
    {  
        printf("\nEmpty List\n");  
    }  
    else  
    {   
        printf("\nEnter item which you want to search?\n");   
        scanf("%d",&item);  
        while (ptr!=NULL)  
        {  
            if(ptr->data == item)  
            {  
                printf("\nitem found at location %d ",i+1);  
                flag=0;  
                break;  
            }   
            else  
            {  
                flag=1;  
            }  
            i++;  
            ptr = ptr -> next;  
        }  
        if(flag==1)  
        {  
            printf("\nItem not found\n");  
        }  
    }     
          
}  
Output

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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 beginning
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?

Exited.