Advertisement
d1i2p3a4k5

15.Doubly linked list (DS)

Nov 1st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. typedef struct node
  5. {
  6.     int data;
  7.     struct node *next,*prev;
  8. }node;
  9. typedef struct
  10. {
  11.      node *start,*last;
  12. }head;
  13. void insertend(head *t,int ele)
  14. {
  15.     node *p;
  16.     p = (node*)malloc(sizeof(node));
  17.     p->data=ele;
  18.     p->next=p->prev=NULL;
  19.     if(t->start==NULL)
  20.     {
  21.         t->last=t->start=p;
  22.         return;
  23.     }
  24.     t->last->next = p;
  25.     p->prev = t->last;
  26.     t->last = p;
  27.     return ;
  28. }
  29. void displayrev(head *t)
  30. {
  31.     node *q;
  32.     if(t->start==NULL&&t->last==NULL)
  33.     {
  34.         printf("\nLinked list is empty");
  35.         return;
  36.     }
  37.     printf("\nElement of Linked list is given below\n");
  38.     for(q=t->last;q!=NULL;q=q->prev)
  39.     {
  40.         printf("%d\t",q->data);
  41.     }
  42.     return;
  43. }
  44. void deletebeg(head *t)
  45. {
  46.     int z;
  47.     node *p;
  48.     if(t->start==NULL)
  49.     {
  50.         printf("Linked list is Empty");
  51.         return;
  52.     }
  53.     p=t->start;
  54.     z = p->data;
  55.     if(t->start==t->last)
  56.     {
  57.         t->start=t->last=NULL;
  58.     }
  59.     else
  60.     {
  61.         t->start = t->start->next;
  62.         t->start->prev = NULL;
  63.     }
  64.     printf("deleted element in Linked list %d",z);
  65.     return;
  66. }
  67. void display(head *t)
  68. {
  69.     node *q;
  70.     if(t->start==NULL&&t->last==NULL)
  71.     {
  72.         printf("\nLinked list is empty");
  73.         return;
  74.     }
  75.     printf("\nElement of Linked list is given below\n");
  76.     for(q=t->start;q!=NULL;q=q->next)
  77.     {
  78.         printf("%d\t",q->data);
  79.     }
  80.     return;
  81. }
  82. int main()
  83. {
  84.     head x;
  85.     int ch,ele;
  86.     x.start=x.last=NULL;
  87.     printf("\nImplementation of Doubly Linked list with function insertend,display,deletebeg,reversedisplay\n");
  88.     while(1)
  89.     {
  90.         printf("\nenter choice \n1.InsertEnd 2.Deletebeg 3.Reversedisplay 4.display 5.Exit\n");
  91.         scanf_s("%d",&ch);
  92.         if(ch==5)
  93.         {
  94.             break;
  95.         }
  96.         else
  97.         {
  98.             switch(ch)
  99.             {
  100.             case 1:printf("\nenter element to insert");
  101.                 scanf_s("%d",&ele);
  102.                 insertend(&x,ele);
  103.                 break;
  104.             case 2:deletebeg(&x);
  105.                 break;
  106.             case 3:displayrev(&x);
  107.                 break;
  108.             case 4:display(&x);
  109.                 break;
  110.             default:printf("\ninvalid input");
  111.             }
  112.         }
  113.     }
  114.     _getch();
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement