Professional Documents
Culture Documents
Lab Manual 07 DSA PDF
Lab Manual 07 DSA PDF
Student Roll #
Department
Batch/Year/Section
Lab Instructor:
Ms. Humaira Anwer
10/04/2017
Lab Manual #7 Stacks
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.
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.
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:
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps:
If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.
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
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
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
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.
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:
Following steps can be used to insert a new node into 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
The following steps can be used to delete a node from 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
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:
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
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
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