Download - Stacks and queues

Transcript
Page 1: Stacks and queues

Stacks and Queues

Trupti Agrawal 1

Page 2: Stacks and queues

Stacks

Linear list.

One end is called top.

Other end is called bottom.

Additions to and removals from the top end only.

Trupti Agrawal 2

Page 3: Stacks and queues

Stack Of Cups

Add a cup to the stack.

bottom

top

C

A

B

D

E

F

• Remove a cup from new stack.

• A stack is a LIFO list.

bottom

top

C

A

B

D

E

Trupti Agrawal 3

Page 4: Stacks and queues

Towers Of Hanoi/Brahma

64 gold disks to be moved from tower A to tower C

each tower operates as a stack

cannot place big disk on top of a smaller one

A B C

1234

Trupti Agrawal 4

Page 5: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

123

Trupti Agrawal 5

Page 6: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

12

3

Trupti Agrawal 6

Page 7: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

1 2 3

Trupti Agrawal 7

Page 8: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

1 23

Trupti Agrawal 8

Page 9: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

123

Trupti Agrawal 9

Page 10: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

123

Trupti Agrawal 10

Page 11: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

12

3

Trupti Agrawal 11

Page 12: Stacks and queues

Towers Of Hanoi/Brahma

3-disk Towers Of Hanoi/Brahma

A B C

123

• 7 disk movesTrupti Agrawal 12

Page 13: Stacks and queues

Recursive Solution

n > 0 gold disks to be moved from A to C using B

move top n-1 disks from A to B using C

A B C

1

Trupti Agrawal 13

Page 14: Stacks and queues

Recursive Solution

move top disk from A to C

A B C

1

Trupti Agrawal 14

Page 15: Stacks and queues

Recursive Solution

move top n-1 disks from B to C using A

A B C

1

Trupti Agrawal 15

Page 16: Stacks and queues

Recursive Solution

moves(n) = 0 when n = 0

moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0

A B C

1

Trupti Agrawal 16

Page 17: Stacks and queues

Towers Of Hanoi/Brahma

moves(64) = 1.8 * 1019 (approximately)

Performing 109 moves/second, a computer would take about 570 years to complete.

At 1 disk move/min, the monks will take about 3.4 * 1013 years.

Trupti Agrawal 17

Page 18: Stacks and queues

Rat In A Maze

Trupti Agrawal 18

Page 19: Stacks and queues

Rat In A Maze

Move order is: right, down, left, up

Block positions to avoid revisit.Trupti Agrawal 19

Page 20: Stacks and queues

Rat In A Maze

Move order is: right, down, left, up

Block positions to avoid revisit.

Trupti Agrawal 20

Page 21: Stacks and queues

Rat In A Maze

Move backward until we reach a square from which a forward move is possible.

Trupti Agrawal 21

Page 22: Stacks and queues

Rat In A Maze

Move down.

Trupti Agrawal 22

Page 23: Stacks and queues

Rat In A Maze

Move left.

Trupti Agrawal 23

Page 24: Stacks and queues

Rat In A Maze

Move down.

Trupti Agrawal 24

Page 25: Stacks and queues

Rat In A Maze

Move backward until we reach a square from which a forward move is possible.

Trupti Agrawal 25

Page 26: Stacks and queues

Rat In A Maze

Move backward until we reach a square from which a forward move is possible.

• Move downward.Trupti Agrawal 26

Page 27: Stacks and queues

Rat In A Maze

Move right.

• Backtrack. Trupti Agrawal 27

Page 28: Stacks and queues

Rat In A Maze

Move downward.

Trupti Agrawal 28

Page 29: Stacks and queues

Rat In A Maze

Move right.

Trupti Agrawal 29

Page 30: Stacks and queues

Rat In A Maze

Move one down and then right.

Trupti Agrawal 30

Page 31: Stacks and queues

Rat In A Maze

Move one up and then right.

Trupti Agrawal 31

Page 32: Stacks and queues

Rat In A Maze

Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.

Trupti Agrawal 32

Page 33: Stacks and queues

Stacks

Standard operations:

IsEmpty … return true iff stack is empty

IsFull … return true iff stack has no remaining capacity

Top … return top element of stack

Push … add an element to the top of the stack

Pop … delete the top element of the stack

Trupti Agrawal 33

Page 34: Stacks and queues

Stacks

Use a 1D array to represent a stack.

Stack elements are stored in stack[0] through stack[top].

Trupti Agrawal 34

Page 35: Stacks and queues

Stacks

stack top is at element e

IsEmpty() => check whether top >= 0

O(1) time

IsFull() => check whether top == capacity – 1

O(1) time

Top() => If not empty return stack[top]

O(1) time

0 1 2 3 4 5 6

a b c d e

Trupti Agrawal 35

Page 36: Stacks and queues

