Computer Science Program
Sunday, 2 March 2014
Friday, 21 February 2014
C++ Program
(Stack)
using Template !!!!!
#include<iostream>
#include<iomanip>
using namespace std;
template<class x>
class stack
{
x stk[10];
int tos;
public:
stack()
{
tos=0;
}
void push(x ob);
x pop();
void print();
void print1();
};
template<class x>
void stack<x>::push(x ob)
{
int size=10;
if(tos==size)
{
cout<<"Stack Full"<<endl;
}
else
{
stk[tos]=ob;
print();
tos++;
}
}
template<class x>
x stack<x>::pop()
{
if(tos==0)
{
cout<<"Stack Empty"<<endl;
return 0;
}
else
{
tos--;
cout<<"Pop value :"<<endl;
cout<<"stack["<<tos<<"]"<<" | "<< setw(4)<<stk[tos]<<endl;
cout<<"\nAvailable Stack are\n";
print1();
return stk[tos];
}
}
template<class x>
void stack<x>::print()
{
if(tos<=10)
{
cout<<"Position"<<" | "<<"Stack"<<endl;
for(int i=0; i<=tos; i++)
{
cout<<"stack["<<i<<"]"<<" | "<< setw(4)<<stk[i]<<endl;
}
}
else
{
cout<<"Sorry Space not available"<<endl;
}
}
template<class x>
void stack<x>::print1()
{
cout<<"Position"<<" | "<<"Stack"<<endl;
for(int i=0; i<=tos-1; i++)
{
cout<<"stack["<<i<<"]"<<" | "<< setw(4)<<stk[i]<<endl;
}
}
int main()
{
int a,d,ch,count=0, count1=0;
char b,again;
stack<int>s1;
stack<char>s2;
do
{
cout<<"(1).ONLY INTEGER"<<endl;
cout<<"(2).CHARACTER"<<endl;
cout<<"(3).PRINT TOTAL STACK VALUE"<<endl;
cout<<"\nPress any key"<<endl;
cin>>ch;
switch(ch)
{
case 1:do
{
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"\nPress any key"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the integer value"<<endl;
cin>>a;
s1.push(a);
count++;
break;
case 2:s1.pop();
count--;
break;
default:cout<<"wrong choice"<<endl;
}
cout<<"Do You want to run integer Value again (Y/N)"<<endl;
cin>>again;
}while(again=='Y' || again=='y');
break;
case 2: do
{
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"\nPress any key"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the character value"<<endl;
cin>>b;
s2.push(b);
count1++;
break;
case 2:s2.pop();
count1--;
break;
default:cout<<"wrong choice"<<endl;
}
cout<<"Do You want to run character Value again (Y/N)"<<endl;
cin>>again;
}while(again=='Y' || again=='y');
break;
case 3: cout<<"\n Total Value in stack\n ";
cout<<"Integer:"<<setw(2)<<count<<" | "<<"character :"<<setw(2)<<count1<<endl;
d=count+count1;
cout<<" Total stack :"<<setw(2)<<d<<endl;
break;
default:cout<<"wrong choice"<<endl;
}
cout<<"Do u want to run again(Y/N)"<<endl;
cin>>again;
}while(again=='Y' || again=='y');
return 0;
}
Subscribe to:
Posts (Atom)
