#include<iostream.h>
#include<constream.h>
template<class T>
class Stack
{
public:
Stack(int MaxStackSize);
~Stack(){delete[] S;}
int IsEmpty()const{return top==-1;}
int IsFull()const{return top==MaxTop;}
T Peek()const;
void Push(T);
T Pop();
void Display();
private:
int top; //current top of stack
int MaxTop; //max val for top
T *S; //element array
};
template<class T>
Stack<T>::Stack(int MaxStackSize)
{
//stack constructor
MaxTop=MaxStackSize-1;
S=new T[MaxStackSize];
top=-1;
}
template<class T>
T Stack<T>::Peek()const
{
if(IsEmpty()) //top fails
return 0;
else
return S[top];
}
template<class T>
void Stack<T>::Push(T x)
{
if(IsFull())
cout<<"no memory()"; //add fails
else
{
S[++top]=x;
}
}
template<class T>
T Stack<T>::Pop()
{
T x;
if(IsEmpty())
{
cout<<"stack is empty\n";
return -1;
}
else
{
x=S[top--];
return x;
}
}
template<class T>
void Stack<T>::Display()
{
if(IsEmpty())
cout<<"out of bounds"; //delete fails
else
for(int i=top;i>=0;i--)
{
cout<<S[i]<<"\t";
}
}
void menu()
{
cout<<"1.Push\n 2.Pop\n 3.Peek\n 4.Display\n";
}
void main()
{
Stack<int>iobj(5);
int ch,x;
clrscr();
do
{
menu();
cout<<"enter the choice\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter x value to push into the stack\n";
cin>>x;
iobj.Push(x);
break;
case 2:
x=iobj.Pop();
if(x!=-1)
cout<<"poped value is \t"<<x<<endl;
break;
case 3:
x=iobj.Peek();
cout<<"top most value is \t"<<x<<endl;
break;
case 4:
iobj.Display();
break;
}
}while(ch>=1&&ch<=4);
getch();
}
Not Satisfied ? Just search & get the result
Related posts:
- C++ program to implement stack using Linked List
- Sample – Program to enter an integer and print its total value based on the formula ‘x – 1/3!x3 + 1/5!x5 – 1/7!x7 + 1/9!x9′.
- Sample – understanding the concept of objects
- Sample – functions
- Sample – Program to print the first 10 lines of pascal’s triangle.