Derive From arrayList

Push(theElement) => if full then either error or increase capacity and then add at stack[top+1]

Suppose we increase capacity when full

O(capacity) time when full; otherwise O(1)

Pop() => if not empty, delete from stack[top]

O(1) time

0 1 2 3 4 5 6

a b c d e

Trupti Agrawal 36

Page 37: Stacks and queues

Push

void push(element item)

{/* add an item to the global stack */

if (top >= MAX_STACK_SIZE - 1)

StackFull();

/* add at stack top */

stack[++top] = item;

}

Trupti Agrawal 37

0 1 2 3 4

a b c d e

top

Page 38: Stacks and queues

Pop

element pop()

{

if (top == -1)

return StackEmpty();

return stack[top--];

}

Trupti Agrawal 38

0 1 2 3 4

a b c d e

top

Page 39: Stacks and queues

StackFull()void StackFull()

{

fprintf(stderr, “Stack is full, cannot add

element.”);

exit(EXIT_FAILURE);

}

Trupti Agrawal 39

Page 40: Stacks and queues

StackFull()/Dynamic Array Use a variable called capacity in place of

MAX_STACK_SIZE

Initialize this variable to (say) 1

When stack is full, double the capacity using REALLOC

This is called array doubling

Trupti Agrawal 40

Page 41: Stacks and queues

StackFull()/Dynamic Array

void StackFull()

{

REALLOC(stack, 2*capacity*sizeof(*stack);

capacity *= 2;

}

Trupti Agrawal 41

Page 42: Stacks and queues

Complexity Of Array

Doubling Let final value of capacity be 2k

Number of pushes is at least 2k-1+ 1

Total time spent on array doubling is S1<=i=k2i

This is O(2k)

So, although the time for an individual push is O(capacity), the time for all n pushes remains O(n)!

Trupti Agrawal 42

Page 43: Stacks and queues

Queues

Linear list.

One end is called front.

Other end is called rear.

Additions are done at the rear only.

Removals are made from the front only.

Trupti Agrawal 43

Page 44: Stacks and queues

Bus Stop Queue

Bus

Stop

front

rear

rear rear rear rear

Trupti Agrawal 44

Page 45: Stacks and queues

Bus Stop Queue

Bus

Stop

front

rear

rear rear

Trupti Agrawal 45

Page 46: Stacks and queues

Bus Stop Queue

Bus

Stop

front

rear

rear

Trupti Agrawal 46

Page 47: Stacks and queues

Bus Stop Queue

Bus

Stop

front

rear

rear

Trupti Agrawal 47

Page 48: Stacks and queues

Revisit Of Stack Applications Applications in which the stack cannot be replaced

with a queue.

Parentheses matching.

Towers of Hanoi.

Switchbox routing.

Method invocation and return.

Try-catch-throw implementation.

Application in which the stack may be replaced with a queue.

Rat in a maze.

Results in finding shortest path to exit.

Trupti Agrawal 48

Page 49: Stacks and queues

Queue Operations

IsFullQ … return true iff queue is full

IsEmptyQ … return true iff queue is empty

AddQ … add an element at the rear of the queue

DeleteQ … delete and return the front element of the queue

Trupti Agrawal 49

Page 50: Stacks and queues

Queue in an Array

Use a 1D array to represent a queue.

Suppose queue elements are stored with the front element in

queue[0], the next in queue[1], and so on.

Trupti Agrawal 50

Page 51: Stacks and queues

Queue in an Array(Linear Queue)

DeleteQ() => delete queue[0]

O(queue size) time

AddQ(x) => if there is capacity, add at right end

O(1) time

0 1 2 3 4 5 6

a b c d e

Trupti Agrawal 51

Page 52: Stacks and queues

Circular Array(Circular Queue)

Use a 1D array queue.

queue[]

• Circular view of array.

[0]

[1]

[2] [3]

[4]

[5]Trupti Agrawal 52

Page 53: Stacks and queues

Circular Array• Possible configuration with 3 elements.

[0]

[1]

[2] [3]

[4]

[5]

A B

C

Trupti Agrawal 53

Page 54: Stacks and queues

Circular Array

• Another possible configuration with 3

elements.

[0]

[1]

[2] [3]

[4]

[5]AB

C

Trupti Agrawal 54

Page 55: Stacks and queues

Circular Array

• Use integer variables front and rear.

– front is one position counterclockwise from first

element

– rear gives position of last element

[0]

[1]

[2] [3]

[4]

[5]

A B

C

frontrear

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rear

Trupti Agrawal 55

Page 56: Stacks and queues

Add An Element

[0]

[1]

[2] [3]

[4]

[5]

A B

C

frontrear

• Move rear one clockwise.

Trupti Agrawal 56

Page 57: Stacks and queues

Add An Element

• Move rear one clockwise.

[0]

[1]

[2] [3]

[4]

[5]

A B

C

front

rear

• Then put into queue[rear].

D

Trupti Agrawal 57

Page 58: Stacks and queues

Delete An Element

[0]

[1]

[2] [3]

[4]

[5]

A B

C

frontrear

• Move front one clockwise.

Trupti Agrawal 58

Page 59: Stacks and queues

Delete An Element

[0]

[1]

[2] [3]

[4]

[5]

A B

C

front

rear

• Move front one clockwise.

• Then extract from queue[front].

Trupti Agrawal 59

Page 60: Stacks and queues

Moving rear Clockwise

[0]

[1]

[2] [3]

[4]

[5]

A B

C

front rear

• rear++;

if (rear = = capacity) rear = 0;

• rear = (rear + 1) % capacity;

Trupti Agrawal 60

Page 61: Stacks and queues

Empty That Queue

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rear

Trupti Agrawal 61

Page 62: Stacks and queues

Empty That Queue

[0]

[1]

[2] [3]

[4]

[5]B

C

front

rear

Trupti Agrawal 62

Page 63: Stacks and queues

Empty That Queue

[0]

[1]

[2] [3]

[4]

[5]

C

front

rear

Trupti Agrawal 63

Page 64: Stacks and queues

Empty That Queue

[0]

[1]

[2] [3]

[4]

[5]

front

rear

Trupti Agrawal 64

Page 65: Stacks and queues

Dequque In computer science, a double-ended queue (dequeue,

often abbreviated to deque) is an abstract data type that

generalizes a queue, for which elements can be added to

or removed from either the front (head) or back (tail).

It is also often called a head-tail linked list,

Trupti Agrawal 65

Page 66: Stacks and queues

Dequque [Cont…]

This differs from the queue abstract data type or First-In-

First-Out List (FIFO), where elements can only be added to

one end and removed from the other. This general data

class has some possible sub-types:

An input-restricted deque is one where deletion can be

made from both ends, but insertion can be made at one end

only.

An output-restricted deque is one where insertion can be

made at both ends, but deletion can be made from one end

only

Trupti Agrawal 66

Page 67: Stacks and queues

Dequque [Cont…] Both the basic and most common list types in

computing, queues and stacks can be considered

specializations of deques, and can be implemented using

deques.

Trupti Agrawal 67

Page 68: Stacks and queues

Applications of stacks: Conversion form

infix to postfix and prefix

expressions

There are a number of applications of stacks such as:

To print characters/string in reverse order.

Check the parentheses in the expression.

To evaluate the arithmetic expressions such as, infix, prefix

and postfix.

Trupti Agrawal 68

Page 69: Stacks and queues

Arithmetic expression:

An expression is defined as a number of operands or data

items combined using several operators. There are

basically three types of notations for an expression:

1) Infix notation

