Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Data Structure & Algorithms

Chapter 5:
Stack
Imran Ali Memon
IT Department
Contents
5.1 - Introduction
5.2 – Basic Operations
5.3 – Types of representation of stack in memory
5.4 – Push( ) Algorithm
5.5 – Pop( ) Algorithm
Introduction
Stack: (LIFO)
• A stack is a list of elements in which an element may
be added or deleted only at one end, called the top of
the stack.
• Anything added to the stack goes on the “top” of the
stack.
• Anything removed from the stack is taken from the
“top” of the stack.
• Things are removed in the reverse order from that in
which they were inserted (LIFO:Last In, First Out)
• Other names for stacks are piles and push-down lists.
Stack (LIFO)
Stack
AA BB CC DD
0 1 2 3 4 5 …… N-1 N

0 AA N
1 BB N-1
2 CC .
3 DD TOP
.
4 5
5 4
Count = 4
. 3 DD
Top = 3
. CC
2
N-1 BB
1
N 0 AA
Basic Operations
• There are two basic operations associated with
stack:
1. Push() is the term used to insert/add an element
into a stack.
2. Pop() is the term used to delete/remove an
element from a stack.
Push()
Pop()
The Towers of Hanoi
A Stack-based Application

– GIVEN: three poles


– a set of discs on the first pole, discs of different sizes, the smallest discs at
the top
– GOAL: move all the discs from the left pole to the right one.
– CONDITIONS: only one disc may be moved at a time.
– A disc can be placed either on an empty pole or on top of a larger disc.
There are two ways to represent
Stack in memory.
1. Array (Static)
2. Linked List (Dynamic)
Linked List Representation of Stack
(Dynamic)
DDD
3901

CCC 3901
2000
TOP = 3901
BBB 2000
Count = 4
1050

AAA 1050
1001

1001
Head
Linked List Representation of Stack (Dynamic)

NEW 2100

DDD 3901 push() DDD 2100 3901

CCC 3901 2000


CCC 3901 2000

BBB 1050
2000 1050 BBB 2000 1050

AAA 1050 1001 AAA 1050 1001


TOP = 3901 2100
1001
Count = 4 5 1001 Head

Head
Linked List Representation of Stack (Dynamic)

DDD 3901 pop() CCC 2000

CCC 3901 2000


BBB 2000 1050

BBB 1050
2000 1050
AAA 1050 1001

AAA 1050 1001


TOP = 3901 2000 1001

1001
Count = 4 3 Head
Head
Array Representation of Stacks
• Usually the stacks are represented in the computer
by a linear array.
• In the algorithms of pushing and popping an item
from the stacks, we have considered,
• A linear array STACK,
• A variable TOP which contain the location of the
top element of the stack
• A variable STACKSIZE / MAXSTACK which gives the
maximum number of elements that can be hold by
the stack.
Stacks conditions
• The condition Top = -1 / Null indicate that the stack is empty.
• The condition Top = 8 indicate that the stack is full.
• It will be overflow if Top = 8 & push() will be performed.
• It will be underflow if Top = -1 & pop() will be performed.
Array Representation of Stacks
Stack conditions

StackSize - 1 StackSize - 1 JJ StackSize - 1


StackSize - 2 StackSize - 2 II StackSize - 2
. .
. HH .
. .
TOP 5 5 GG 5
4 4 FF 4
3 EE 3 EE 3
2 DD 2 DD 2

1 CC 1 CC 1
0 BB 0 BB 0

Top = 3 Top = StackSize-1 An Empty Stack


Stack is full Top = -1 / NULL
Push Operation
Push Algorithm
Pop Operation
POP Algorithm
Stack Application (Evaluation of Expressions)
• X=a/b-c+d*e-a*c
a = 4, b = c = 2, d = e = 3
Interpretation 1:
((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1
Interpretation 2:
(4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666…
How to generate the machine instructions
corresponding to a given expression?
Assignment
• How to evaluate expressions with stack
• Hint: notations
• Submit in hand written form

You might also like