2. 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
3. Stack Of Cups
top
bottom
E
D
C
B
A
Add a cup to the stack.
top
bottom
F
E
D
C
B
A
• Remove a cup from new stack.
• A stack is a LIFO list.
Trupti Agrawal 3
4. Towers Of Hanoi/Brahma
1 2 3 4
A B C
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
Trupti Agrawal 4
15. Recursive Solution
A B C
move top n-1 disks from B to C using A
1
Trupti Agrawal 15
16. Recursive Solution
1
A B C
moves(n) = 0 when n = 0
moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
Trupti Agrawal 16
17. 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
30. Rat In A Maze
Move one down and then right.
Trupti Agrawal 30
31. Rat In A Maze
Move one up and then right.
Trupti Agrawal 31
32. 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
33. 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
34. Stacks
Use a 1D array to represent a stack.
Stack elements are stored in stack[0] through stack[top].
Trupti Agrawal 34
35. Stacks
a b c d e
0 1 2 3 4 5 6
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
Trupti Agrawal 35
36. Derive From arrayList
a b c d e
0 1 2 3 4 5 6
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
Trupti Agrawal 36
37. Push
a b c d e
0 1 2 3 4
top
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
38. Pop
a b c d e
0 1 2 3 4
top
element pop()
{
if (top == -1)
return StackEmpty();
return stack[top--];
}
Trupti Agrawal 38
40. 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
42. 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
43. 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
44. Bus Stop Queue
Bus
Stop
front
rear
rear rear rear rear
Trupti Agrawal 44
45. Bus Stop Queue
Bus
Stop
front
rear
rear rear
Trupti Agrawal 45
46. Bus Stop Queue
Bus
Stop
front
rear
rear
Trupti Agrawal 46
47. Bus Stop Queue
Bus
Stop
front
rear
rear
Trupti Agrawal 47
48. 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
49. 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
50. 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
51. Queue in an Array(Linear Queue)
a b c d e
0 1 2 3 4 5 6
DeleteQ() => delete queue[0]
O(queue size) time
AddQ(x) => if there is capacity, add at right end
O(1) time
Trupti Agrawal 51
52. Circular Array(Circular Queue)
Use a 1D array queue.
queue[]
• Circular view of array.
[0]
[1]
[2] [3]
[4]
[5]
Trupti Agrawal 52
53. Circular Array
• Possible configuration with 3 elements.
[0]
[1]
[2] [3]
[4]
[5]
A B
C
Trupti Agrawal 53
54. Circular Array
• Another possible configuration with 3
elements.
[0]
[1]
[2] [3]
[4]
[5]
B A
C
Trupti Agrawal 54
55. 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
front
rear
[0]
[1]
[2] [3]
[4]
[5]
B A
C
front
rear
Trupti Agrawal 55
56. Add An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move rear one clockwise.
Trupti Agrawal 56
57. Add An Element
• Move rear one clockwise.
• Then put into queue[rear].
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
D
rear
Trupti Agrawal 57
58. Delete An Element
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move front one clockwise.
Trupti Agrawal 58
59. Delete An Element
• Then extract from queue[front].
[0]
[1]
[2] [3]
[4]
[5]
A B
C
front
rear
• Move front one clockwise.
Trupti Agrawal 59
60. Moving rear Clockwise
• rear++;
if (rear = = capacity) rear = 0;
front rear
[0]
[1]
[2] [3]
[4]
[5]
A B
C
• rear = (rear + 1) % capacity;
Trupti Agrawal 60
61. Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]
B A
C
front
rear
Trupti Agrawal 61
62. Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]
B
C
front
rear
Trupti Agrawal 62
63. Empty That Queue
[0]
[1]
[2] [3]
[4]
[5]
C
front
rear
Trupti Agrawal 63
64. Empty That Queue
[0]
rear
[1]
[2] [3]
[4]
[5]
front
Trupti Agrawal 64
65. 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
66. 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
67. 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
68. 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
69. 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
70. 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
71. 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
72. 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
73. 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
74. Operator Precedence
Operator Symbol Precedence
Exponential $ Highest
Multiplication/Division *,/ Next highest
Addition/Substraction +,- Lowest
Trupti Agrawal 74