Sunday, October 2, 2011

Data Structure C program Doubly Linked List Dev c++


Data Structures in C Program Doubly Linked List


 #include <stdio.h>  
 #include <stdlib.h>  
 struct dllist {  
  int data;  
  struct dllist *next;  
  struct dllist *prev;  
 };  
 typedef struct dllist* node;  
 void inserts(node);  
 void inserte(node);  
 void insertm(node,node);  
 void removen(node);  
 node find(int);  
 void dispaly(int);  
 node start=NULL;  
 node end=NULL;  
 void inserts(node temp)  
 {  
    if(start==NULL)  
    {  
    temp->next=NULL;  
    temp->prev=NULL;  
    start=temp;  
    end=temp;  
    }  
    else  
    {  
     temp->prev=NULL;  
     temp->next=start;  
     start->prev=temp;  
     start=temp;  
     }  
 }  
 void inserte(node temp)  
 {  
    if(end==NULL)  
    {  
    inserts(temp);  
    }  
    else  
    {  
      temp->prev=end;  
      end->next=temp;  
      temp->next=NULL;  
      end=temp;  
    }  
 }  
  void insertm(node temp,node after)  
  {  
    temp->prev=after;  
    temp->next=after->next;  
    if(after->next==NULL)  
    {  
     end=temp;  
    }  
    else  
    {  
      (after->next)->prev=temp;  
    }  
    after->next=temp;  
  }  
 void removen(node x)  
 {  
   if(x->prev==NULL && x->next==NULL)  
   {  
    start=NULL;  
    end=NULL;  
   }  
   else if(x->prev==NULL)  
   {  
    (x->next)->prev=NULL;  
    start=x->next;  
    free(x);  
   }else if(x->next==NULL)  
   {  
     (x->prev)->next=NULL;  
     end=x->prev;  
     free(x);  
   }else  
   {  
      x->prev->next=x->next;  
      x->next->prev=x->prev;  
      free(x);  
   }  
 }  
 void display(int x)  
 {  
    node temp=(node)malloc(sizeof(struct dllist));  
    if(start==NULL)  
    printf("Empty list\n");  
    else  
    {  
      printf("List is\n");  
    if(x==1)  
    {  
    temp=start;  
    while(temp!=NULL)  
    {  
     printf("%d\n",temp->data);  
     temp=temp->next;  
    }  
    }  
    else  
    {  
    temp=end;  
    while(temp!=NULL)  
    {  
     printf("%d\n",temp->data);  
     temp=temp->prev;  
    }  
    }  
   }  
 }  
 node find(int x)  
 {  
    node temp=start;  
    while(temp!=NULL)  
    {  
    if(temp->data==x)  
    return temp;  
    temp=temp->next;  
    }  
    printf("NOT FOUND DATA !!!PROGRAM WILL EXIT\n");  
    exit(0);  
 }  
 int main()  
 {  
   printf("1.Insert at Start\n2.Insert at End\n3.Insert after some node\n4.Delete\n5.Display from start\n6.Display from end\n7.Exit\n");  
   int ch,x;  
   node temp,temp2;  
   scanf("%d",&ch);  
   while(ch!=7)  
   {  
   switch(ch)  
   {  
    case 1:temp=(node)malloc(sizeof(struct dllist));  
        printf("Enter data:");  
        scanf("%d",&temp->data);  
        inserts(temp);  
        break;  
    case 2:temp=(node)malloc(sizeof(struct dllist));  
        printf("Enter data:");  
        scanf("%d",&temp->data);  
        inserte(temp);  
        break;  
    case 3:temp=(node)malloc(sizeof(struct dllist));  
        printf("Enter data:");  
        scanf("%d",&temp->data);  
        printf("Enter data after which you want to insert:");  
        scanf("%d",&x);  
        temp2=find(x);  
        insertm(temp,temp2);  
        break;  
    case 4:  
        printf("Enter data");  
        scanf("%d",&x);  
        temp=find(x);  
        removen(temp);  
        break;  
    case 5:display(1);  
        break;  
    case 6:display(60);  
        break;  
    default:printf("WRONG CHOICE ENTER AGAIN\n");  
    }  
    printf("1.Insert at Start\n2.Insert at End\n3.Insert after some node\n4.Delete\n5.Display from start\n6.Display from end\n7.Exit\n");  
   scanf("%d",&ch);  
   }  
 }  

No comments:

Post a Comment