fork(1) download
  1. #include<stdio.h>
  2. #define sz 100
  3.  
  4. int arr[sz],top=0;
  5.  
  6. void push(int val)
  7. {
  8. if(top>=sz)
  9. printf("No more space on Stack.\n");
  10. else
  11. arr[top++]=val;
  12. }
  13.  
  14. void pop()
  15. {
  16. if(top<=0)
  17. {
  18. printf("Stack is empty\n");
  19. }
  20. else
  21. {
  22. top--;
  23. printf("Popped element: %d\n",arr[top]);
  24. }
  25. }
  26.  
  27. void printlist()
  28. {
  29. int i;
  30. for(i=0;i<top;i++)
  31. printf("%d ",arr[i]);
  32. puts("");
  33. }
  34.  
  35. int main()
  36. {
  37. int num,val;
  38. bool getout=false;
  39.  
  40. while(true)
  41. {
  42. printf("Enter your choice:\n");
  43. printf("1. Push\n2. Pop\n3. Exit\n");
  44. scanf("%d",&num);
  45.  
  46. switch(num)
  47. {
  48. case 1:
  49. scanf("%d",&val);
  50.  
  51. push(val);
  52.  
  53. printlist();
  54. puts("");
  55. break;
  56. case 2:
  57. pop();
  58.  
  59. printlist();
  60. puts("");
  61. break;
  62. case 3:
  63. getout=true;
  64. break;
  65. }
  66.  
  67. if(getout==true)
  68. break;
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74.  
Success #stdin #stdout 0s 2856KB
stdin
1
10
1
20
2
2
2
3
stdout
Enter your choice:
1. Push
2. Pop
3. Exit
10 

Enter your choice:
1. Push
2. Pop
3. Exit
10 20 

Enter your choice:
1. Push
2. Pop
3. Exit
Popped element: 20
10 

Enter your choice:
1. Push
2. Pop
3. Exit
Popped element: 10


Enter your choice:
1. Push
2. Pop
3. Exit
Stack is empty


Enter your choice:
1. Push
2. Pop
3. Exit