Header Ads

C program for Doubly Linked List

In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.
The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified.


****************Implementation*******************************

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
    struct node *prev;
    int data;
    struct node *next;
};
struct node *head;
void create();
void display();
void insert_at_any_position();
void print_reverse();
void reverse_list();
main()
{
    int choice;
    do
    {
        printf("\n\t1.Create Double List");
        printf("\n\t2.Display lists");
        printf("\n\t3.Insert at any position");
        printf("\n\t4.Print reverse");
        printf("\n\t5.Reverse List");
        printf("\n\t6.Exit\t");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            create();
            break;
        case 2:
            display();
            break;
        case 3:
            insert_at_any_position();
            break;
        case 6:
            exit (0);
            break;
        case 4:
            print_reverse();
            break;
        case 5:
            reverse_list();
            break;
        default:
            printf("\n\t\twrong choice.......");
        }


    }while(choice!=3);
    getch();
}
create()
{
    system("cls");
    struct node *temp,*new_node;
    new_node=(struct node *)malloc(sizeof(struct node));
    printf("\n\tEnter data in form of integer\t");
    scanf("%d",&new_node->data);
    new_node->next=NULL;
    if(head==NULL)
    {
        head=new_node;
        head->prev=NULL;
    }
    else{
        temp=head;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=new_node;
        new_node->prev=temp;

    }
    return 0;
}
display()
{
    system("cls");
    struct node *temp;
    int choice2;
    printf("\n\t1.From first");
    printf("\n\t2.From last\t");
    scanf("%d",&choice2);

    if(choice2==1)
    {
        temp=head;
        while(temp!=NULL)
        {
            printf("%d ==> ",temp->data);
            temp=temp->next;
        }
    }
    else if(choice2==2)
    {
        temp=head;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        while(temp!=NULL)
        {
            printf(" ==> %d",temp->data);
            temp=temp->prev;
        }
    }
    return 0;


}
void insert_at_any_position()
{
    system("cls");
    struct node *temp,*ptr;
    ptr=(struct node *)malloc(sizeof(struct node));
    int pos,i=1;
    printf("Insert position number\t");
    scanf("%d",&pos);
    temp=head;
    while(i<pos-1)
    {
        temp=temp->next;
    }
    ptr->prev=temp;
    temp->next->prev=ptr;
    ptr->next=temp->next;
    temp->next=ptr;

    printf("\n\tEnter data\t");
    scanf("%d",&ptr->data);
    return 1;
}
print_reverse()
{
    struct node *temp;
    temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    while(temp->prev!=NULL)
    {
        printf(" %d ==> ",temp->data);
        temp=temp->prev;
    }
    printf(" %d",temp->data);
}
reverse_list()
{
    system("cls");
    struct node *temp,*p;
    while(head!=NULL)
    {
        temp=head;
        head=head->next;
        p=temp->next;
        temp->next=temp->prev;
        temp->prev=p;

    }
    head=temp;

}

1 comment: