0% found this document useful (0 votes)
12 views46 pages

Chapter 3

This document summarizes key concepts about stacks, queues, and their implementation. It then describes using a stack to solve a mazing problem by traversing the maze and backtracking paths. The maze is represented as a 2D array, and a stack is used to store path positions and directions. The algorithm iterates through possible moves, marking visited positions until reaching the exit. Analysis shows the worst case time complexity is O(mp) where m and p are the number of rows and columns.

Uploaded by

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

Chapter 3

This document summarizes key concepts about stacks, queues, and their implementation. It then describes using a stack to solve a mazing problem by traversing the maze and backtracking paths. The maze is represented as a 2D array, and a stack is used to store path positions and directions. The algorithm iterates through possible moves, marking visited positions until reaching the exit. Analysis shows the worst case time complexity is O(mp) where m and p are the number of rows and columns.

Uploaded by

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

Chapter 3

Stacks and Queues


Yi-Fen Liu
Department of IECS, FCU

References:
- E. Horowitz, S. Sahni and S. Anderson-Freed, Fundamentals of Data Structures (2nd Edition)
- Slides are credited from Prof. Chung, NTHU
Outline
• The Stack Abstract Data Type
• The Queue Abstract Data Type
• A Mazing Problem
• Evaluation of Expressions

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 2


THE STACK ADT
The Stack ADT (1)
• A stack is an ordered list in which insertions and
deletions are made at one end called the top
– If we add the elements A, B, C, D, E to the stack, in that
order, then E is the first element we delete from the
stack
• A stack is also known as a Last-In-First-Out (LIFO) list

C
B
A top
The Stack ADT (2)
• System stack
– Stack frame of function call

fp: a pointer to current stack frame

Previous frame pointer fp


Return address a1
Local variables
Previous frame pointer fp Previous frame pointer
Return address main Return address main

system stack before a1 is invoked system stack after a1 is invoked

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 5


The Stack ADT (3)
The Stack ADT (4)
• Implementation: using array

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 7


The Stack ADT (5)
stack

item
Top

stack

item Top
THE QUEUE ADT
The Queue ADT (1)
• A queue is an ordered list in which all insertion take
place one end, called the rear and all deletions take
place at the opposite end called the front
• First-In-First-Out (FIFO) list
• If we insert the elements A, B, C, D, E, in that order,
then A is the first element we delete from the
queue as a

rear
rear C
rear B front
rear A front
The Queue ADT (2)
The Queue ADT (3)
• Using a one dimensional array and two
variables, front and rear

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 12


The Queue ADT (4)
Problem: there may be available space when IsFullQ is true i.e. movement is required.
Example: Job scheduling
• The figure illustrates how an operating system might process jobs if it used a
sequential representation for its queue.

• As jobs enter and leave the system, the queue gradually shift to right.
• In this case, queue_full should move the entire queue to the left so that the
first element is again at queue[0], front is at -1, and rear is correctly positioned.
– Shifting an array is very time-consuming, queue_full has a worst case
complexity of O(MAX_QUEUE_SIZE).

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 14


Circular Queue (1)
• We can obtain a more
efficient representation if
we regard the array
queue[MAX_QUEUE_SIZE]
as circular
– front: one position
counterclockwise from the
first element
– rear: current end
• Problem: one space is left
when queue is full

Data Structures (Ch3) - 15


Circular Queue (2)
• Implementing addq and deleteq for a circular
queue is slightly more difficult since we must
assure that a circular rotation occurs

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 16


Circular Queue (3)

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 17


A MAZING PROBLEM
A Mazing Problem (1)
• Representation of the maze
entrance
– The most obvious choice is a two
dimensional array 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
• 0s the open paths and 1s the barriers 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1

– Notice that not every position has 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1


1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
eight neighbors 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
• To avoid checking for these border 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1

conditions we can surround the maze 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1


1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
by a border of ones. Thus an mp 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
maze will require an (m+2)  (p+2) 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
array 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1

• The entrance is at 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

position [1][1] and the exit at [m][p]


exit
A Mazing Problem (2)
• If X marks the spot of our current location, maze[row][col],
then the figure shows the possible moves from this position
A Mazing Problem (3)
• A possible implementation
– Predefinition: the possible directions to move in an
array as in the figure
typedef struct {
short int vert;
short int horiz;
} offsets;
offsets move[8]; /*array of moves for each direction*/
– If we are at position, maze[row][col], and we wish to
find the position of the next move
• next_row = row + move[dir].vert;
• next_col = col + move[dir].horiz;
A Mazing Problem (4)
• Initial attempt at a maze traversal algorithm
– Maintain a second two-dimensional array, mark,
to record the maze positions already checked
– use stack to keep pass history
#define MAX_STACK_SIZE 100 /*maximum
stack size*/
typedef struct {
short int row;
short int col;
short int dir;
} element;
element stack[MAX_STACK_SIZE];

Data Structures (Ch3) - 22


A Mazing Problem (5)
R: row
R1C1D1 C: col
R4 C12
R3 C14 D 5
2
D: dir
R3 C13 D 6
3
Pop out
R2 C12 D 3
R2 C11 D 2
maze[1][1]: entrance Initially set mark[1][1]=1
R1 C10 D 3
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
R1C9D2 1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 █
█ █
█  █ █ █ 
█  ██
█ █ █ █ █ █ █
R1C8D2 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 █ █
█ █
█ █ █
█ █ █ █
█ 
█ █ █ █ █
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 █ █ █ █ █
█ 
█ █ █ █ █ █ 
█ 
█ █ █ █
R2C7D1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 █ █ █ █ █ █ █ █ █ █ █
█ █ █ 
█ █ █
R3C6D1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
R3C5D2 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

