CS2320: DATA
STRUCTURES &
ALGORITHMS
Overview
◻ Stacks and Queues
Data structure holding a collection of objects, ordered by when they were inserted into the structure.
Stack can access newest object.
Queue (رﻮﺑﺎط )can access oldest object.
Stacks
3
◻ A stack is a linear collection whose elements are
added and removed from the same end
◻ Stacks are processed in a last in, first out (LIFO)
manner
◻ Usually, stacks are depicted vertically, and we
refer to the top of the stack as the end to which elements are added and removed
Stack Operations
4
◻ push: places an element onto the top of a stack
◻ pop: removes an element from the top of the stack
◻ peek: which retrieves (copies) a value from the top of the stack without removing it
◻ isempty: an operation to determine whether or not the stack is empty
The Stack Interface
The Stack Interface
◻ Iast-in, first-out or LIFO policy
Last item pushed on the stack is next item popped off the stack.
Example:
■ A stack of plates.
The Stack Interface
Push
8
◻ Push means place a new data element at the top of the stack
17 5 11
Push (cont.)
9
◻ Push means place a new data element at the top of the stack
17 5 11 3
Push (cont.)
10
◻ Push means place a new data element at the top of the stack
17 5 11 3
Push (cont.)
11
◻ Push means place a new data element at the top of the stack
17 5 11 3
Pop
12
◻ Pop means take a data element off the top of the stack
17 5 11 3
Pop (cont.)
13
◻ Pop means take a data element off the top of the stack
17 5 11 3
Pop (cont.)
14
◻ Pop means take a data element off the top of the stack
17 5 11 3
Pop (cont.)
15
◻ Pop means take a data element off the top of the stack
17 5 11
Peek
16
◻ Peek means retrieve the top of the stack without removing it
17 5 11 3
Peek (cont.)
17
◻ Peek means retrieve the top of the stack without removing it 17 5 11 3 3
Peek (cont.)
18
◻ Peek means retrieve the top of the stack without removing it 17 5 11 3 3
Linked-List Stack
19
◻ Stacks can be implemented with a linked list ◻ The front node is the top of the stack
Linked-List Stack (cont.)
20
top
To pop, we remove the node at the front of the linked list, and return the element to the client…
Linked-List Stack (cont.)
21
top
To pop, we remove the node at the front of the linked list, and return the element to the client…
Linked-List Stack (cont.)
22
top
To push, we place the new element in a node and insert it at the front of the linked list…
The Program (Call) Stack
◻ When a method is called
Runtime environment creates activation record Shows method's state during execution
◻ Activation record pushed onto the program stack (Java stack)
Top of stack belongs to currently executing method Next method down is the one that called current method
The Program Stack
The program stack at 3 points in time; (a) when main begins execution; (b) when methodA begins execution, (c) when methodB begins execution.
Evaluating Postfix Expressions
25
◻ Before looking further at the implementation of a stack, let's first see how one might be used
◻ Arithmetic operations are traditionally written in
infix notation, meaning that the operator is placed
between its operands in the form
<operand> <operator> <operand>
◻ When evaluating infix expressions, we rely on
precedence rules to determine the order of operator evaluation
◻ In a postfix expression, the operator comes after its
two operands
Evaluating Postfix Expressions
26
◻ The process of evaluating a postfix expression can
be stated simply:
scan from left to right,
apply each operation to the two previous operands immediately preceding it and
replace the operator with the result
◻ Consider the infix expression: 4 + 5 * 2
Evaluating Postfix Expressions
27
◻ Consider the design of a program that can compute
the result of a postfix expression
◻ The evaluation rule relies on being able to retrieve
the previous two operands whenever we encounter an operator
◻ A large postfix expression will have many operators and operands to manage
◻ A stack is the perfect collection to use in this
Evaluating Postfix Expressions
28
◻ Solution algorithm:
scan the expression from left to right, identifying each token as an operator or operand
if the scanned token is an operand, push it onto the stack if the scanned token is an operator
■ pop the top two elements off the stack, ■ apply the operation to them, and
■ push the result onto the stack
◻ If we reach the end of the expression the remaining
element on the stack is the result of the expression (otherwise the expression was not well formed)
Using a Stack to Evaluate a Postfix
Expression
29
Given the expression: 7 4 -3 * 1 5 + / *
7 top 4 -3 * 1 5 + / -12 6 -2 -14 *
Infix-to-Postfix Algorithm
Symbol in
Infix Action
Operand Append to end of output expression Operator ^ Push ^ onto stack
Operator +,-, *, or /
Pop operators from stack, append to output expression until stack empty or top has
lower precedence than new operator. Then push new operator onto stack
Open
parenthesis
Push ( onto stack Close
parenthesis
Pop operators from stack, append to output expression until we pop an open
parenthesis. Discard both parentheses.
Transforming Infix to Postfix
Steps to convert the infix expression
a / b * ( c + ( d – e ) ) to postfix form.
Evaluating Postfix Expression
Fig. 21-10 The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4
Evaluating Postfix Expression
Fig. 21-11 The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4 and c is 3
Evaluating Infix Expressions
Two stacks during evaluation of a + b * c when a = 2, b = 3, c = 4; (a) after reaching end of expression;
(b) while performing multiplication; (c) while performing the addition
Performance and Limitations
◻ Performance
Let n be the number of elements in the stack The space used is O(n)
Each operation runs in time O(1)
◻ Limitations
The maximum size of the stack must be defined a priori and cannot be changed
Trying to push a new element into a full stack causes an implementation-specific exception
Stacks 35
QUEUES
36
Definition: A sequence of elements of the same type. The first stored element is first accessible.
The structure is known also under the name FIFO - first in first out
Basic operations
37
EnQueue
: store a data item at
the end of the queue
DeQueue
: retrieve a data item
The Queue Interface
◻ Queue
Very similar to a stack.
Items are inserted in one end (the back) and removed from the other end (the front).
first-in, first-out, or FIFO
The Queue Interface
Array Implementation of a Queue
40
◻ Similar to the linked-list queue, there are two data
members called front and back, but they are indexes into an Array instead of pointers
◻ When enqueuing, the back index is incremented,
Enqueue / Dequeue
41
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
42
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
43
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
44
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
45
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
46
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
47
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
48
0 1 2 3 4 5 6 7 front back
Enqueue / Dequeue (cont.)
49
0 1 2 3 4 5 6 7
front back
50
0 1 2 3 4 5 6 7
front back
DEQUEUE
51
0 1 2 3 4 5 6 7
front back
DEQUEUE
52
0 1 2 3 4 5 6 7
front back
DEQUEUE
53
0 1 2 3 4 5 6 7
front back
DEQUEUE
54
0 1 2 3 4 5 6 7
front back
ENQUEUE
55
0 1 2 3 4 5 6 7
front back
ENQUEUE
56
0 1 2 3 4 5 6 7
front back
DEQUEUE
57
0 1 2 3 4 5 6 7
front back
DEQUEUE
58
0 1 2 3 4 5 6 7
front back
DEQUEUE
59
0 1 2 3 4 5 6 7
front back
ENQUEUE
60
0 1 2 3 4 5 6 7
front back
ENQUEUE
?
61
0 1 2 3 4 5 6 7
front back
ENQUEUE We could double the size of the array
here.
62
0 1 2 3 4 5 6 7
front back
ENQUEUE
But if we keep doing this, we may have a million elements in the Array, but only a few at the end are used!
63
0 1 2 3 4 5 6 7
front back
ENQUEUE
We handle this problem by having the back wrap around to the beginning of the array.
64
0 1 2 3 4 5 6 7
front back
ENQUEUE
65
0 1 2 3 4 5 6 7
front back
The front also wraps to the beginning when it reaches the end of the array
How Do We Know When the Array is
Full?
66
◻ We may still need to double the capacity of the
array if it gets filled
◻ An array will be full when
back + 1 == front OR
A Full Array
67
0 1 2 3 4 5 6 7
front back
A Full Array
68
0 1 2 3 4 5 6 7
front back
If the next operation is ENQUEUE, the array capacity will need to be doubled
Implementing Queues with Links
69
◻ Because a queue is a linear collection, we can
implement a queue as a linked list of LinearNode
objects, as we did with stacks
◻ One important difference, however, is that we will
have to operate on both ends of the list
◻ So we'll add an additional reference variable (rear)
Dequeue Operation
70
front back
Dequeue Operation (cont.)
71
front back
Enqueue Operation
72
front back
Enqueue Operation (cont.)
73
front back
Summary
◻ Stacks and queues are collections of objects.
Stack follows a last-in, first-out policy.
■ Objects are pushed onto and popped off the top.
Queue follows a first-in, first-out policy.
■ Objects are added to the back and removed from the front.