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

Record File - Stacks

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)
22 views4 pages

Record File - Stacks

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

STACK USING ARRAYS

#include <iostream>

using namespace std;


void push(int);
void pop();
void display();
int stack[5];
int n=5;
int staJc top = -1;

int main()
{
int ch, value;
while(1){
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice";
cin>>ch;

switch(ch)
{
case 1: cout<<"enter element";
cin>>value;
push(value);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
exit(0);

default: cout<<"\n Invalid entry";

}
}
return(1);
}
void push(int value)
{
if(top >= n-1)
{
cout<<"\nstack overflow";
}
else{
top++;
stack[top] = value;
}

void pop()
{
if(top == -1)
{
cout<<"\n stack underflow";
}
else
{
cout<<"popped vlue = "<<stack[top];
top--;
}
}

void display()
{
if(top>=0)
{
for(int i=top;i>=0;i--)
{
cout<<stack[i]<<"\n";
}
}
else
{
cout<<"\n stack is empty";
}
}

You might also like