1 .
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>
using namespace std;
class stack{
int a[10];
int top;
public:
stack(){
top=-1;
bool push(int n){
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;
int peep(int i){
if((top-i+1)<0){
return -1;
else{
return top-i+1;
bool change(int i){
if((top-i+1)<0){
return false;
else{
int x;
cout<<endl<<"Enter element to insert:";
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{
cout<<"\nWhat you want to do:";
cout<<"\n1:insert element(push)\n2:Delete element(Pop)\n3:Is stack full\n4:Is Stack
empty\n5:Peep\n6:Change\n7:Count Number of elements\n8:Exit\nEnter your choice:";
cin>>c;
switch(c){
case 1:
cout<<"\nEnter number you want to insert:";
cin>>no;
if(s.push(no)){
cout<<endl<<"----"<<no<<" inserted successfully.----
"<<endl;
s.display();
else{
cout<<endl<<"----"<<"There was a problem.----"<<endl;
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{
cout<<endl<<"----"<<"There's still some space in stack.----";
break;
case 4:
if(s.isEmpty()){
cout<<endl<<"----"<<"Stack is empty.----"<<endl;
else{
cout<<endl<<"----"<<"There are some elements in stack.---
-"<<endl;
break;
case 5:
cout<<endl<<"Enter position of element your want:";
cin>>pos;
m=s.peep(pos);
if(m<=-1){
cout<<"\n----Postion you entered does not exist.----\n";
else{
cout<<endl<<"----"<<pos<<" is at "<<m<<" postion.----
\n";
break;
case 6:
cout<<endl<<"Enter position of element your want:";
cin>>pos;
if(!s.change(pos)){
cout<<"\n----Postion you entered does not exist.----\n";
else{
cout<<"\n----Element changed successfully.----\n";
s.display();
break;
case 7:
cout<<"\n----There are "<<s.count()<<" elements in stack.----";
break;
case 8:
break;
default:
cout<<endl<<"----"<<"Invalid input----"<<endl;
}while(c!=8);
return 0;
Push:
Pop:
Change and count:
isFull:
isEmpty:
Peep: