Upload
trupti-agrawal
View
212
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Stacks, Queues, types of queues and standard operations on queues and stacks
Citation preview
Stacks and Queues
Trupti Agrawal 1
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
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
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
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
123
Trupti Agrawal 5
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
12
3
Trupti Agrawal 6
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
1 2 3
Trupti Agrawal 7
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
1 23
Trupti Agrawal 8
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
123
Trupti Agrawal 9
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
123
Trupti Agrawal 10
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
12
3
Trupti Agrawal 11
Towers Of Hanoi/Brahma
3-disk Towers Of Hanoi/Brahma
A B C
123
• 7 disk movesTrupti Agrawal 12
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
Recursive Solution
move top disk from A to C
A B C
1
Trupti Agrawal 14
Recursive Solution
move top n-1 disks from B to C using A
A B C
1
Trupti Agrawal 15
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
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
Rat In A Maze
Trupti Agrawal 18
Rat In A Maze
Move order is: right, down, left, up
Block positions to avoid revisit.Trupti Agrawal 19
Rat In A Maze
Move order is: right, down, left, up
Block positions to avoid revisit.
Trupti Agrawal 20
Rat In A Maze
Move backward until we reach a square from which a forward move is possible.
Trupti Agrawal 21
Rat In A Maze
Move down.
Trupti Agrawal 22
Rat In A Maze
Move left.
Trupti Agrawal 23
Rat In A Maze
Move down.
Trupti Agrawal 24
Rat In A Maze
Move backward until we reach a square from which a forward move is possible.
Trupti Agrawal 25
Rat In A Maze
Move backward until we reach a square from which a forward move is possible.
• Move downward.Trupti Agrawal 26
Rat In A Maze
Move right.
• Backtrack. Trupti Agrawal 27
Rat In A Maze
Move downward.
Trupti Agrawal 28
Rat In A Maze
Move right.
Trupti Agrawal 29
Rat In A Maze
Move one down and then right.
Trupti Agrawal 30
Rat In A Maze
Move one up and then right.
Trupti Agrawal 31
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
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
Stacks
Use a 1D array to represent a stack.
Stack elements are stored in stack[0] through stack[top].
Trupti Agrawal 34
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
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
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
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
StackFull()void StackFull()
{
fprintf(stderr, “Stack is full, cannot add
element.”);
exit(EXIT_FAILURE);
}
Trupti Agrawal 39
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
StackFull()/Dynamic Array
void StackFull()
{
REALLOC(stack, 2*capacity*sizeof(*stack);
capacity *= 2;
}
Trupti Agrawal 41
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
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
Bus Stop Queue
Bus
Stop
front
rear
rear rear rear rear
Trupti Agrawal 44
Bus Stop Queue
Bus
Stop
front
rear
rear rear
Trupti Agrawal 45
Bus Stop Queue
Bus
Stop
front
rear
rear
Trupti Agrawal 46
Bus Stop Queue
Bus
Stop
front
rear
rear
Trupti Agrawal 47
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
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
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
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
Circular Array(Circular Queue)
Use a 1D array queue.
queue[]
• Circular view of array.
[0]
[1]
[2] [3]
[4]
[5]Trupti Agrawal 52
Circular Array• Possible configuration with 3 elements.
[0]
[1]
[2] [3]
[4]
[5]
A B
C
Trupti Agrawal 53
Circular Array
• Another possible configuration with 3
elements.
[0]
[1]
[2] [3]
[4]
[5]AB
C
Trupti Agrawal 54
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
Add An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
frontrear
• Move rear one clockwise.
Trupti Agrawal 56
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
Delete An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
frontrear
• Move front one clockwise.
Trupti Agrawal 58
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
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
Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]AB
Cfront
rear
Trupti Agrawal 61
Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]B
C
front
rear
Trupti Agrawal 62
Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]
C
front
rear
Trupti Agrawal 63
Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]
front
rear
Trupti Agrawal 64
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
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
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
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
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
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
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
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
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
Operator Precedence
Operator Symbol Precedence
Exponential $ Highest
Multiplication/Division *,/ Next highest
Addition/Substraction +,- Lowest
Trupti Agrawal 74
THANK YOU…!!!
Trupti Agrawal 75