chapter 05 compiled by: dr. mohammad omar alhawarat stacks & queues

74
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

Upload: rhoda-montgomery

Post on 28-Dec-2015

230 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

CHAPTER 05

Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

Page 2: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

2

Page 3: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

3

Stacks

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

Page 4: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

4

Stack Operations

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

empty: an operation to empty out a stack

Page 5: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

The Stack Interface5

Page 6: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

6

Page 7: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

The Stack Interface7

Page 8: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

8

Push

Push means place a new data element at the top of the stack

17

5

11

3

Page 9: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

9

Push (cont.)

Push means place a new data element at the top of the stack

17

5

11

3

Page 10: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

10

Push (cont.)

Push means place a new data element at the top of the stack

17

5

11

3

Page 11: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

11

Push (cont.)

Push means place a new data element at the top of the stack

17

5

11

3

Page 12: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

12

Pop

Pop means take a data element off the top of the stack

17

5

11

3

Page 13: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

13

Pop (cont.)

Pop means take a data element off the top of the stack

17

5

11

3

Page 14: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

14

Pop (cont.)

Pop means take a data element off the top of the stack

17

5

11

3

Page 15: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

15

Pop (cont.)

Pop means take a data element off the top of the stack

17

5

11

3

Page 16: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

16

Peek

Peek means retrieve the top of the stack without removing it

17

5

11

3

Page 17: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

17

Peek (cont.)

Peek means retrieve the top of the stack without removing it

17

5

11

3

3

Page 18: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

18

Peek (cont.)

Peek means retrieve the top of the stack without removing it

17

5

11

3

3

Page 19: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

19

Linked-List Stack

Stacks can be implemented with a linked list

The front node is the top of the stack

Page 20: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

20

Linked-List Stack (cont.)

top

To pop, we remove the node at the front of the linked list, and return the element to the client…

Page 21: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

21

Linked-List Stack (cont.)

top

To pop, we remove the node at the front of the linked list, and return the element to the client…

Page 22: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

22

Linked-List Stack (cont.)

top

To push, we place the new element in a node and insert it at the front of the linked list…

Page 23: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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

23

Page 24: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

24

Page 25: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

25

Evaluating Postfix Expressions

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

<operand> <operand> <operator>

Page 26: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

26

Evaluating Postfix Expressions

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

In postfix notation, it would be written as: 4 5 2 * +

Page 27: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

27

Evaluating Postfix Expressions

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 solution

Page 28: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

28

Evaluating Postfix Expressions

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)

Page 29: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

29

Using a Stack to Evaluate a Postfix Expression

Given the expression: 7 4 -3 * 1 5 + / *

7top

4

-3

*

1

5

+

/

-12

6

-2

-14

*

Page 30: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

30

Page 31: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

Transforming Infix to Postfix

Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form.

31

Page 32: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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

32

Page 33: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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

33

Page 34: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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

34

Page 35: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

Stacks 35

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

35

Page 36: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

36

QUEUES

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

Page 37: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

37

Basic operations

EnQueue : store a data item at the end of the queue

DeQueue : retrieve a data item from the beginning of the queue

Page 38: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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

38

Page 39: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

The Queue Interface39

Page 40: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

40

Array Implementation of a Queue

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, and when dequeuing, the front index is incremented

Page 41: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

41

Enqueue / Dequeue

0 1 2 3 4 5 6 7

front back

Page 42: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

42

Enqueue / Dequeue (cont.)

front back

DEQUEUE

0 1 2 3 4 5 6 7

Page 43: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

43

Enqueue / Dequeue (cont.)

front back

DEQUEUE

0 1 2 3 4 5 6 7

Page 44: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

44

Enqueue / Dequeue (cont.)

front back

DEQUEUE

0 1 2 3 4 5 6 7

Page 45: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

45

Enqueue / Dequeue (cont.)

front back

DEQUEUE

0 1 2 3 4 5 6 7

Page 46: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

46

Enqueue / Dequeue (cont.)

front back

ENQUEUE

0 1 2 3 4 5 6 7

Page 47: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

47

Enqueue / Dequeue (cont.)

0 1 2 3 4 5 6 7

front back

ENQUEUE

Page 48: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

48

Enqueue / Dequeue (cont.)

0 1 2 3 4 5 6 7

front back

ENQUEUE

Page 49: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

49

Enqueue / Dequeue (cont.)

0 1 2 3 4 5 6 7

front back

ENQUEUE

Page 50: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

50

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 51: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

51

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 52: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

52

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 53: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

53

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 54: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

54

0 1 2 3 4 5 6 7

front back

ENQUEUE

Enqueue / Dequeue (cont.)

Page 55: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

55

0 1 2 3 4 5 6 7

front back

ENQUEUE

Enqueue / Dequeue (cont.)

Page 56: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

56

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 57: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

57

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 58: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

58

0 1 2 3 4 5 6 7

front back

DEQUEUE

Enqueue / Dequeue (cont.)

Page 59: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

59

0 1 2 3 4 5 6 7

front back

ENQUEUE

Enqueue / Dequeue (cont.)

Page 60: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

60

0 1 2 3 4 5 6 7

front back

ENQUEUE

?

Enqueue / Dequeue (cont.)

Page 61: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

61

0 1 2 3 4 5 6 7

front back

ENQUEUE We could double the size of the array here.

Enqueue / Dequeue (cont.)

Page 62: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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!

Enqueue / Dequeue (cont.)

Page 63: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

Enqueue / Dequeue (cont.)

Page 64: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

64

0 1 2 3 4 5 6 7

frontback

ENQUEUE

Enqueue / Dequeue (cont.)

Page 65: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

65

0 1 2 3 4 5 6 7

frontback

The front also wraps to the beginning when it reaches the end of the array

Enqueue / Dequeue (cont.)

Page 66: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

66

How Do We Know When the Array is Full? 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 back + 1 == capacity AND front == 0

Page 67: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

67

A Full Array

0 1 2 3 4 5 6 7

frontback

Page 68: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

68

A Full Array

0 1 2 3 4 5 6 7

frontback

If the next operation is ENQUEUE, the array capacity will need to be doubled

Page 69: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

69

Implementing Queues with Links

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) that points to the last element in the list

Page 70: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

70

Dequeue Operation

front back

header

Page 71: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

71

Dequeue Operation (cont.)

front back

header

Page 72: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

72

Enqueue Operation

front back

header

Page 73: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

73

Enqueue Operation (cont.)

front back

header

Page 74: CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

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.

74