Advertisement
asiffff

Stack Using Doubly

Feb 20th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int id;
  6. struct node *next,*prev;
  7. }*start=NULL,*end=NULL,*current;
  8. int co=0,i;
  9. void cre()
  10. {
  11. struct node *n;
  12. if(start==NULL)
  13. {
  14. printf("\nEnter Value:");
  15. n=(struct node*)malloc(1*sizeof(struct node));
  16. n->next=NULL;
  17. n->prev=NULL;
  18. scanf("%d",&n->id);
  19. start=n;
  20. end=n;
  21. current=n;
  22. co++;
  23. }
  24. else
  25. {
  26. printf("\nEnter Value:");
  27. n=(struct node*)malloc(1*sizeof(struct node));
  28. n->next=NULL;
  29. n->prev=NULL;
  30. scanf("%d",&n->id);
  31. current->next=n;
  32. n->prev=current;
  33. end=n;
  34. current=n;
  35. co++;
  36. }
  37. }
  38. void del()
  39. {
  40. if(co==1)
  41. {
  42. start=NULL;
  43. end=NULL;
  44. co=0;
  45. }
  46. if(start==NULL && end==NULL)
  47. {
  48. printf("\nStack is Empty.\n");
  49. }
  50. else
  51. {
  52. struct node *c;
  53. c=end;
  54. c=c->prev;
  55. end=c;
  56. co--;
  57. }
  58. }
  59. void dis()
  60. {
  61. if(start==NULL && end==NULL)
  62. {
  63. printf("\nStack is Empty.\n");
  64. }
  65. else
  66. {
  67. struct node *c;
  68. c=end;
  69. while(c!=NULL)
  70. {
  71. printf("%d<--",c->id);
  72. c=c->prev;
  73. }
  74. printf("\n Front is %d\n",end->id);
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. int x;
  81. printf("Enter the value of Stack\n");
  82. scanf("%d",&i);
  83. while(1)
  84. {
  85. printf("1.Ins, 2.Del,3.Dis\n");
  86. scanf("%d",&x);
  87. switch(x)
  88. {
  89. case 1:
  90. {
  91. if(co==i)
  92. {
  93. printf("Stack is Full.\n");
  94. break;
  95. }
  96. else
  97. {
  98. cre();
  99. break;
  100. }
  101. }
  102. case 2:
  103. {
  104. del();
  105. break;
  106. }
  107. case 3:
  108. {
  109. dis();
  110. break;
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement