Guest User

Untitled

a guest
Nov 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int info;
  6. struct node *llink,*rlink;
  7. };
  8. typedef struct node *NODE;
  9. NODE insert_front(NODE first,int item);
  10. NODE delete_rear(NODE first);
  11. void display(NODE first);
  12. NODE getnode();
  13. int main()
  14. { int ch,item;
  15. NODE first=NULL;
  16. while(1)
  17. {
  18. printf("Enter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit");
  19. scanf("%d",&ch);
  20. switch(ch)
  21. {
  22. case 1:printf("Enter the element to be inserted");
  23. scanf("%d",&item);
  24. first=insert_front(first,item);
  25. break;
  26. case 2:first=delete_rear(first);
  27. break;
  28. case 3:display(first);
  29. break;
  30. case 4:exit(0);
  31. }
  32. }
  33. return 0;
  34. }
  35. NODE getnode()
  36. {
  37. NODE x;
  38. x=(NODE)malloc(sizeof(struct node));
  39. return x;
  40. }
  41. NODE insert_front(NODE first,int item)
  42. {
  43. NODE temp;
  44. temp=getnode();
  45. temp->info=item;
  46. if(first==NULL)
  47. {
  48. temp->llink=temp;
  49. temp->rlink=temp;
  50. return temp;
  51. }
  52. if(first->rlink==first&&first->llink==first)
  53. {
  54. temp->rlink=first;
  55. temp->llink=first;
  56. first->rlink=temp;
  57. first->llink=temp;
  58. return temp;
  59. }
  60.  
  61. NODE t;
  62. t=first->llink;
  63. first->llink=temp;
  64. temp->rlink=first;
  65. temp->llink=t;
  66. t->rlink=temp;
  67. return temp;
  68. }
  69. NODE delete_rear(NODE first)
  70. {
  71. if(first==NULL)
  72. {
  73. printf("List is empty\n");
  74. return first;
  75. }
  76. if(first->rlink==first&&first->llink==first)
  77. {
  78. free(first);
  79. return NULL;
  80. }
  81. NODE t;
  82. t=first;
  83. while(t->rlink!=first)
  84. {
  85. t=t->rlink;
  86. }
  87. NODE p;
  88. p=t->llink;
  89. printf("Deleted data is %d",t->info);
  90. p->rlink=first;
  91. t->llink=NULL;
  92. t->rlink=NULL;
  93. first->llink=p;
  94. free(t);
  95. return first;
  96. }
  97. void display(NODE first)
  98. {
  99. if(first==NULL)
  100. {
  101. printf("List is empty\n");
  102. return;
  103. }
  104. NODE t;
  105. t=first;
  106. while(t->rlink!=first)
  107. {
  108. printf("%d\t",t->info);
  109. t=t->rlink;
  110. }
  111. printf("%d",t->info);
  112. return;
  113. }
Add Comment
Please, Sign In to add comment