affiliate marketing

Monday 12 December 2011

DOUBLY LINKED LIST – LINKED LIST IMPLEMENTATION


DOUBLY LINKED LIST – LINKED LIST IMPLEMENTATION

      Aim:
         To write a program to implement doubly linked list using linked list.
      Algorithm:
         Step 1: Declare header and pointer variables
         Step 2: Display the choices
         Step 3: If choice is 1 the get the element to be inserted in beginning and call ins_beg function.
         Step 4: If choice is 2 the get the element to be inserted in the end and call the ins_end function
         Step 5: If choice is 3 then get the element to be deleted and call deletion function.
         Step 6: If choice is 4 then call display duncation
         Step 7: If choice is default the exit the program
         Step 8: Terminate the program execution.
      Program:

       #include<stdio.h>
       #include<conio.h>
       #include<stdlib.h>
        void display(struct node *first);
       struct node
{
         int data;
         struct node *lptr,*rptr;
        }*head;
            struct node *ins_beg(int x,struct node *first)
{
         struct node *new1,*cur,*prev;
         new1=malloc(sizeof(struct node));
         if(first==NULL)
         {
                     new1->data=x;
                     new1->lptr=NULL;
                     new1->rptr=NULL;
                     return new1;
         }
         else
         {
                     new1->data=x;
                     new1->lptr=NULL;
                     new1->rptr=first;
                     return new1;
         }
}
         struct node *ins_end(int x,struct node *first)
{
         struct node *new1,*cur,*prev;
         new1=malloc(sizeof(struct node));
         if(first==NULL)
         {
                     new1->data=x;
                     new1->lptr=NULL;
                     new1->rptr=NULL;
                     return new1;
         }
         else
         {
                     cur=first;
                     while(cur->rptr!=NULL)
                     {
                                 prev=cur;
                                 cur=cur->rptr;
                     }
                     cur->rptr=new1;
                     new1->data=x;
                     new1->lptr=cur;
                     new1->rptr=NULL;
                     return first;
         }
}
       struct node *deletion(struct node *first,int del)
{
         struct node *prev,*cur;
         cur=first;
         if(first==NULL)
         {
                     printf("\n\nNo data present!!!");
                     getch();
         }
         else if(first->data==del)
         {
                     printf("\n\nData %d is deleted",first->data);
                     first=first->rptr;
                     getch();
                     return first;
         }
         else
         {
                     while(cur->rptr!=NULL && cur->data!=del)
                     {
                                 prev=cur;
                                 cur=cur->rptr;
                     }
                     if(cur->rptr==NULL && cur->data!=del)
                                 printf("\n\nData is not present!!!");
                     else if(cur->rptr!=NULL && cur->data==del)
                     {
                                 prev->rptr=cur->rptr;
                                 (cur->rptr)->lptr=prev;
                                 printf("\n\nData % d is deleted",cur->data);
                     }
                     else if(cur->rptr==NULL && cur->data==del)
                     {
                                 prev->rptr=NULL;
                                 printf("\n\nData %d is deleted:",cur->data);
                     }
                     getch();
                     return first;
         }
}
      void main()
{
         int x,ch,del;
         head=NULL;
         clrscr();
         printf("\n1.Insert in Begining\n2.Insert in the End\n3.Delete\n4.Display");
         while(1)
         {
                     printf("\n\nEnter your Choice :: ");
                     scanf("%d",&ch);
                     switch(ch)
                     {
                                 case 1:
                                             printf("\n\nEnter the element to be inserted::");
                                             scanf("%d",&x);
                                             head=ins_beg(x,head);
                                             break;
                                 case 2:
                                             printf("\n\nEnter the element to be inserted::");
                                             scanf("%d",&x);
                                             head=ins_end(x,head);
                                             break;
                                 case 3:
                                             printf("\n\nEnter the element to be deleted::");
                                             scanf("%d",&del);
                                             head=deletion(head,del);
                                             break;
                                 case 4:
                                             display(head);
                                             break;
                                 default:
                                             printf("\n\nInvalid Choice......");
                                             getch();
                                             exit(0);
                     }
         }
}
      void display(struct node *first)
{
         struct node *temp;
         temp=first;
         if(temp==NULL)
                     printf("\n\nList is empty!!!");
         while(temp!=NULL)
         {
                     printf("%d ->",temp->data);
                     temp=temp->rptr;
         }
         getch();
}

      Output:
      1.Insert in Begining
      2.Insert in the End
      3.Delete
      4.Display

Enter your Choice :: 1


Enter the element to be inserted::2


Enter your Choice :: 1


Enter the element to be inserted::3


Enter your Choice :: 4
3 ->2 ->

Enter your Choice :: 2


Enter the element to be inserted::1


Enter your Choice :: 2


Enter the element to be inserted::5


Enter your Choice :: 4
3 ->2 ->1 ->5 ->

Enter your Choice :: 3
Enter the element to be deleted::1
Data  1 is deleted
Enter your Choice :: 4
3 ->2 ->5 ->


No comments:

Post a Comment