0% found this document useful (0 votes)
2 views2 pages

Stack Using Templates

This document contains a C++ implementation of a generic stack class using templates. The stack supports basic operations such as push, pop, and display, and allows users to create stacks for different data types (int and float). The main function provides a user interface to interact with the stack operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Stack Using Templates

This document contains a C++ implementation of a generic stack class using templates. The stack supports basic operations such as push, pop, and display, and allows users to create stacks for different data types (int and float). The main function provides a user interface to interact with the stack operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;


template<class Type>
class stack
{
Type s[10];
int top=-1,size;
public:
stack()
{

cout<<"\nenter the size of stack::";


cin>>size;
}
void push(Type item)
{
if(top==size-1)
cout<<"\nstack is full!!";
else
{
top++;
s[top]=item;
}
}
void pop()
{
if(top==-1)
cout<<"\nstack is empty!!";
else
{
cout<<"\npopped element:"<<s[top];
top--;
}
}
void stack_op();
};
template<class Type>void stack<Type>::stack_op()
{
Type item;
int choice=1,i;
while(choice>0&&choice<3)
{
cout<<"\n1..push\t2..pop\t3.display\tenter eny key to exist!!";
cout<<"\n enter the choice::";
cin>>choice;
switch(choice)
{
case 1: cout<<"enter the item::";
cin>>item;
push(item);
break;
case 2: pop();
break;
case 3: cout<<"\n items in the stack are::";
for(i=0;i<=top;i++)
{
cout<<s[i]<<"\t";
}
break;
default:cout<<"invalid choice!!";

}
}

};

int main()
{
cout<<"\nstack operation using templates";
cout<<"\nint stack!!";
stack<int>s1;
cout<<"\n float stack!!";
stack<float>s2;
int choice;
while(1)
{
cout<<"\n1..INT\t2..FLOAT\tenter any key to exit!!";
cout<<"\nenter the choice::";
cin>>choice;
switch(choice)
{
case 1:s1.stack_op();
break;
case 2:s2.stack_op();
break;
default:exit(0);
}
}
return 0;
}

You might also like