Double Linked List

Some times it is necessary for an application that the linked list structure is double sided. That means each node will have pointer to the next node in the list and pointer to its preceding node. With this type of arguments the coupling between each and every elements of a list become strong. The linked list ensures consistency and integrity amongst the elements it is always reverse traversal in the list.

Advantages and Disadvantages
Back and forward traversal of a linked list can be possible at a very high speed, specially when we are working with a very huge database. The extra pointer per record also occupies the extra memory in the database.

/* Double Linked List Menu Drive Program */
struct emp
{
            int eno;
            char ename[20];
            float esal;
            struct emp *next, *prev;
};
typedef struct emp node;
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
node *f,*l,*n,*guest,*x, *y;
int temp,found;
float sal;

void main()
{
            void add_node_first();
            void add_node_last();
            void enquiry_node();
            void modify_node();
            void delete_node();
            void list_from_first();
            void list_from_last();
            void number_nodes();

            int ch;
            clrscr();
            f=l=n=NULL;
            do
            {
                        clrscr ();
                        gotoxy(30,1); printf("1. Add Node at First");
                        gotoxy(30,2); printf("2. Add Node at Last");
                        gotoxy(30,3); printf("3. Enquiry on a Node");
                        gotoxy(30,4); printf("4. Modify a Node");
                        gotoxy(30,5); printf("5. Delete a Node");
                        gotoxy(30,6); printf("6. List From First");
                        gotoxy(30,7); printf("7. List From Last");
                        gotoxy(30,8); printf("8. Number of Nodes");
                        gotoxy(30,9); printf("9. Exit");
                        gotoxy(30,11); printf("Enter Your Choice :");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                        case 1:
                                    add_node_first();
                                    getch();
                                    break;
                        case 2:
                                    add_node_last();
                                    getch();
                                    break;
                        case 3:
                                    enquiry_node();
                                    getch();
                                    break;
                        case 4:
                                    modify_node();
                                    getch();
                                    break;
                        case 5:
                                    delete_node();
                                    getch();
                                    break;
                        case 6:


                                    list_from_first();
getch();
                                    break;
                        case 7:
                                    list_from_last();
                                    getch();
                                    break;
                        case 8:
                                    number_nodes();
                                    getch();
                                    break;
                        case 9:
                                    exit(0);
                        default :
                                    printf("\n Invalid Choice");
                                    getch();
                        }
            }while(1);
