Sunday, 16 October 2011

implementation of doubly linked list


//DOUBLY LINKED LIST OPERATIONS
#include<stdio.h>
#include<stdlib.h>
struct node
{
        int data;
        struct node *next;
        struct node *prev;
}*new,*ptr,*head;
void insert_first();                                        //FUNCTION PROTOTYPES
void disp();
void insert_last();
void insert_middle();
void del_first();
void del_last();
void del_middle();
void search();

int main()
{
        int a,b,q;                                                              //VARIABLE DECLARATION
        system("clear");
        printf("Enter a value\t:\t");
        scanf("%d",&a);                         
        ptr=NULL;        
        while(a!=0)         
        { 
        new=(struct node *)malloc(sizeof(struct node));//DYNAMIC MEMORY ALLOCATION
        new->data=a;
        if(ptr==NULL)
        {
              new->prev=NULL;
            ptr=new;
             head=ptr;
        }
        else
        {
             ptr->next=new;
             new->prev=ptr;
            ptr=new;
        }
             printf("Enter the data\t:\t");
            scanf("%d",&a);
        }
             new->next=NULL;
            ptr=head;
        while(ptr!=NULL)                                                                      //PRINTING THE LIST
        {
            printf("%d\n",ptr->data);
            ptr=ptr->next;
        }
            do
        {
             printf("\nENTER THE OPTION\n");
             printf("\n1.INSERTION AT FIRST POSITION");                                                                                                              /*IMPLEMENTING MENU DRIVEN MANNER*
             printf("\n2.INSERTION AT LAST POSITION");
            printf("\n3.INSERTION AT ANY POSITION");
            printf("\n4.DELETING THE FIRST ELEMENT");
            printf("\n5.DELETING THE LAST ELEMENT");
            printf("\n6.DELETING THE ELEMENT AT ANY POSITION");
            printf("\n7.SEARCH AN ELEMENT\n");
            scanf("%d",&b);
                switch(b)
                {
                         case 1: insert_first();                              /*FUNCTION CALL*/
                        break;
                case 2:
                         insert_last();
                        break;
                case 3:
                         insert_middle();
                        break;
                case 4:
                         del_first();
                        break;
                case 5:
                         del_last();
                        break;
                case 6:
                         del_middle();
                        break;
                case 7:
                        search();
                        break;
                default: printf("\nenter the correct option");
                        break;
                }

    printf("\ndo you want to continue???\n");
    printf("press 1 to continue...\n");
    printf("press 2 to quit...\n");
    scanf("%d",&q);
    }while(q==1);

    return 0;
}
            void insert_first()                          /*INSERTION AT 1ST NODE*/
                {
                int a;
                struct node *ins;
                printf("Enter the number to be inserted...");
                scanf("%d",&a);
                ins=(struct node*)malloc(sizeof(struct node));
                ins->data=a;
                ins->next=head;
                        ins->prev=NULL;
                head=ins;
                disp();
                }
             void disp()                                   /*DISPLAY FUNCTION*/
                {
                        ptr=head;
                        while(ptr!=NULL)
                        {
                        printf("%d\n",ptr->data);
                        ptr=ptr->next;
                        }
                }
             void insert_last()                  /*INSERTION AT THE LAST  NODE*/
                {
                int b;
                struct node *ins1;
                printf("Enter the number to be inserted...");
                scanf("%d",&b);
                ins1=(struct node*)malloc(sizeof(struct node));
                ins1->data=b;
                ins1->next=NULL;
                ptr=head;

                while(ptr->next!=NULL)
                        {
                        ptr=ptr->next;
                        }
                        ptr->next=ins1;
                ins1->prev=ptr;
                disp();
                }
            void insert_middle()           /*INSERTION AT ANY NODE*/
                {
                int z,y;
                struct node *ins2;
                printf("\nEnter the number to be inserted...");
                scanf("%d",&y);
                printf("\nEnter the number in the list after which the number is to be inserted...");
                scanf("%d",&z);
                ins2=(struct node*)malloc(sizeof(struct node));
                ins2->data=y;
                ins2->next=NULL;
                ptr=head;
                        while(ptr->data!=z)
                        {
                        ptr=ptr->next;
                        }
                ins2->next=ptr->next;
                        (ptr->next)->prev=ins2;
                        ins2->prev=ptr;
                ptr->next=ins2;
                disp();
                }

            void del_first()               /*DELETION OF 1ST NODE*/
                {
                ptr=head;
                ptr=ptr->next;
                head=ptr;
                printf("\nelements after the deletion of the first term\n");
                disp();
                }

             void del_last()             /*DELETION OF LAST NODE*/
                  {
                ptr=head;
                        while((ptr->next)->next!=NULL)
                        {
                        ptr=ptr->next;
                        }
                ptr->next=NULL;
                printf("\nlist after the deletion of the last element\n");
                disp();
                }

             void del_middle()           /*DELETION OF ANY NODE*/
                {
                int t;
                printf("Enter the element in the list which is to be deleted...");
                scanf("%d",&t);
                ptr=head;
                        while((ptr->next)->data!=t)
                        {
                        ptr=ptr->next;
                        }
                ptr->next=(ptr->next)->next;
                        ptr=(ptr->next)->prev;
                printf("\nElements in the list after deletion\n");
                disp();
                }

             void search()                                                                           /*SEARCHING*/
                {
                int l,v=0;
                printf("Enter the element to be searched\n");
                scanf("%d",&l);
                ptr=head;
                        while(ptr->next!=NULL)
                        {
                                if(ptr->data==l)
                                {
                                v++;
                                break;
                                }
                        ptr=ptr->next;
                        }
                        if(v!=0)
                        {
                        printf("\nThe element is found");
                        }
                        else
                        {
                        printf("\nThe element is not found");
                        }
                }








OUTPUT:
Enter a value   :       2
Enter the data  :       3
Enter the data  :       4
Enter the data  :       2
Enter the data  :       1
Enter the data  :       5
Enter the data  :       6
Enter the data  :       7
Enter the data  :       8
Enter the data  :       0
2
3
4
2
1
5
6
7
8


ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
1
Enter the number to be inserted...5
5
2
3
4
2
1
5
6
7
8




do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
2
Enter the number to be inserted...5
5
2
3
4
2
1
5
6
7
8
5
do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
3
Enter the number to be inserted...5
Enter the number in the list after which the number is to be inserted...6
5
2
3
4
2
1
5
6
5
7
8
5
do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
4
elements after the deletion of the first term
2
3
4
2
1
5
6
5
7
8
5
do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
5
list after the deletion of the last element
2
3
4
2
1
5
6
5
7
8
do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
6
Enter the element in the list which is to be deleted...7
Elements in the list after deletion
2
3
4
2
1
5
6
5
8
do you want to continue???
press 1 to continue...
press 2 to quit...
1
ENTER THE OPTION
1.INSERTION AT FIRST POSITION
2.INSERTION AT LAST POSITION
3.INSERTION AT ANY POSITION
4.DELETING THE FIRST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
7
Enter the element to be searched
2
The element is found
do you want to continue???
press 1 to continue...
press 2 to quit...

No comments:

Post a Comment