Guest User

Untitled

a guest
Nov 14th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 5
  4. struct stack {
  5. int st[SIZE];
  6. int top;
  7. };
  8. void push(struct stack *, int);
  9. int pop(struct stack *);
  10. void display(struct stack *);
  11. void tftop(struct stack *);
  12. void tfbotm(struct stack *);
  13. void npop(struct stack *);
  14.  
  15. int main()
  16. {
  17. struct stack s;
  18. s.top=-1;
  19. int ch,b,x;
  20. while(1)
  21. {
  22. printf("Enter the choice.\n");
  23. printf("1:PUSH\n2:POP\n3:DISPLAY\n4:THIRD FROM TOP\n5:THIRD FROM BOTTOM\n6:POP 'N' ELEMENTS\n7:EXIT\n");
  24. scanf("%d",&ch);
  25. printf("\n");
  26. switch(ch)
  27. { case 1: printf("Enter the element to push\n");
  28. scanf("%d",&b);
  29. push(&s,b);
  30. // printf("PUSHED\n");
  31. break;
  32. case 2: x=pop(&s);
  33. // printf("POPED\n");
  34. break;
  35. case 3: display(&s);
  36. break;
  37. case 4: tftop(&s);
  38. break;
  39. case 5: tfbotm(&s);
  40. break;
  41. case 6: npop(&s);
  42. break;
  43.  
  44. case 7: exit (0);
  45.  
  46. }
  47. } return 0;
  48. }
  49. void push(struct stack *s,int b)
  50. {
  51. if (s->top==SIZE-1)
  52. {printf("OVERFLOW\n");
  53. return;
  54. }else
  55. {s->top+=1;
  56. s->st[s->top]=b; // or ++s->top
  57.  
  58. }
  59. }
  60. int pop(struct stack *s){
  61. int a;
  62. if(s->top==-1){
  63. printf("UNDERFLOW\n");
  64. return(-1);
  65. }else{
  66. a=s->st[s->top];
  67. s->top-=1;
  68. return a; // or return (s->st[s->top--]);
  69.  
  70. }
  71. }
  72. void display(struct stack *s)
  73. { if(s->top==-1){
  74. printf("Stack is empty\n");
  75. }else{
  76. int i;
  77. for (i=s->top;i>=0;i--)
  78. printf("%d\n",s->st[i]);
  79. }
  80. }
  81. void tftop(struct stack *s)
  82. {
  83. int i,y;
  84. struct stack temp;
  85. temp.top=-1;
  86. if (s->top<2){
  87. printf("No sufficient elements in stack\n");
  88. }else{
  89.  
  90. for (i=0;i<2;i++)
  91. {
  92. y=pop(s);
  93. push(&temp,y);
  94. }
  95. y=pop(s);
  96. printf("The third element from top is %d.\n",y);
  97. push(s,y);
  98. for(i=0;i<2;i++){
  99. y=pop(&temp);
  100. push(s,y);
  101. }
  102. }
  103. }
  104. void tfbotm(struct stack *s)
  105. {
  106. int i,y;
  107. struct stack temp;
  108. temp.top=-1;
  109. if (s->top<2){
  110. printf("No sufficient elements in stack.\n");
  111.  
  112. }else{
  113. for(i=s->top;i>2;i--)
  114. {
  115. y=pop(s);
  116. push(&temp,y);
  117. }
  118. y=pop(s);
  119. printf("The third element from bottom is %d.\n",y);
  120. push(s,y);
  121. for (i=temp.top;i>=0;i--){
  122. y=pop(&temp);
  123. push(s,y);
  124. }
  125. }
  126. }
  127. void npop(struct stack *s)
  128. {
  129. int n,i,y;
  130. // struct stack temp;
  131. // temp.top=-1;
  132. printf("Enter the no of elements to be poped.\n");
  133. scanf("%d",&n);
  134. if (s->top<n-1){
  135. printf("Not sufficient elements.\n");
  136. }else{
  137. printf("Poped elements are:\n");
  138. for(i=0;i<=n-1;i++){
  139. y=pop(s);
  140. printf("%d\n",y);
  141. }
  142. }
  143. }
Add Comment
Please, Sign In to add comment