2) Prefix notation

3) Postfix notation

(Main advantage of postfix and prefix notation are no need

to use brackets)

Trupti Agrawal 69

Page 70: Stacks and queues

Infix notation: It is most common notation in which,

the operator is written or placed in-between the two

operands. For eg. The expression to add two numbers

A and B is written in infix notation as:

A+ B

• A and B : Operands

• + : Operator

In this example, the operator is placed in-between the

operands A and B. The reason why this notation is

called infix.

Trupti Agrawal 70

Page 71: Stacks and queues

Prefix Notation: It is also called Polish notation,

named after in the honor of the mathematician Jan

Lukasiewicz, refers to the notation in which the

operator is placed before the operand as: +AB

As the operator ‘+’ is placed before the operands A and

B, this notation is called prefix (pre means before).

Trupti Agrawal 71

Page 72: Stacks and queues

Postfix Notation: In the postfix notation the operators are written after the operands, so it is called the postfix notation (post means after), it is also known as suffix notation or reverse polish notation. The above postfix if written in postfix notation looks like follows; AB+

Trupti Agrawal 72

Page 73: Stacks and queues

Notation Conversions: Let us take an expression A+B*C

which is given in infix notation. To evaluate this expression

for values 2, 3, 5 for A, B, C respectively we must follow

certain rule in order to have right result. For eg. A+B*C =

2+3*5 = 5*5 = 25!!!!!!!!

Is this the right result? No, this is because the multiplication

is to be done before addition, because it has higher

precedence over addition. This means that for an expression

to be calculated we must have the knowledge of precedence

of operators.

Trupti Agrawal 73

Page 74: Stacks and queues

Operator Precedence

Operator Symbol Precedence

Exponential $ Highest

Multiplication/Division *,/ Next highest

Addition/Substraction +,- Lowest

Trupti Agrawal 74

Page 75: Stacks and queues

THANK YOU…!!!

Trupti Agrawal 75


Top Related