Advertisement
palmerstone

stack

Jul 24th, 2011
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. #define MAX 100
  8.  
  9. int t, i, j = 0, k, l, a, b, c, d, n, x, y, z;
  10. int ar[MAX];
  11. int top = 0;
  12.  
  13. void stack()
  14. {
  15. puts("Press Any key to continue");
  16. puts("1 --> push");
  17. puts("2 --> pop");
  18. puts("3 --> exit");
  19. }
  20.  
  21. void push()
  22. {
  23. if (j >= MAX) printf("Stack full\n");
  24. else
  25. {
  26. printf("Item to push:");
  27. scanf("%d", &ar[j]);
  28. j++;
  29. top = j;
  30. printf("Item %d has been pushed\n", ar[j - 1]);
  31. }
  32. }
  33.  
  34. int pop()
  35. {
  36. if (top == 0) {puts("Stack empty, invalid operation"); return 0;}
  37. else
  38. {
  39. x = ar[j - 1];
  40. ar[j - 1] = 0;
  41. j--;
  42. return x;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. for (; ;)
  49. {
  50. void stack();
  51. scanf("%d", &n);
  52. if (n == 3) break;
  53. if (n == 1)
  54. {
  55. push();
  56. }
  57. else if (n == 2)
  58. {
  59. z = pop();
  60. if (z)printf("Item %d has been popped\n", z);
  61.  
  62. }
  63. else puts("Ooops...try again");
  64. }
  65. for (i = 0; i < j; i++)
  66. printf("%d ", ar[j]);
  67. puts("");
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement