Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Student Name

Student Roll #

Department

Batch/Year/Section

For Lab. Instructor

Marks Signature _________________________

Course: Data Structures & Algorithms

Lab Instructor:
Ms. Humaira Anwer

Department of Computer Science & Information


Technology

Khwaja Fareed University of Engineering


& Information Technology,
(KFUEIT)
Abu Dhabi Road,
Rahim Yar Khan.

10/04/2017
Lab Manual #7 Stacks

Lab Manual # 7
Stacks

KFUEIT Department of CS/IT


88
Lab Manual #7 Stacks

7.1. Objective
After performing this lab you would be able to:
1. Understand the concept of stacks.
2. Understand the basic algorithms of PUSH, POP and PEEK in stacks.
3. Understand basic logic of stacks and to implement its algorithms in C++ language
using arrays and linked lists.
7.2. Stacks
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It
is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of
plates, etc.

Fi g. 7.1. Gra phi ca l Repres enta ti on of a Node i n ci rcul a rl y Li nked Li s t

A real-world stack allows operations at one end only. For example, we can place or remove a
card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations
at one end only. At any given time, we can only access the top element of a stack.

This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last, is accessed first. In stack terminology,
insertion operation is called PUSH operation and removal operation is called POP operation.

7.3. Stack Representation


The following diagram depicts a stack and its operations −

Fi g. 7.2. Sta ck Repres enta ti on

KFUEIT Department of CS/IT


89
Lab Manual #7 Stacks

As per the above illustration, following are some important points to be considered:
i. A stack can be implemented by means of Array, Structure, Pointer, and Linked
List.
ii. Stack can either be a fixed size one or it may have a sense of dynamic resizing.
iii. All insertions, deletions i.e. all operations take place from top only.
iv. Initially when stack is empty top is NULL.
v. The situation when stack is empty is known as stack underflow.
vi. The situation when stack is full is known as stack overflow.
7.3.1. Basic Operations

Stack operations may involve initializing the stack, using it and then de-initializing it. Apart
from these basics, a stack is used for the following four primary operations:

1. PUSH–Adds an element at top of the stack.


2. POP – Deletes an element from top of the stack.
3. PEEK– Get the top data element of the stack, without removing it.
4. Display − Displays the complete stack.

7.4. Stack Implementation Using Arrays


7.4.1. Push Operation

The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps:

 Step 1 − Checks if the stack is full.


 Step 2 − If the stack is full, produces an error and exit.
 Step 3 − If the stack is not full, increments top to point next empty space.
 Step 4 − Adds data element to the stack location, where top is pointing.
 Step 5 − Returns success.

Fi g. 7.3. Sta ck PUSH Opera ti on

KFUEIT Department of CS/IT


90
Lab Manual #7 Stacks

If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.

The aforementioned procedure is accomplished in algorithm 7.4.2

7.4.2. Algorithm: PUSH (ITEM)


Description: This algorithm INSERTS an ITEM always at the Top of
the stack. If Top = -1 then first element is added in the stack.

[This algorithm always PUSHES new ITEM at TOP of the STACK]

1. START
2. CHECK if TOP = UB then [Process to INSERT first NODE]
a. Print “Stack Overflow”
3. ELSE [Process to INSERT AT TOP]
a. SET STACK[TOP]=ITEM
b. Increment TOP
4. EXIT

7.4.3. POP Operation


Accessing the content while removing it from the stack, is known as a Pop Operation. In an
array implementation of pop() operation, the data element is not actually removed, instead
top is decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.

A Pop operation may involve the following steps −

 Step 1 − Checks if the stack is empty.


 Step 2 − If the stack is empty, produces an error and exit.
 Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
 Step 4 − Decreases the value of top by 1.
 Step 5 − Returns success.

Fi g. 7.4. Sta ck POP Opera ti on

KFUEIT Department of CS/IT


91
Lab Manual #7 Stacks

The aforementioned procedure is accomplished in algorithm 7.4.4.

7.4.4. Algorithm: POP( )


Description: This algorithm DELETES always from TOP of the STACK.

[This algorithm always deletes from TOP of the STACK]

1. START
2. CHECK if TOP = -1 then [Process to CHECK if STACK is empty]
a. PRINT “Stack Underflow”
3. ELSE
a. SET Temp = STACK[TOP] [Process to DELETE from TOP]
b. Decrement TOP
c. PRINT “Item Popped from Stack”
4. EXIT

7.4.5. PEEK Operation


Algorithm of PEEK Operation is:

7.4.6. Algorithm: PEEK ( )


Description: This algorithm PRINTS value from TOP of the STACK

1. START
2. CHECK if TOP = -1 then [Process to CHECK if STACK is empty]
a. PRINT “Stack Underflow”
3. ELSE
a. PRINT STACK[TOP]
4. EXIT

7.5. Drawback of Stack Implementation using Arrays


