Doubly linked list in data structure and algorithm

        The linked list concept is commonly used in the real world. The concept of singly linked lists we learned comes into play when we use Spotify to play the next song in the queue. But what exactly can you do to play the previous song in the queue? 

        In this blog, we will learn about another concept related to data structures, namely doubly linked list. We will also discuss implementation using C language and real-time applications.

What is a doubly linked list?

        A linked list is a linear data structure that contains nodes connected in a sequential fashion. The node contains three fields, namely the data stored at the reference address and two pointers to the successor nodes to the left and right of the reference node. 

        The left node pointer stores the memory address of the previous node in the sequence, and the right node stores the memory address of the next node. We use dynamic memory allocation here instead of arrays so that the memory size can be allocated or deallocated at runtime depending on the operations performed. 

 

        In this example, the head points to the first node. The reference node's left pointer stores NULL, as does the last node's right pointer.

To operate on this example, we can go further and make corresponding changes. 

Implementation of doubly linked list

1. Insert the node in front

        To accomplish the above operations, first create a node and allocate memory using dynamic memory. Point the head to the new node and store NULL values ​​in the left and right nodes.

void front_add(){
      //allocate memory using dynamic memory allocation.
       newnode -> data = NULL;
       newnode -> prev = NULL;
       newnode -> next = head;
       head= newnode;
}

2. Delete the front node

To remove a node from the front, we have to store the correct node value for the reference address in the head and free the first node. 

void front_del(){
         newnode=head;
         head= head->next ;
         head->prev = NULL;
         free(newnode);
}

3. Insert node at the end

To add a node at the end, we have to traverse to the end and point the last node to the new node referenced and vice versa.

void end_add(){
     //allocate memory to newnode
     newnode -> data= item; // temp=head
     while(temp ->next !=NULL)
     {
         temp = temp->next;
     }
     temp->next= newnode;
     newnode -> prev = temp;
     newnode-> next = NULL;

}

4. Delete the end node

To remove a node at the end, we have to traverse the linked list and reach the end. We'll use a pointer to the last second node. Then release the last node.

void rear_del(){
    
     while(temp -> next!=NULL)
     {
         temp = temp->next;          //temp=head
     }
     temp ->prev-> next = NULL;
     free(temp);
}

Now that we have understood the basic operations, we will now implement a doubly linked list in C step by step.

#include<stdio.h>
#define MAX 5

struct node{
    int data;
    struct node * prev;
    struct node * next;
};
struct node *head;
void front_add();
void front_del();
void rear_add();
void rear_del();
void display();

int main(){

    int choice=0;
    while(choice!=6){
    printf("enter choice:\n");
    printf("\n1.front_add\n2.front_Del\n3.rear_add\n4.rear_del\n5.display\n6.exit");
    scanf("%d\n",&choice);

    switch(choice){
        case 1:
           front_add();
           break;
        case 2:
           front_del();
           break;
        case 3:
           rear_add();
           break;
        case 4:
           rear_del();
           break;
        case 5:
           display();
           break;
        case 6:
           printf("exiting...\n");
           break;
        default:
           printf("unknown choice\n");
    }
  }
}
void front_add(){
     struct node* newnode;
     int item;
     newnode = (struct node*)malloc(sizeof(struct node));

     printf("enter item value:\n");
     scanf("%d", &item);
     if(head == NULL)
     {
       newnode -> next = NULL;
       newnode -> prev = NULL;
       newnode -> data = item;
       head = newnode;
     }
     else
     {
       newnode -> data = item;
       newnode -> prev = NULL;
       newnode -> next = head;
       head->prev = newnode;
       head= newnode;
       }
}
void front_del(){
     struct node *newnode;
     if(head->next == NULL)
       {
        head = NULL;
        free(head);
        printf("\nnode deleted\n");
       }
       else
        {
         newnode=head;
         head= head->next ;
         head->prev = NULL;
         free(newnode);
         printf("deleted\n");
       }
}
void rear_add(){
     struct node *temp,*newnode;
     int item;
     newnode = (struct node*)malloc(sizeof(struct node));
     printf("enter item");
     scanf("%d", &item);
     newnode -> data= item;
     temp = head;
     while(temp ->next !=NULL)
     {
         temp = temp->next;
     }
     temp->next= newnode;
     newnode -> prev = temp;
     newnode-> next = NULL;
     printf("inserted\n");

}
void rear_del(){
     struct node *temp;
     temp=head;
     if(head->next==NULL)
     {
         head = NULL;
         free(head);
         printf("deleted\n");
     }
    else{
     while(temp -> next!=NULL)
     {
         temp = temp->next;
     }
     temp ->prev-> next = NULL;
     free(temp);
     printf("deleted\n");

}
}
void display(){
     struct node *temp;
     temp = head;
     if(head==NULL)
     {
         printf("empty\n");
     }
     else{
     while(temp!=NULL)
     {
         printf("%d", temp->data);
         temp = temp->next;
     }
     }
}

This code will give you the desired output:

 

 

        Have you ever wondered how these multiplayer games are developed where players get chances in a repeating loop? This means that the last player is linked to the first player again to form a loop.

To make this possible, we introduce another concept related to linked lists. In this case, a circular linked list is useful.

circular linked list

        In a circular singly linked list, the last node of the linked list contains a pointer to the first node of the linked list. Both doubly linked and singly linked lists can use this concept.

        The only difference between this list and the other two is that the right pointer of the last node points to the first node, while the head node always points to the first node itself.

in conclusion

In my opinion, the concept of linked list is very important and useful in solving complex problems. Both doubly linked lists and circular singly linked lists go hand in hand when going through various scenarios. I hope you enjoy reading this blog. Please like and comment your thoughts on today's topic. Happy learning!

 

Guess you like

Origin blog.csdn.net/qq_28245905/article/details/132115737