Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #define max 50
  4.  
  5. void push();
  6.  
  7. void pop();
  8.  
  9. void display();
  10.  
  11. int menu();
  12.  
  13. int stack[max],top=0;
  14.  
  15. int main()
  16.  
  17. {
  18.  
  19. intch;
  20.  
  21. do
  22.  
  23. {
  24.  
  25. {
  26.  
  27. ch=menu();
  28.  
  29. switch(ch)
  30.  
  31. case 1: push();
  32.  
  33. break;
  34.  
  35. case 2: pop();
  36.  
  37. break;
  38.  
  39. case 3: display();
  40.  
  41. break;
  42.  
  43. case 4: printf("----Stack exited----");
  44.  
  45. break;
  46.  
  47. default: printf("Enter a valid choice!");
  48.  
  49. }
  50.  
  51. }
  52.  
  53. while(ch!=4);
  54.  
  55. return 0;
  56.  
  57. }
  58.  
  59. int menu()
  60.  
  61. {
  62.  
  63. int ch;
  64.  
  65. printf("-----Select the operation you want to perform on the stack-----\n");
  66.  
  67. printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
  68.  
  69. printf("Enter your choice\t");
  70.  
  71. scanf("%d",&ch);
  72.  
  73. return ch;
  74.  
  75. }
  76.  
  77. void push()
  78.  
  79. {
  80.  
  81. if(top==max) printf("Stack overflow!\n");
  82.  
  83. else
  84.  
  85. {
  86.  
  87. int element;
  88.  
  89. printf("----Enter element----\n");
  90.  
  91. scanf("%d",&element);
  92.  
  93. printf("----Element has been pushed----\n");
  94.  
  95. stack[top++]=element;
  96.  
  97. }
  98.  
  99. }
  100.  
  101. void pop()
  102.  
  103. {
  104.  
  105. if(top==0) printf("----Stack Underflow!----\n");
  106.  
  107. else
  108.  
  109. {
  110.  
  111. top--;
  112.  
  113. printf("----Element has been popped out----\n");
  114.  
  115. }
  116.  
  117. }
  118.  
  119. void display()
  120.  
  121. {
  122.  
  123. if(top==0) printf("----Stack is empty!----\n");
  124.  
  125. else
  126.  
  127. {
  128.  
  129. {
  130.  
  131. inti;
  132.  
  133. printf("----Displaying stack elements----\n");
  134.  
  135. for(i=0;i<top;i++)
  136.  
  137. printf("%d\n",stack[i] );
  138.  
  139. }
  140.  
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement