Saturday, November 19, 2011

C and C++ Examples (Stack )


#include<iostream.h>
#include<conio.h>
template<class p>
class stack
{
p s[10];
int i;
p element;
int top;
public:
stack()
{
top=-1;
}
void push()
{
if(top>10)
cout<<"stack is full";
else
{
cout<<"enter the element to push"<<endl;
cin>>element;
s[++top]=element;
cout<<"element\t"<<element<<"\tis pushed"<<endl;
}
}
void pop()
{
if(top<0)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<"element poped is"<<s[top--]<<endl;
}
}
void display()
{
if(top<0)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<"the elements are"<<endl;
for(i=0;i<=top;i++)
{
cout<<"\t"<<s[i];
}
}
}
};
void main()
{
stack<int> obj;
stack<float> obj1;
stack<char> obj2;
int ch;
clrscr();
while(1)
{
cout<<"enter\n1.int push\n2.float push\n3.char push"<<endl;
cout<<"\n4.int pop\n5.float pop\n6.char pop"<<endl;
cout<<"\n7.int display\n8.float display\n9.char display\n10.exit"<<endl;
cout<<"\nenter u r choice"<<endl;
cin>>ch;
if(ch==10)
{
break;
}
switch(ch)
{
case 1:obj.push();
getch();
clrscr();
break;
case 2:obj1.push();
getch();
clrscr();
break;
case 3:obj2.push();
getch();
clrscr();
break;
case 4:obj.pop();
getch();
clrscr();
break;
case 5:obj1.pop();
getch();
clrscr();
break;
case 6:obj2.pop();
getch();
clrscr();
break;
case 7:obj.display();
getch();
clrscr();
break;
case 8:obj1.display();
getch();
clrscr();
break;
case 9:obj2.display();
getch();
clrscr();
break;
}
}
}

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju