You are on page 1of 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING


Department of Computer Science

Lab Manual 11
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 11: Stacks in C++

Instructors:
Mr. Awais Mehmood
awais.mehmood@uettaxila.edu.pk

&
Mr. M. Faheem Saleem
muhammad.faheem@uettaxila.edu.pk

CS201: Data Structure and Algorithms Page 1


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Lab Manual 11
Introduction
This lab is about stacks of C++ in arrays.
Objectives
The objective of this session is to understand the various operations on stacks in C++ using arrays.
Tools/Software Requirement
Dev C++
Goals for today’s lab:
• Perform operations related to stacks in C++ using arrays.

Stacks:-

Suppose that you have a program with several functions. To be specific, suppose that you have the
functions A, B, C, and D in your program. Now suppose that function A calls function B, function
B calls function C, and function C calls function D. When function D terminates, control goes back
to function C; when function C terminates, control goes back to function B; and when function B
terminates, control goes back to function A. During program execution, how do you think the
computer keeps track of the function calls? What about recursive functions? How does the
computer keep track of the recursive calls?

CS201: Data Structure and Algorithms Page 2


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

This section discusses the data structure called the stack, which the computer uses to implement
function calls. You can also use stacks to convert recursive algorithms into non recursive
algorithms, especially recursive algorithms that are not tail recursive. Stacks have numerous other
applications in computer science. After developing the tools necessary to implement a stack, we
will examine some applications of stacks.

A stack is a list of homogenous elements in which the addition and deletion of elements occurs
only at one end, called the top of the stack. For example, in a cafeteria, the second tray in a stack
of trays can be removed only if the first tray has been removed. For another example, to get to
your favorite computer science book, which is underneath your math and history books, you must
first remove the math and history books. After removing these books, the computer science book
becomes the top book—that is, the top element of the stack. Figure below shows some examples
of stacks.

The elements at the bottom of the stack have been in the stack the longest. The top element of the
stack is the last element added to the stack. Because the elements are added and removed from one
end (that is, the top), it follows that the item that is added last will be removed first. For this reason,
a stack is also called a Last In First Out (LIFO) data structure.

Stack:

A data structure in which the elements are added and removed from one end only; a Last In First
Out (LIFO) data structure.

Now that you know what a stack is, let us see what kinds of operations can be performed on a
stack. Because new items can be added to the stack, we can perform the add operation, called push,
to add an element onto the stack. Similarly, because the top item can be retrieved and/or removed
from the stack, we can perform the operation top to retrieve the top element of the stack, and the
operation pop to remove the top element from the stack.

CS201: Data Structure and Algorithms Page 3


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

The push, top, and pop operations work as follows: Suppose there are boxes lying on the floor that
need to be stacked on a table. Initially, all of the boxes are on the floor and the stack is empty.

First we push box A onto the stack. After the push operation, the stack is as shown in Figure
below.

We then push box B onto the stack. After this push operation, the stack is as shown in Figure (b).
Next, we push box C onto the stack. After this push operation, the stack is as shown in Figure (c).
Next, we look at the top element of the stack. After this operation, the stack is unchanged and
shown in Figure (d). We then push box D onto the stack. After this push operation, the stack is as
shown in Figure (e). Next, we pop the stack. After the pop operation, the stack is as shown in
Figure (f).

Implementation of Stacks as Arrays:-

Because all the elements of a stack are of the same type, you can use an array to implement a stack.
The first element of the stack can be put in the first array slot, the second element of the stack in
the second array slot, and so on. The top of the stack is the index of the last element added to the
stack.

CS201: Data Structure and Algorithms Page 4


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

In this implementation of a stack, stack elements are stored in an array, and an array is a random
access data structure; that is, you can directly access any element of the array. However, by
definition, a stack is a data structure in which the elements are accessed (popped or pushed) at only
one end—that is, a Last In First Out data structure. Thus, a stack element is accessed only through
the top, not through the bottom or middle. This feature of a stack is extremely important and must
be recognized in the beginning.