R2C4D3 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
R1C5D5
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
R1C4D2 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

R1C3D2 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
R2C2D1
R1C1D1
3 maze[11][15]: exit
A Mazing Problem (7)
• Review of add and delete to a stack
A Mazing Problem (8)
A Mazing Problem (9)

• Analysis:
– The worst case of computing time of path is
O(mp), where m and p are the number of rows
and columns of the maze respectively

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 27


A Mazing Problem (10)
• The size of a stack
mp  m / 2 * p, or  p / 2 * m

0 0 0 0 0 1
1 1 1 1 1 0
1 0 0 0 0 1
0 1 1 1 1 1
1 0 0 0 0 1
1 1 1 1 1 0
1 0 0 0 0 1
0 1 1 1 1 1
1 0 0 0 0 0 m*p
Simple maze with a long path
EVALUATION OF EXPRESSIONS
Evaluation of Expressions (1)
• The representation and evaluation of
expressions
((rear+1==front) || ((rear==MAX_QUEUE_SIZE-1) && !front))
x = a/b - c+d*e - a*c
– If we examine expressions, we notice that they
contains
• operators: ==, +, -, ||, &&, !
• operands: rear, front, MAX_QUEUE_SIZE
• parentheses: ( )

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 30


Evaluation of Expressions (2)
• Understanding the meaning of these or any other
expressions and statements
– Assume a = 4, b = c = 2, d = e = 3 in the statement x =
a/b - c+d*e - a*c, finding out the value of x
• Interpretation 1
((4/2)-2)+(3*3)-(4*2) = 0+9-8 = 1
• Interpretation 2
(4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.66666…
– We would have written the statement differently by
using parentheses to change the order of evaluation
• x = ((a/(b - c+d))*(e - a)*c

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 31


Precedence Hierarchy for C (1)
• How to generate the machine instructions corresponding to a
given expression?
• precedence rule + associative rule
Precedence Hierarchy for C (2)

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 33


Evaluating Postfix Expressions (1)
• The standard way of writing expressions is known as infix
notation
– binary operator in-between its two operands
• Infix notation is not the one used by compilers to evaluate
expressions
• Instead compilers typically use a parenthesis-free notation
referred to as postfix
– Postfix: no parentheses, no precedence
Evaluating Postfix Expressions (2)
• Evaluating postfix expressions is much simpler than the
evaluation of infix expressions
– There are no parentheses to consider
– To evaluate an expression we make a single left-to-right
scan of it
– We can evaluate an
expression easily by
using a stack
– The figure shows this
processing when the
input is nine
character string
6 2/3-4 2*+

Data Structures (Ch3) - 35


Evaluating Postfix Expressions (3)
• Representation
– We now consider the representation of both
the stack and the expression

Yi-Fen Liu, FCU IECS Data Structures (Ch3) - 36


Evaluating Postfix Expressions (4)
• Get Token
Evaluating Postfix Expressions (5)
• Function to evaluate a postfix expression

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 38


Evaluating Postfix Expressions (6)
Evaluating Postfix Expressions (7)
• string: 6 2/3-4 2*+
• make a single left-to-right scan

6 2 / 3 - 4 2 * +

not an
notoperator,
an annotoperator,
operator, an operator,
an operator,
pushpop
pushtwo
into pop
thevalue,
intopush
thetwo
stack into
value,
stackthe stack 3
2
calculatecalculate
it and it and
6/2-3
6/2
6 top
push thepush
resultthe
into
result
the stack
into the stack

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 40


Evaluation of Expressions (1)
• The idea for producing a postfix expression from
an infix one
Ex: Trans a / b - c + d * e - a * c To postfix
(1) Fully parenthesize expression
a/b-c+d*e-a*c
((((a / b) - c) + (d * e)) - (a * c))
(2) All operators replace their corresponding right parentheses
((((a / b) - c) + (d * e)) - (a * c))
((((a b / )c - ) + (d e *)) - ( a c *))
((((a b /) c -) (d e *) +) (a c *) -)
(3)Delete all parentheses
ab/c-de*+ac*-
The order of operands is the same in infix and postfix
Evaluation of Expressions (2)
• Algorithm to convert from infix to postfix
– Assumptions
• operators: (, ), +, -, *, /, %
• operands: single digit integer or variable of one character
– Scan string from left to right
– Operands are taken out immediately
– Operators are taken out of the stack as long as their
in-stack precedence (isp) is higher than or equal to the
incoming precedence (icp) of the new operator

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 42


Evaluation of Expressions (3)
• ‘(‘ has low isp, and high icp
– op ( ) + - * / % eos
– isp 0 19 12 12 13 13 13 0
– icp 20 19 12 12 13 13 13 0

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 43


Evaluation of Expressions (4)
Example [Simple expression]: Simple expression a+b*c,
which yields abc*+ in postfix

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 44


Evaluation of Expressions (5)
Example : The expression a*(b+c)*d, which yields abc+*d* in
postfix

Yi-Fen Liu, FCU IECS (2018) Data Structures (Ch3) - 45


Evaluation of Expressions (6)
• Complexity: (n)
– The total time spent
here is (n) as the
number of tokens that
get stacked and
unstacked is linear in n
– where n is the number
of tokens in the
expression

46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy