Performing push and pop operations in stack data structure using C++ Language.
#include<iostream>#include<conio.h>using namespace std;int stack[100],top=-1,n,i,j,item;int push();int pop();int display();int main(){ int choice; cout<<"Enter the Size of stack : "; cin>>n; cout<<"\nStack Operations\n"; while(choice!=4) {
cout<<"\n----------------------\n"; cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n"; cout<<"Enter your choice : "; cin>>choice; switch(choice) { case 1: { push(); break; } case 2: { pop(); break; } case 3: { display(); break; } case 4: { cout<<"\nExiting..."; break; } default: { cout<<"\nChoose the correct option..."; } } }}int push(){ if(top==n) { cout<<"\nOverflow..."; } else { cout<<"Enter the element : "; cin>>item; top=top+1; stack[top]=item; cout<<"\nValue Inserted..."; }}int pop(){ if(top==-1) { cout<<"\nUnderflow..."; } else { top=top-1; cout<<"\nTop value poped..."; }}int display(){ if(top==-1) { cout<<"\nStack is Empty..."; } else { cout<<"\nItem values are -\n"; for(i=top;i>=0;i--) { cout<<stack[i]<<endl; } }}
You mast run the above code using Dev C++ compiler to get the exact output given below -
Output -
Enter the Size of stack : 5
Stack Operations
----------------------1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the element : 22
Value Inserted...----------------------1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the element : 33
Value Inserted...----------------------1. Push2. Pop3. Display4. ExitEnter your choice : 2
Top value poped...----------------------1. Push2. Pop3. Display4. ExitEnter your choice : 3
Item values are -22
----------------------1. Push2. Pop3. Display4. ExitEnter your choice : 4
Exiting...--------------------------------Process exited after 19.57 seconds with return value 0Press any key to continue . . .
Also check out this : Queue Data-Structure Program in C Language - Learn With AVRK