0% found this document useful (0 votes)
33 views4 pages

Astack

Uploaded by

Mimansha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Astack

Uploaded by

Mimansha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//PROGRAM FOR ARRAY/STATIC IMPLEMENTATION OF A STACK

#include<iostream.h>
#include<conio.h>
const int SIZE=10;
class stacktype
{
int s[SIZE];
int top;
public:
void push(int);
int pop();
int isempty();
int isfull();
void display();
stacktype()
{
top=-1;
}
};
void stacktype::push(int p)
{
top++;
s[top]=p;
}
T stacktype::pop()
{
int d;
d=s[top];
top--;
return(d);
}
void stacktype::display()
{
if(top==-1)
cout<<"stack is empty"<<endl;
else
{
cout<<"Contents of the stack starting from top are ";
for(int i=top;i>=0;i--)
cout<<s[i]<<" ";
cout<<endl;
}
}
int stacktype::isempty()
{
int e;
if(top==-1)
{
cout<<endl<<"Stack is empty"<<endl;
return(1);
}
else
{
cout<<"stack is not empty"<<endl;
return(0);
}
}
int stacktype::isfull()
{
int f;
if(top==SIZE-1)
{
cout<<"Stack is full"<<endl;
return(1);
}
else
{
cout<<endl<<"Stack is not full"<<endl;
return(0);
}
}

void main()
{
stacktype st;
char c='y';
int choice,f,p,d,e;
clrscr();
while(c=='y' || c=='Y')
{
cout<<endl<<"Main Menu"<<endl;
cout<<"1.Push onto the stack"<<endl;
cout<<"2.Pop from the stack"<<endl;
cout<<"3.Check if stack is empty"<<endl;
cout<<"4.Check if stack is full"<<endl;
<<"\n5.Display"<<endl;
cout
cout<<"Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:f=st.isfull();
if(f==1)
cout<<"Stack full.Insertion not possible";
else
{
cout<<"Enter the element to be pushed ";
cin>>p;
st.push(p);
st.display();
}
break;
case 2:e=st.isempty();
if(e==1)
cout<<".. deletion not possible"<<endl;
else
{
d=s.pop();
cout<<"\n"<<d<<" has been deleted";
st.display();
}
break;
case 3:e=st.isempty();
if(e==1)
cout<<"\n Stack is empty";
else
cout<<"\n Stack is not empty";
break;
case 4:f=stack.isfull();
if(f==1)
cout<<".......
else
cout<<"\n............
break;
case 5:st.display();
break;
default:cout<<"Error in input";
}
cout<<"Do you want to continue(y or n) ";
cin>>c;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy