(DSA – 03)
Piles
Push-down Lists
Stacks (Contd …)
Examples:
Pile of plates in a cafeteria.
Coin stacker.
Railway system for shunting cars.
6 F TOP
5 E
4 D
3 C
2 B
1 A
Stacks (Contd …)
Basic Terminology associated with Stacks:
Overflow
Occurs when we try to insert (push) more information
on a stack than it can hold
Underflow
Occurs when we try to delete (pop) an item off a stack,
which is empty.
Stacks (Contd …)
Basic operations associated with Stacks:
Recursion
Infix, Postfix and Prefix Expressions
Infix Expression
Example:
A+B
Prefix / Polish Expression
Example:
+AB
Postfix / Suffix/ Reverse Polish Expression
Example:
AB+
Infix operator precedence
Highest
Parentheses ( ) Left to Right
1. a +b – c
2. a + b * c
3. (a + b) * c
4. a + (b / c) * d
5. ((a + b * c) / d / e / f * g + h) / i
6. a + (b – c * d / e) – (f / g)
Evaluation of Postfix expressions
Operand Scanned Contents of stack
a 5
b 54
c 542
/ 52
d 522
* 54
+ 9
Conversion of Infix expression to Postfix expression
Convert the Infix expression (a + b * c * d) * (e + f / d)
to Postfix form.
Technique of
in terms of itself.
Examples
1. Factorial of N
factorial(N) = 1, if N = 0
= N * factorial(N–1), if N > 0
For N = 5
factorial(5) = 5 * factorial(4)
= 20 * factorial(3)
= 60 * factorial(2)
= 120 * factorial(1)
= 120 * factorial(0)
= 120
2. Calculation of Fibonacci numbers
fibonacci(n)
= 1, for n = 0 or n = 1
= fibonacci(n–1) + fibonacci(n–2), for n >= 2
GCD(24, 10)
= GCD(10, 4)
= GCD(4, 2)
= GCD(2, 0)
=2
4. Ackerman’s function
Acker(M, N) = N+1, if M = 0
= Acker(M–1, 1), if N = 0
= Acker(M–1, Acker(M, N–1), otherwise
Acker(1, 1)
= Acker(0, Acker(1, 0))
= Acker(0, Acker(0, 1))
= Acker(0, 2)
=3
Queues
Queue
Ordered collection of data such that
data is inserted at one end
and deleted from another end.
Examples:
Structure where many people line up for few resources.
Representation of Queue
deletion insertion
Front Rear
Operations on Queues
Inserting an element into a queue Q
List
INFO LINK
NODE
INFO LINK
X ^
NEW
Representation
…
FIRST
Node in a doubly linked list
if (R = NULL) then
LPTR(NEW) ← NULL
RPTR(NEW) ← NULL
L ← NEW
R ← NEW
Return
NEW
After
L→ ^ W X Y ^ ←R
NEW M
5. [Right most Insertion]
if M = R then
RPTR(NEW) ← NULL
LPTR(NEW) ← M
RPTR(M) ← NEW
R ← NEW
Return
Before
L→ ^ X Y ^ ←R
NEW
After
L→ ^ X Y W ^ ←R
M NEW
6. [Insert in middle]
LPTR(NEW) ← LPTR(M)
RPTR(NEW) ← M
RPTR(LPTR(M)) ← NEW
LPTR(M) ← NEW
Return
Before
L→ ^ X Y ^ ←R
LPTR(M) M
W
NEW
After
L→ ^ X W Y ^
←R
NEW M
Deleting an item from a doubly linked list
Algorithm DOUBEL (L, R, OLD)
L Address of leftmost node
R Address of rightmost node
OLD Address of the node to be deleted
LPTR Left link of the node
RPTR Right link of the node
Before
LPTR INFO RPTR
L→ ^ X ^ ←R
After
else if (OLD = L) then [Leftmost node to be deleted]
L ← RPTR(L)
LPTR(L) ← NULL
Before
L→ ^ X Y Z ^ ←R
RPTR(L)
After
L→ ^ Y Z ^ ←R
else if (OLD = R) then [Rightmost node to be deleted]
R ← LPTR(R)
RPTR(R) ← NULL
Before
L→ ^ X Y Z ^ ←R
LPTR(R)
After
L→ ^ X Y ^ ←R
else [middle node to be deleted]
RPTR(LPTR(OLD)) ← RPTR(OLD)
LPTR(RPTR(OLD)) ← LPTR(OLD)
Before
L→ ^ X Y Z ^ ←R
After
L→ ^ X Z ^ ←R
3. [Return deleted node]
AVAIL ← OLD
Sequences
Linear Sequence(S)
Contains n elements.
Each element e of S
uses an integer in the range [0, n–1].
Rank of an element e in S
Specifies where to
insert a new element
and to delete an old element.
Iterator
Consists of a
Sequence S
Current position in S
Way of stepping to the next positioning S
and making it the current position.
Returns the elements
according to their linear ordering.