Algorithm for pushing onto the Stack:-

PUSH(STACK,TOP,MAXSTK,ITEM)

This procedure pushes an ITEM onto the stack.

1. [Stack already filled?]


If TOP = MAXSTK then Print OVERFLOW and Return
2. Set TOP = TOP + 1 [Increases TOP by 1]
3. Set STACK[TOP] = ITEM [Inserts ITEM into new TOP position]
4. Return.

Algorithm for popping from the Stack:-

This procedure deletes the top elements of STACK and assigns it to the variable ITEM.

1. [Stack has an ITEM to be removed?]


If TOP = 0 then Print UNDERFLOW and Return.
2. Set ITEM = STACK[TOP] [Assign TOP element to ITEM]
3. Set TOP = TOP-1 [Decreases TOP by 1]
4. Return.

Lab Task:-

Write down the code to push and pop the values from the stack using
arrays. Create the menu as follow.

CS201: Data Structure and Algorithms Page 5


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Stacks using Linked List:-

In this section we will discuss the representation of stacks using one way list. The advantages of
linked list over arrays are already known to us. It is in this perspective that one appreciates the use
of a linked list for stacks in comparison to that of arrays.

The linked representation of stack commonly termed linked stack is a stack that is implemented
using singly linked list. The “info” field of the node contain the data portion of the node and “link”
field points to the neighboring element on the stack. The START pointer of the linked list behaves
as the top pointer variable of the stack and the NULL pointer of the last node in the list signals to
the bottom of the stack.

Following figure shows empty and non-empty stack linked list.

CS201: Data Structure and Algorithms Page 6


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

PUSH Operation on to the Stack:-

Following figure shows the stack before PUSH operation.

Following figure shows the steps of pushing onto the stack.

CS201: Data Structure and Algorithms Page 7


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Algorithm for pushing onto the stack:-

PUSH_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)

This procedure pushes an ITEM into a linked stack.

1. [Available Space?] If AVAIL = NULL then Write OVERFLOW and


Exit.
2. [Remove First Node from the Avail List]
Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM [Copies ITEM into the new node]
4. Set LINK[NEW]=TOP [New node points to the original top node
in the stack.]
5. Set TOP=NEW [Reset TOP to point to the new node at the top
of the stack]
6. Exit.

POP Operation on the Stack:-

Following figure shows the stack before POP operation.

CS201: Data Structure and Algorithms Page 8


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Following figure shows the steps of POP operation on the stack.

Algorithm for popping from the Stack:-

This procedure deletes the top elements of STACK and assigns it to the variable ITEM.

5. [Stack has an ITEM to be removed?]


If TOP = NULL then Print UNDERFLOW and Return.
6. Set ITEM = INFO[TOP] [Copies the TOP element of the stack
into ITEM]

CS201: Data Structure and Algorithms Page 9


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

7. Set TEMP=TOP and TOP=LINK[TOP] [Remember the old value of the


TOP pointer in TEMP and reset TOP to point to the next element
in the stack]
8. [Return deleted node to AVAIL list]
Set LINK[TEMP]=AVAIL and AVAIL=TEMP
9. Return.

Lab Task:-

Write down the code to push and pop the values from the stack using
linked list. Create the menu as follow.

Home Tasks:

1. Get string input from user, and use stack to write it backwards.
2. Get number input from user, and use stack to write it backwards.
3. Evaluate an expression to check if it has balanced parentheses using stacks:

Algorithm

Step 1: Define a stack to hold brackets

Step 2: Traverse the expression from left to right

CS201: Data Structure and Algorithms Page 10


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Step 2.1: If the character is opening bracket (, or { or [, then


push it into stack

Step 2.2: If the character is closing bracket ), } or ] Then pop


from stack, and if the popped character is matched with the
starting bracket then it is ok. otherwise they are not balanced.

Step 3: After traversal if the starting bracket is present in the


stack then it is not balanced.

CS201: Data Structure and Algorithms Page 11

You might also like