Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views5 pages

Chapter Two Stack

The document presents a C++ implementation of a stack using arrays with functions for push, pop, and display operations. The main function allows users to interact with the stack by choosing options to manipulate it until they decide to exit. It includes error handling for stack overflow and underflow conditions.

Uploaded by

Eng Said
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)
7 views5 pages

Chapter Two Stack

The document presents a C++ implementation of a stack using arrays with functions for push, pop, and display operations. The main function allows users to interact with the stack by choosing options to manipulate it until they decide to exit. It includes error handling for stack overflow and underflow conditions.

Uploaded by

Eng Said
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/ 5

Stack practical (Main function)

int stack[100],top,n,choice,x;
void push();
void pop();
void display();
int main()
{
top=-1;
cout<<"\n\t enter the size of the stack max[100] ";
cin>>n;
cout<<"\n\t\stack operation using arrays";
cout<<"\n\t\ 1.push \n\t\ 2.pop \n\t\ 3. display \n\t\ 4. exit";
do{
cout<<"\n\t enter your choice ";
cin>>choice;
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<" \n\t exiting from stack";
break;
default:
cout<<"\n\t please enter valid choice (1/2/3/4) ";
}
}
while(choice!=4);

return 0;
}
Push function
void push()
{
if(top>=n-1)
{
cout<<" \n\t stack is overflow";
}
else
{
cout<<"\n\t enter new element";
cin>>x;
top++;
stack[top]=x;
}
}
Pop function
void pop()
{
if(top<=-1)
{
cout<<" \n\t stack is underflow";
}
else
{
cout<<"\n\t this element is deleted"<<stack[top];
top--;
}
}
Display function
void display()
{
if(top>=0)
{
cout<<"\n\t the elements are ";
for(int i=top; i>=0; i--)
{
cout<<"\n\t "<<stack[i];
}
cout<<"\n\t press next choice";
}
else
{
cout<<"\n\t stack is empty"<<endl;
}
}

You might also like