#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();
} |
#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();
}
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.
good