The major problem with the stack implemented using array is, it works only for fixed number
of data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using array is not suitable, when we don't know the
size of data which we are going to use. A stack data structure can be implemented by using
linked list data structure. The stack implemented using linked list can work for unlimited
number of values. That means stack implemented using linked list works for variable size of
data. So, there is no need to fix the size at the beginning of the implementation. The Stack
implemented using linked list can organize as many data values as we want.

7.6. Stack Implementation Using Linked List


Stack implementation using linked list makes use of singly linked list and is simply
accomplished by following the LIFO principle. In simple words it can be said that a singly
linked list that supports insertion and deletion from head only (TOP in stack terminology)
becomes stack.

KFUEIT Department of CS/IT


92
Lab Manual #7 Stacks

In linked list implementation of a stack, every new element is inserted as 'top' element. That
means every newly inserted element is pointed by 'top'. Whenever we want to remove an
element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to
its next node in the list. The next field of the first element must be always NULL.

Fi g. 7.5. Sta ck Us i ng Li nked Li s t

In above Fig. 7.5, the last inserted node is 99 and the first inserted node is 25. The order of
elements inserted is 25, 32,50 and 99.

Stack implementation using linked list follows all other principles of singly linked list some
of them are:

i. Initially TOP is set to NULL to mark beginning of the stack.


ii. Each node in stack implementation using linked list points to next node in forward
navigation only.
iii. Last node points to NULL to mark end of the list.
7.6.1. PUSH Operation

Following steps can be used to insert a new node into the stack:

 Step 1: Create a new Node with given value.


 Step 2: Check whether stack is Empty (top = = NULL)
 Step 3: If it is Empty, then set new Node → next = NULL.
 Step 4: If it is Not Empty, then set new Node → next = top.
 Step 5: Finally, set top = new Node.

The aforementioned procedure is accomplished in algorithm 7.6.2.

KFUEIT Department of CS/IT


93
Lab Manual #7 Stacks

7.6.2. Algorithm: PUSH (ITEM)


Description: This algorithm INSERTS an ITEM always at the Top of
the stack. If Top = NULL then first element is added in the stack.

[This algorithm always PUSHES new ITEM at TOP of the STACK]

1. START
2. CHECK if TOP = NULL then [Process to INSERT first NODE]
a. SET TOP = new node
b. SET TOP->data =ITEM
c. SET TOP->next = NULL
3. ELSE [Process to INSERT AT TOP]
a. SET temp = new node
b. temp->data = ITEM
c. temp->next = TOP
d. TOP = temp
4. EXIT

7.6.3. POP Operation

The following steps can be used to delete a node from the stack...

 Step 1: Check whether stack is Empty (top = = NULL).


 Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and
terminate the function
 Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
 Step 4: Then set 'top = top → next'.
 Step 7: Finally, delete 'temp'.

The aforementioned procedure is accomplished in algorithm 7.6.4.

7.6.4. Algorithm: POP ( )

Description: This algorithm DELETES always from TOP of the STACK.

1. START
2. CHECK if TOP = NULL then
a. PRINT Stack Underflow
3. ELSE [Process to DELETE from TOP]
a. SET temp = TOP
b. TOP = TOP->next
c. delete temp
4. EXIT

7.6.5. PEEK Operation


7.6.6. Algorithm: PEEK ( )
Description: This algorithm PRINTS value from TOP of the STACK

KFUEIT Department of CS/IT


94
Lab Manual #7 Stacks

[This algorithm deletes Node from position entered by the User]

1. START
2. Check if HEAD=NULL [Process to Check if List is EMPTY]
a. PRINT “STACK UNDERFLOW”
3. ELSE
a. PRINT TOP-data
4. EXIT

7.6.7. Display

Displaying a stack means to show all of the elements in the stack in the same order in which
they are inserted. To display a stack:

 Step 1: Check whether stack is Empty (top == NULL).


 Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
 Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until
temp reaches to the first node in the stack (temp != NULL).

7.6.8. Algorithm: DISPLAY ( )


Description: This algorithm DISPLAYS all the ITEMS in the STACK in
by moving a pointer variable temp to the end of the STACK.

1. START
2. CHECK if TOP = NULL then
a. Print ”STACK IS EMPTY”
3. Else
a. SET temp = TOP
b. REPEAT steps i to iii while temp != NULL
i. PRINT temp->DATA [Prints DATA element of the all
NODES]
ii. SET temp = temp->NEXT
iii. END loop STEP b
4. EXIT

KFUEIT Department of CS/IT


95
Lab Manual #7 Stacks

LAB TASK 01

1. Write a single C++ code to implement all the below mentioned operations for a Stack
using arrays
i. PUSH
ii. POP
iii. PEEK
iv. DISPLAY
Code Area

KFUEIT Department of CS/IT


96
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


97
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


98
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


99
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


100
Lab Manual #7 Stacks

LAB TASK 02

2. Write a single C++ code to implement all the below mentioned operations for a Stack
using linked list
i. PUSH
ii. POP
iii. PEEK
iv. DISPLAY

Code Area

KFUEIT Department of CS/IT


101
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


102
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


103
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


104
Lab Manual #7 Stacks

KFUEIT Department of CS/IT


105

You might also like