fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class stack{
  5. private:
  6. int size;
  7. int top;
  8. int *arr;
  9. public:
  10. stack(int value){
  11. size=value;
  12. top=-1;
  13. arr = new int[size];
  14. }
  15. bool empty(){
  16.  
  17. return(top==-1);}
  18. bool full(){
  19. return(top==size-1);}
  20. void push(int v){
  21. if (full()){
  22. cout<<" sorry i cant add : "<< v<<endl;
  23. }
  24. else{
  25. top++;
  26. arr[top]=v;
  27. cout<< v<<" "<<"push succefull"<<endl;
  28. }}
  29. int pop(){
  30. if(empty()){
  31. cout<<" stack is empty"<<endl;
  32.  
  33. } else{
  34.  
  35. cout<< " pop succsesful"<<endl;}
  36. return arr[top--];}
  37.  
  38.  
  39. int peek(){
  40. return arr[top];}
  41. void display(){
  42. cout<<" your element is "<<endl;
  43. for(int i=top;i>=0;i--){
  44. cout << " valuue is :"<< arr[i]<<" ";
  45. }cout<<endl;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. };
  52. int main()
  53. {
  54. int si;
  55. cout<< "enter your(stack)size"<<endl;
  56. cin>>si;
  57. cout << " your size now is " << si<<endl;
  58. stack s(si);
  59. cout<<" enter size order for push"<<endl;
  60. int p;
  61. cin>>p;
  62. while (p-->0)
  63. {
  64. int value ;
  65. cout<< "push : value << ";
  66. cin>>value;
  67. s.push(value);
  68. }
  69.  
  70. s.display();
  71. cout<<"how often do you want to pop : ";
  72. int pop;
  73. cin>>pop;
  74. cout<<endl;
  75. while (pop-->0)
  76. {
  77. cout<<s.pop();
  78. }
  79. cout<<endl;
  80.  
  81. cout<<" the end value is : "<<s.peek()<<endl;
  82. cout<<"now your element after push and pop is "<<endl;
  83. s.display();
  84.  
  85.  
  86.  
  87. return 0;
  88. }
  89.  
Success #stdin #stdout 0.01s 5268KB
stdin
5
4
1
2
3
4
2
stdout
enter your(stack)size
 your size now is 5
 enter size order for push
push : value << 1 push succefull
push : value << 2 push succefull
push : value << 3 push succefull
push : value << 4 push succefull
 your element is 
 valuue is :4  valuue is :3  valuue is :2  valuue is :1 
how often do you want to pop : 
 pop succsesful
4 pop succsesful
3
 the end value is : 2
now your element after push and pop is 
 your element is 
 valuue is :2  valuue is :1