Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /*Stack Operations with Array
  2. Afif Al Mamun*/
  3.  
  4. #include <stdio.h>
  5. #define MAX 5
  6.  
  7. int top = -1;
  8. int arr[MAX];
  9.  
  10. void push(int val)
  11. {
  12. if(top==MAX-1)
  13. {
  14. printf("Stack Overflow!\n");
  15. return;
  16. }
  17. top++;
  18. arr[top]=val;
  19. }
  20.  
  21. void pop()
  22. {
  23. if(top==-1)
  24. {
  25. printf("Stack Underflow\n");
  26. return;
  27. }
  28. printf("%d has been popped!\n",arr[top--]);
  29. }
  30.  
  31. int peek()
  32. {
  33. return arr[top];
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39. int opt=1, v, i;
  40.  
  41. while(opt!=0)
  42. {
  43. printf("1. Push\n2. Pop\n3. Top\n4. Display\n0. Exit\n\n");
  44. printf("Your choice: ");
  45.  
  46. scanf("%d",&opt);
  47. printf("\n");
  48.  
  49.  
  50. switch(opt)
  51. {
  52. case 1:
  53. printf("--Push--\n");
  54. printf("Enter the value: ");
  55. scanf("%d",&v);
  56. push(v);
  57. break;
  58.  
  59. case 2:
  60. printf("--Pop--\n");
  61. pop();
  62. break;
  63.  
  64. case 3:
  65. printf("--Top--\n");
  66. printf("%d\n",peek());
  67. break;
  68.  
  69. case 4:
  70. printf("--Display--\n");
  71. for(i=top;i>=0;i--)
  72. {
  73. printf("---\n");
  74. printf(" %d\n",arr[i]);
  75. }
  76. printf("---\n");
  77. break;
  78.  
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement