Stack
Stack
WAP in C++ to implement stack using array and perform following operations:
1. PUSH 2. POP 3. PEEP 4. TOP 5. CHANGE 6. COUNT 7. ISFULL 8. ISEMPTY. Write a
menu driven program.
Program:
#include<iostream>
class stack{
int a[10];
int top;
public:
stack(){
top=-1;
if(top==9){
return false;
else{
top=top+1;
a[top]=n;
return true;
int pop(){
if(top==-1){
cout<<"\nStack is empty!";
return -1;
else{
int y=a[top];
top=top-1;
a[top+1]=0;
return y;
bool isFull(){
if(top==9){
return true;
else{
return false;
bool isEmpty(){
if(top==-1){
return true;
else{
return false;
}
}
int getTop(){
return top;
if((top-i+1)<0){
return -1;
else{
return top-i+1;
if((top-i+1)<0){
return false;
else{
int x;
cin>>x;
a[top-i+1]=x;
return true;
int count(){
if(top==-1){
return 0;
else{
return (top+1);
void display(){
cout<<endl<<"Elements is stack:\n";
for(int k=0;k<=top;k++){
cout<<"\t"<<a[k];
};
int main(){
int c,no,pos,m;
stack s;
do{
cin>>c;
switch(c){
case 1:
cout<<"\nEnter number you want to insert:";
cin>>no;
if(s.push(no)){
s.display();
else{
break;
case 2:
no=s.pop();
if(no==-1){
cout<<endl<<"----"<<"Stack is empty.----"<<endl;
else{
cout<<endl<<"----"<<no<<" popped.----"<<endl;
s.display();
break;
case 3:
if(s.isFull()){
cout<<endl<<"----"<<"Stack is full.----"<<endl;
}
else{
break;
case 4:
if(s.isEmpty()){
cout<<endl<<"----"<<"Stack is empty.----"<<endl;
else{
break;
case 5:
cin>>pos;
m=s.peep(pos);
if(m<=-1){
else{
break;
case 6:
if(!s.change(pos)){
else{
s.display();
break;
case 7:
break;
case 8:
break;
default:
cout<<endl<<"----"<<"Invalid input----"<<endl;
}while(c!=8);
return 0;
Push:
Pop:
Change and count:
isFull:
isEmpty:
Peep: