fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int top = -1, n;
  5.  
  6. void print(int st[]);
  7.  
  8. void pop(int st[]){
  9. if(top == -1){
  10. cout<<"underflow"<<endl;
  11. return;
  12. }
  13. cout<<"Popped item: "<<st[top]<<endl;
  14. top--;
  15. print(st);
  16. }
  17.  
  18. void push(int st[], int data){
  19. if(top == n-1){
  20. cout<<"overflow"<< endl;
  21. return;
  22. }
  23. top++;
  24. st[top] = data;
  25. print(st);
  26. }
  27.  
  28. void print(int st[]){
  29. cout <<"Stack: ";
  30. for(int i=0; i<=top; i++){
  31. cout<<st[i]<<" ";
  32. }
  33. cout<<endl;
  34. }
  35.  
  36. int main(){
  37. cout << "Enter the size: ";
  38. cin >> n;
  39. int st[n];
  40.  
  41. push(st, 10);
  42. push(st, 20);
  43. push(st, 30);
  44.  
  45. pop(st);
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Enter the size: overflow
overflow
overflow
underflow