waltermilner

Untitled

Jan 21st, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define STACKSIZE  5 // the size of our stack
  5. int myStack[STACKSIZE]; // the stack
  6. int stackTop=0; // current top of stack - next item goes here
  7.  
  8. void push(int data)
  9. {
  10. if (stackTop==STACKSIZE)
  11. {
  12. printf("Stack overflow\n");
  13. exit(1);
  14. }
  15. myStack[stackTop]=data;
  16. stackTop++;
  17. }
  18.  
  19. int pop()
  20. {
  21. if (stackTop==0)
  22. {
  23. printf("Stack underflow\n");
  24. exit(2);
  25. }
  26. stackTop--;
  27. return myStack[stackTop];
  28. }
  29. int main()
  30. {
  31. push(1);
  32. push(2);
  33. printf("%d\n", pop()); // 2
  34. printf("%d\n", pop()); // 1
  35. push(1); push(1); push(1); push(1); push(1); push(1); // stack overflow
  36. return 0;
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment