Lab. Sheet Three: Data Structure Laboratory
Lab. Sheet Three: Data Structure Laboratory
LABORATORY
Link Stack
template<class T >
class ArrayStack
{
private:
T a[N];
int top ; // stack top
public:
ArrayStack() //constructor
{top= -1 ; }
return ;
}
T peek()
{
if (isEmpty() ) return NULL ;
return a[top] ;
}
bool isEmpty()
{
return (top== -1) ;}
int size()
{return top+1 ; }
};
void main()
{ int i,n ;
ArrayStack<char> cs ;
cs.push ('A') ; cs.push('B');
cs.push ('C') ; cs.push('D');
cs.push ('E') ; cs.push('F');
n= cs.size() ;
cout<<"\nPoped items from char stack : " ;
for(i= 0 ;i<n ; i++)
cout<<" " << cs.pop() ;
ArrayStack<int> ints ;
ints.push(11);
ints.push (22);
ints.push (33);
ints.push(44);
n=ints.size( ) ;
cout<<"\nPopped items from integer stack : " ;
for(i=0 ; i<n ; i++)
cout<< " " <<ints.pop() ;
ArrayStack<char*> ss ;
ss.push("Alpha");
ss.push ("Beta");
ss.push ("Gamma");
n= ss.size() ;
cout<<"\nPoped items from char stack : " ;
for(i= 0 ;i<n ; i++)
cout<<" " << ss.pop() ;
cout<<"\n\n";
}
Lab Exercise:
Because an array size is fixed, in the array (linear) representation of a stack, only a
fixed number of elements can be pushed onto the stack. If in a program the number of
elements to be pushed exceeds the size of the array, the program may terminate in an
error. We can overcome these problems by emplementing Stack using link list.
Exercises:
1. Write function copyStack makes an identical copy of a stack. The function
should use push and pop functions.
2. Using stack datastructuer conseept ,write a program to implement Postfix
Expressions Calculator.
Note:- In your program you should enter inphix mathamtical
expression , and that would be evaluated as Postfix