fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 5
  4.  
  5. int stack[SIZE];
  6. int sp = 0;
  7.  
  8. void push(int data) {
  9. printf("push()sp = %d\n", sp);
  10.  
  11. if (sp >= SIZE) return;
  12.  
  13. stack[sp] = data;
  14. sp++;
  15. }
  16.  
  17. int pop(void) {
  18. printf("pop()sp = %d\n", sp);
  19.  
  20. if (sp <= 0) return -1;
  21.  
  22. sp--;
  23. return stack[sp];
  24. }
  25.  
  26. void printStack(void) {
  27. printf("printstack: ");
  28. for (int i = 0; i < sp; i++) {
  29. printf("%d ", stack[i]);
  30. }
  31. printf("\n");
  32. }
  33.  
  34. int main(void) {
  35. push(1);
  36. push(2);
  37. push(3);
  38.  
  39. printStack();
  40.  
  41. printf("%d\n", pop());
  42. printf("%d\n", pop());
  43. printf("%d\n", pop());
  44. printf("%d\n", pop());
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
push()sp = 0
push()sp = 1
push()sp = 2
printstack: 1 2 3 
pop()sp = 3
3
pop()sp = 2
2
pop()sp = 1
1
pop()sp = 0
-1