//implementing stack adt using array
#include<iostream.h>
#include<conio.h>
template <class t>
class stack{int top;t *a;
public: stack() {top=0;}
void push(t &el) { a[++top]=el; }
t pop()
{
if (top==0)
{cout<<"stack empty";}
return(a[top--]);
}
void display()
{
if (top==0)
{ cout<<"stack is empty"; return;
}
cout<<"the elements are \n";
for(int i=top;i>0;i--)
{cout<<a[i]<<" ";}
}
};
void main()
{
stack <int> st;
char cnt='y';
int choice, element;
clrscr();
do
{
cout<<"main menu\n"; cout<<"1.push\n2.pop\n3.display\nenter your choice\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"enter new element\n";
cin>>element;
st.push(element);
break;
case 2:cout<<"deleted elements\n";
st.pop();
break;
case 3:cout<<"the elements of stack are\n";
st.display();
break;
}
cout<<" do u want to continue(y/n)";
cin>>cnt; getch();
}
while(cnt!='n');
getch();
}
/* note:this is more simpler than the previous one ...If any one found bugs or logical errors please comment......*/
No comments:
Post a Comment