getch();
}
void add_node_first()
{
            n=(node*)malloc(sizeof(node));
            printf("\n Enter Employee Number :");
            scanf("%d", &n->eno);
            printf("\n Enter Employee Name :");
            fflush(stdin);
            gets(n->ename);
            printf("\n Enter Employee Salary :");
            scanf("%f", &sal);
            n->esal=sal;
            n->next=NULL;
            n->prev=NULL;
            if(f==NULL)
            f=l=n;
            else
            {
                        f->prev=n;
                        n->next=f;
                        f=n;
            }
            printf("\n Node added at first");
}
void add_node_last()
{
            n=(node*)malloc(sizeof(node));
            printf("\n Enter Employee Number :");
            scanf("%d", &n->eno);
            printf("\n Enter Employee Name :");
            fflush(stdin);
            gets(n->ename);
            printf("\n Enter Employee Salary :");
            scanf("%f", &sal);
            n->esal=sal;
            n->next=NULL;
            n->prev=NULL;
            if(f==NULL)
            f=l=n;
            else
            {
                        l->next=n;
                        n->prev=l;
                        l=n;
            }
            printf("\n Node added at last");
}
void enquiry_node()
{
            printf("\n Enter Employee Number to Display :");
scanf("%d", &temp);
            guest=f;
            found=0;
            while((guest!=NULL) && (found==0))
            {
                        if(guest->eno==temp)
                        {
                                    found=1;
                                    printf("\n Employee Name    :%s", guest->ename);
                                    printf("\n Employee Salary  :%.2f", guest->esal);
                        }
                        else
                        guest=guest->next;
            }
            if(found==0)
            printf("\n No such employee to display");
}
void modify_node()
{
            printf("\n Enter Employee Number to Modify :");
            scanf("%d", &temp);
            guest=f;
            found=0;
            while((guest!=NULL) && (found==0))
            {
                        if(guest->eno==temp)
                        {
                                    found=1;
printf("\n Employee Name  :%s", guest->ename);
printf("\n Em[ployee Salary :%.2f", guest->esal);
                                    printf("\n Enter Employee Number :");
                                    scanf("%d",&guest->eno);
                                    printf("\n Enter Employee Name :");
scanf("%d", &temp);
            guest=f;
            found=0;
            while((guest!=NULL) && (found==0))
            {
                        if(guest->eno==temp)
                        {
                                    found=1;
                                    printf("\n Employee Name    :%s", guest->ename);
                                    printf("\n Employee Salary  :%.2f", guest->esal);
                        }
                        else
                        guest=guest->next;
            }
            if(found==0)
            printf("\n No such employee to display");
}
void modify_node()
{
            printf("\n Enter Employee Number to Modify :");
            scanf("%d", &temp);
            guest=f;
            found=0;
            while((guest!=NULL) && (found==0))
            {
                        if(guest->eno==temp)
                        {
                                    found=1;
                                    printf("\n Employee Name  :%s", guest->ename);
                                    printf("\n Em[ployee Salary :%.2f", guest->esal);
                                    printf("\n Enter Employee Number :");
                                    scanf("%d",&guest->eno);
                                    printf("\n Enter Employee Name :");
fflush(stdin);
                                    gets(guest->ename);
                                    printf("\n Enter Employee Salary :");
                                    scanf("%f", &sal);
                                    guest->esal=sal;
                                    printf("\n Node Modified");
                        }
                        else
                        guest=guest->next;
            }
            if(found==0)
            printf("\n No such employee to Modify");
}
void delete_node()
{
            printf("\n Enter Employee Number to Delete :");
            scanf("%d", &temp);
            if(f->eno==temp)
            {
                        f=f->next;
                        f->prev=NULL;
                        printf("\n Node Deleted");
            }
            else if(l->eno==temp)
            {
                        l=l->prev;
                        l->next=NULL;
                        printf("\n Node Deleted");
            }
            else
            {
                        x=f;
                        y=f->next;
                        found=0;
                        while((y!=NULL) && (found==0))
                        {
                                    if(y->eno==temp)
                                    found=1;
                                    else
                                    {
                                                x=x->next;
                                                y=y->next;
                                    }
                        }
                        if(y==NULL)
                        printf("\n No such employee to delete");
                        else
                        {
                                    x->next=y->next;
                                    y->next->prev=x;
                                    printf("\n Node Deleted");
                        }
            }
}
void list_from_first()
{
            guest=f;
            while(guest!=NULL)
            {
            printf("%d\t%s\t%.2f\n", guest->eno, guest->ename, guest->esal);
                        guest=guest->next;
            }

                        {
                                    if(y->eno==temp)
                                    found=1;
                                    else
                                    {
                                                x=x->next;
                                                y=y->next;
                                    }
                        }
                        if(y==NULL)
                        printf("\n No such employee to delete");
                        else
                        {
                                    x->next=y->next;
                                    y->next->prev=x;
                                    printf("\n Node Deleted");
                        }
            }
}
void list_from_first()
{
            guest=f;
            while(guest!=NULL)
            {
            printf("%d\t%s\t%.2f\n", guest->eno, guest->ename, guest->esal);
                        guest=guest->next;
            }
}
void list_from_last()
{
            guest=l;
            while(guest!=NULL)
            {
            printf("%d\t%s\t%.2f\n", guest->eno, guest->ename, guest->esal);
                        guest=guest->prev;
            }
}
void number_nodes()
{
            int n=0;
            guest=f;
            while(guest!=NULL)
            {
                        n++;
                        guest=guest->next;
            }
            printf("\n Number of Nodes :%d", n);
}

No comments:

Post a Comment