/* static implementation of stack*/
#include<iostream.h>
#include<conio.h>
template <class T>
class stack
{
private:
T s[5];
int top,size;
public:
stack();
void push();
T pop();
void display();
};
stack<T>::stack(int k=40)
{
size=k;
top=-1;
}
template <class T>
void stack<T>::push(T x)
{
if(top==size-1)
{
cout<<"\nStack is Full\n";
}
else
{
top=top+1;
s[top]=x;
}
}
template<class T>
T stack<T>::pop()
{
T x;
if(top==-1)
{
return(-1);
}
else
{
x=s[top];
top--;
return(x);
}
}
template<class T>
void stack<T>::display()
{
if(top==-1)
cout<<"stack is empty\n";
cout<<"\n item in stack are\n";
for(i=top; i>=0; i--)
cout<<"s[i]<<"\n";;
}
void menu()
{
cout<<"1. integer stack\n";
cout<<"2. float stack\n";
cout<<"3. char stack\n";
cout<<"4. exit\n";
}
void menu2()
{
cout<<"1. insertion\n";
cout<<"2. deletion\n";
cout<<"3. display the stack\n";
cout<<"4. exit\n";
}
template<class T>
void operation(stack<T> &b)
{
int ch,i;
T x;
menu();
cout<<"enter ur choic\n"
cin>>ch;
while(ch<4)
{
switch(ch)
{
case 1:
{
cout<<"enter value to be inserted\n";
cin>>x;
if(x==-1)
cout<<"-1 is not allowed\n";
b.push(x);
break;
}
case 2:
{
b.pop();
if(x==-1)
cout<<"deletion is not possible\n";
else
cout<<"deleted value is="<<x<<endl;
break;
case 3:{
b.display();
break;
}
}
menu();
cout<<"enter ur choic\n"
cin>>ch;
}
}
void main()
{
int ch,k;
clrscr();
menu2();
cout<<"enter ur choic\n"
cin>>ch;
while(ch<4)
{
cout<<"enter size of the stack\n";
cin>>k;
switch(ch)
case 1:
{
stack<int> a(k);
operation(a);
break;
}
case 2:
{
stack<float> a(k);
operation(a);
break;
}
case 2:
{
stack<char> a(k);
operation(a);
break;
}
}
menu2();
cout<<"enter ur choic\n"
cin>>ch;
}//end of while
}//end of main()
}
void main()
{
stack<int> a;
int x;
cout<<"enter value to be inserted, dont enter -1\n";
cin>>x;
if(x==-1)
cout<<"renter a value\n";
a.push()
clrscr();
while(ch!='n')
{
push();
cout<<"\n Do you want to push any item in stack y/n");
ch=getch();
}
display();
while(ch1!='n')
{
cout<<"\n Do you want to delete any item in stack y/n");
ch1=getch();
pop();
}
display();
getch();
}
Definition
The abstract Data Type
Array Representation
No comments:
Post a Comment