0% found this document useful (0 votes)
6 views3 pages

Exp 4

The document defines a class called stack with methods like push, pop and display to perform stack operations. It also contains a main function to test the stack implementation by taking user input for different stack operations and performing them.

Uploaded by

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

Exp 4

The document defines a class called stack with methods like push, pop and display to perform stack operations. It also contains a main function to test the stack implementation by taking user input for different stack operations and performing them.

Uploaded by

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

Name- Kartik Shinde

Roll no-2059 S7
#include<iostream>
using namespace std;
class stack
{
int stackarray[10];
int top;
int maxsize;
public:
stack() //non paramitrized constructor
{
top=-1;
maxsize=5;
}
void push(int x)
{
if(top==maxsize-1)
{
cout<<"stack overflow"<<endl;
}
else
{
top=top+1;
stackarray[top]= x;
}}
void pop()
{
if(top==-1)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<"poped element"<<stackarray[top]<<endl;
top--;
}}
void display()
{
if(top==-1)
{
cout<<"stack underflow"<<endl;
}
else
{
for(int i=top;i>-1;i--)
{
cout<<stackarray[i]<<endl;
}
}
}
~stack() //destructor
{
cout<<"destructor called"<<endl;
}
};
int main()
{
stack s;
int num;
int choice;
do{
cout<<"enter the operation"<<endl;
cout<<"1.push"<<endl<<"2.pop"<<endl<<"3.display"<<endl<<"4.exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter the num"<<endl;
cin>> num;
s.push(num);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
cout<<"exit"<<endl;
}}
while(choice!=4);
{return 0;
}
return 0;
}

You might also like