0% found this document useful (0 votes)
23 views33 pages

DSA Ch5 (Stack)

Uploaded by

reemjawedunar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views33 pages

DSA Ch5 (Stack)

Uploaded by

reemjawedunar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structure & Algorithms

Chapter 5:
Stack
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?
Use of Stack
• Examples of uses of stack include- traversing and
evaluating prefix, infix and postfix expressions
• Consider the expression A+B:
• There are few important things to consider here:
– Firstly, + operator requires two operators or in other words “+”
is a binary operator
– Secondly, in the expression A+B, the one operand A is on left
of the operator while the other operand B is on the right side.
• This kind of expressions where the operator is present between two
operands called infix expressions. We take the meanings of this
expression as to add both operands A and B.
Use of Stack
• There are two other ways of writing expressions:
– We could write +AB, the operator is written before the
operands A and B.
• These kinds of expressions are called Prefix Expressions
– We can also write it as AB+, the operator is written after
operand A and B.
• This expression is called Postfix expression.
• The pre and post refer to the position of the
operator in the expression
Use of Stack
• Suppose we have following expression
• A+B*C (Task: convert it into postfix expression)

• (A+B)*C (Task: convert it into postfix expression)


• Find Prefix and postfix
Assignment
• How to evaluate expressions with stack
• Hint: notations
• Submit in hand written form

You might also like