0% found this document useful (0 votes)
54 views

Lab. Sheet Three: Data Structure Laboratory

This document discusses implementing a stack data structure using both an array and linked list. It provides code for an ArrayStack template class that implements push, pop, peek and other standard stack functions for different data types like characters, integers and strings. The document then notes that because an array has a fixed size, it poses limitations. It assigns exercises to implement a copyStack function using push and pop, and to write a program for evaluating postfix expressions using a stack.

Uploaded by

xz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Lab. Sheet Three: Data Structure Laboratory

This document discusses implementing a stack data structure using both an array and linked list. It provides code for an ArrayStack template class that implements push, pop, peek and other standard stack functions for different data types like characters, integers and strings. The document then notes that because an array has a fixed size, it poses limitations. It assigns exercises to implement a copyStack function using push and pop, and to write a program for evaluating postfix expressions using a stack.

Uploaded by

xz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structure

LABORATORY

Lab. Sheet Three

Link Stack

Computer engineering department


Second class
Linear Stack:
#include "stdafx.h"
#include<iostream>
using namespace std ;
#define N 10

template<class T >
class ArrayStack
{
private:
T a[N];
int top ; // stack top
public:
ArrayStack() //constructor
{top= -1 ; }

void push( T item ) // add an item on top of stack


{
if( top== N-1 )
{cout<<"Stack is Full " << endl;

return ;
}

top++ ; //increament top


a[top] = item ;
}
T pop() //remove an item from top of stack
{
if(isEmpty())
{
cout<<"Stack is empty " <<endl ;
return NULL ;
}

T item= a[top] ; //access top item


top-- ; //decrement top
return item ;

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

You might also like