fork download
  1. #include <stdio.h>
  2.  
  3. #define STACK_SIZE 100
  4. int stack[STACK_SIZE];
  5. int sp=0;
  6.  
  7. int push(int x){
  8. printf("push> sp= %d\n", sp);
  9.  
  10. if(sp == STACK_SIZE)
  11. return -1;
  12. stack[sp] = x;
  13. sp++;
  14. return 1;
  15. }
  16.  
  17. int pop(void){
  18. printf("pop> sp= %d\n", sp);
  19.  
  20. if(sp == 0)
  21. return -1;
  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() {
  35.  
  36. push(1);
  37. push(2);
  38. push(3);
  39.  
  40. printStack();
  41.  
  42. printf("%d\n", pop());
  43. printf("%d\n", pop());
  44. printf("%d\n", pop());
  45. printf("%d\n", pop());
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 5316KB
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