DS Lab 5 - Array Based Stack Implementation
DS Lab 5 - Array Based Stack Implementation
Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Stack implementation as a static array...................................................................................................................3
1.2 Stack implementation as a dynamic array.............................................................................................................3
1.3 Stack as a class template........................................................................................................................................3
1.4Relevant Lecture Readings..........................................................................................................................................3
2. Activity Time boxing.............................................................................................................................................4
3. Objectives of the experiment.................................................................................................................................4
4. Concept Map..........................................................................................................................................................4
4.1 stack class using a static array.....................................................................................................................................4
4.2Stacks using dynamic arrays........................................................................................................................................6
4.3Stack using a class template.........................................................................................................................................8
6. Procedure& Tools................................................................................................................................................................9
6.1 Tools............................................................................................................................................................................9
6.2 Walk through Tasks [Expected time = 20mins].................................................................................................9
7. Practice Tasks....................................................................................................................................................................11
7.1 Practice Task 1 [Expected time = 15mins]......................................................................................................11
7.2 Practice Task 2 [Expected time = 20mins]....................................................................11
7.4Out comes...................................................................................................................................................................12
7.5Testing........................................................................................................................................................................13
8.Evaluation Task (Unseen) [Expected time = 60mins]..................................................................................................13
9. Evaluation criteria.............................................................................................................................................................14
10. Further Readings.............................................................................................................................................................14
10.1 Web sites related to C++ tutorials related to stacks................................................................................................14
10.2 Web sites containing supporting material...............................................................................................................14
1. Introduction
Objective of this lab is to introduce the implementation of stack in C++ and further introduce
concept of class templates and defining a stack as a class template or generic class. A stack allows last in
first out (LIFO) operations. We can place a new element at top of stack and we may remove an element
from top of stack. Stacks can be implemented using static as well as dynamic arrays. Further we may
implement a stack as a generic class/class template.
A stack can be implemented using a static array in C++. We need to define an array with a
maximum size and a variable of integer type normally called “top” for such stack. Stack is initially set to
empty when program is executed, so at start of program top is set to -1 which represents an empty stack.
There are two operations associated with a stack; push operation—used to add an element at top of stack
and pop operation – to remove an element from top of stack. When push operation is performed we need
to check that whether stack exceeds its maximum size or not, this is called stack overflow condition
check. When pop operation is performed we need to test that whether stack is empty or not, this is called
stack underflow condition check.
When maximum size of stack is to be decided at program execution time, then we need a dynamic
array to represent the stack. Size of such array is specified at program execution time, a pointer is defined
in program which points to an array dynamically created. Even the stack with dynamic array is
implemented for stack overflow and underflow conditions in push and pop operations respectively.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in lab.
4.1 stack class using a static array
Following is an implementation of stack using static array in C++.
#include <iostream>
using namespace std;
stack::stack()
{
tos = -1;
}
Department of Computer Science Page 4
IIUI, Islamabad
Lab 5: Array based Stack implementation
void stack::push(int x)
{
if(!full())
arr[++tos] = x;
else
cout<< "Stack is full, Cannot push " << x <<endl;
}
int stack::pop()
{
if(!empty())
return arr[tos--];
else
cout<< "Stack is empty, cannot pop" <<endl;
return -1;
}
bool stack::empty()
{
if(tos == -1)
return true;
else
return false;
}
bool stack::full()
{
if(tos+1 == STACKSIZE)
return true;
else
return false;
}
int stack::size()
{
return tos;
}
void stack::display()
{
if(tos == -1)
{
cout<< "No elements to display" <<endl;
return;
}
for(int i=0;i < STACKSIZE; i++)
Department of Computer Science Page 5
IIUI, Islamabad
Lab 5: Array based Stack implementation
int main()
{
stackmystack;
cout<<mystack.size() <<endl;
mystack.push(1);
if(mystack.full())
cout<< "stack is full" <<endl;
mystack.pop();
if(mystack.empty())
cout<< "stack is empty" <<endl;
}
Following figure 1 represents the functionality of pop and push operations on a stack implemented
using a static array.
For the array implementation, we need to keep track of (at least) the array contents and a top
index. Definition of stack class will be as:
class Stack{
public:
Stack(int stack_size);
Department of Computer Science Page 6
IIUI, Islamabad
Lab 5: Array based Stack implementation
~Stack();
void push(int x);
int pop();
bool isEmpty();
bool isFull();
private:
char *contents;
int top;
};
Stack::Stack(int stack_size)
{
if (contents == NULL) {
cout<< "Insufficient memory to initialize stack.\n";
exit(1);
}
maxSize = stack_size;
top = -1;
}
Stack::~Stack()
{
delete [] contents;
contents = NULL;
maxSize = 0;
top = -1;
}
contents[++top] = element;
Department of Computer Science Page 7
IIUI, Islamabad
Lab 5: Array based Stack implementation
char Stack::pop()
{
if(!isEmpty())
return contents[--top];
else
cout<< "Stack is empty, cannot pop" <<endl;
return -1;
}
4.3Stack using a class template
A stack using a class template provides a generic implementation of stack which provides facility
to use stack with many primitive and user defined data types. Creating a stack using a class template in C+
+ provides a way to create a generic stack that can store elements of any data type. Using templates allows
you to write code that is independent of the specific type of data that the stack will hold, providing
flexibility and code reusability.
int main() {
Stack<char, 10> s; // 10 chars
Char ch;
while ((ch = cin.get()) != '\n')
if (!s.full()) s.push(ch);
while (!s.empty())
cout<<s.pop();
cout<<endl;
Stack<double, 4> ds; // 4 doubles
double d[] =
{1.0, 3.0, 5.0, 7.0, 9.0, 0.0};
int i = 0;
while (d[i] != 0.0 && !ds.full())
if (!ds.full()) ds.push(d[i++]);
while (!ds.empty())
cout<<ds.pop() << " ";
cout<<endl;
return 0;
}
6. Procedure& Tools
This section provides information about tools and programming procedures used for the lab.
6.1 Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
6.2 Walk through Tasks [Expected time = 20mins]
We will discuss the demonstration of stack using static array in Microsoft Visual Studio 2017. You
are required to open an empty Win32 console application. You have already knowledge of creating a
console application in Microsoft Visual Studio 2017 in your courses: computer programming and object
oriented programming, which you have previously studied.
Figure 2: Stack definition using static array in Microsoft Visual Studio 2017.
Figure 3: Implementation of push and pop operations for stack using static array in Microsoft Visual Studio
2017
Figure 4: Output window displaying values from stack in Microsoft Visual Studio 2017
7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed in lab
session. Design solutions of problems discussed in each task and placesolution code in a folder specified
by your lab instructor.
Lab instructors are guided to help students learn how ACM problems work and provide students
with certain practice/home tasks on the pattern of ACM Problems.
Create a Stack of integer using arrays. Give the following menu to the user and then implement the given
operations:
Sample Input:
Stack
3 2 6 7 8 13
Sample Output:
36
26
67
78
813
13-1
7.4Out comes
After completing this lab, student will be able to understand and develop programs related stacks using
static and dynamic arrays and using stack as class template in C++ using Microsoft Visual Studio 2017
environment.
7.5Testing
Sample Sample
Inputs-1 Outputs-1
Push elements for stack:
2
3
4
5
8
Enter number of elements to be popped from
stack1: 3
Elements of stack popped:
8
5
4
Sample Sample
Inputs-1 Outputs-1
Enter expression:
{{{
Can't be made balanced using reversals
Enter expression:
{{{{
2
Enter expression:
{{{{}}
1
Sample Sample
Inputs-1 Outputs-1
Input : abaabcdabf
new sequence is:
acdab
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student has
finished the complete/partial task(s).
1. http://www.slideshare.net/nita23arora/stacks-queues-presentation