Professional Documents
Culture Documents
005 - DS Stack
005 - DS Stack
005 - DS Stack
By Jay Wengrow
2
Stack
3
Stack
4
Stack
5
Stack
6
Stack
The following diagram depicts a stack and its operations −
7
Stack
Stack is a LIFO data structure
8
Basic Operations of Stack
9
Working of Stack Data Structure
The operations work as follows:
1. A pointer called TOP is used to keep track of the top element
in the stack.
2. When initializing the stack, we set its value to -1 so that we
can check if the stack is empty by comparing TOP == -1.
3. On pushing an element, we increase the value of TOP and
place the new element in the position pointed to by TOP.
4. On popping an element, we return the element pointed to
by TOP and reduce its value.
5. Before pushing, we check if the stack is already full
6. Before popping, we check if the stack is already empty
10
Working of Stack Data Structure
11
# Stack implementation in Python
# Creating a stack
def create_stack():
stack = []
return stack
return stack.pop()
stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
12
Algorithm of isempty function
else
return false
endif
end procedure
13
Algorithm of isfull function
else
return false
endif
end procedure
14
Algorithm of peek function
15
Algorithm for PUSH Operation
pointing.
Step 5 − Return success 16
Algorithm for PUSH Operation
endif
top ← top + 1
stack[top] ← data
end procedure
17
Algorithm for POP Operation
18
Algorithm for POP Operation
19
Algorithm for POP Operation
endif
data ← stack[top]
top ← top - 1
return data
end procedure
20
# Python program for linked list implementation of stack
# Class to represent a node
class StackNode:
# Constructor to initialize a node
def __init__(self, data):
self.data = data
self.next = None
class Stack:
# Constructor to initialize the root of linked list
def __init__(self):
self.root = None
def isEmpty(self):
return True if self.root is None else False
def push(self, data):
newNode = StackNode(data)
newNode.next = self.root
self.root = newNode
print "% d pushed to stack" % (data)
def pop(self):
if (self.isEmpty()):
return float("-inf")
temp = self.root
self.root = self.root.next
popped = temp.data
return popped
def peek(self):
if self.isEmpty():
return float("-inf")
return self.root.data
# Driver code
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
21
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
Top element is 20
Elements present in stack : 20 10
22
Application of Stack
24
Application of Stack
Infix expression:
The expression of the form a op b. When an operator is in-
between every pair of operands.
It follows the scheme of <operand><operator><operand> i.e.
an <operator> is preceded and succeeded by an <operand>.
Such an expression is termed infix expression. E.g., A+B
Postfix expression:
The expression of the form a b op. When an operator is
followed for every pair of operands.
It follows the scheme of <operand><operand><operator> i.e.
an <operator> is succeeded by both the <operand>. E.g., AB+
25
Application of Stack
27
Application of Stack
Algorithm to convert Infix To Postfix
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1 If the precedence of the scanned operator is greater than the precedence of the
operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push
it.
2 Else, Pop all the operators from the stack which are greater than or equal to in
precedence than that of the scanned operator. After doing that Push the scanned
operator to the stack. (If you encounter parenthesis while popping then stop there
and push the scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output 28
8. Pop and output from the stack until it is not empty.
Application of Stack
Algorithm to convert Infix To Postfix
Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression Y.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same
precedence as or higher precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is
encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.
29
Infix to Prefix Conversion : Example - 1
Infix: A* B + C
Infix: A * B + C
30
Infix to Prefix Conversion : Example - 1
Infix: A* B + C
No. Expression Stack Postfix
0 (
1 A ( A
2 ( A
3 * ( * A
4 ( * A
5 B ( * A B
6 ( * A B
7 + ( + A B *
8 ( + A B *
9 C ( + A B * C
10 ) A B * C +
31
Infix to Prefix Conversion : Example - 2
Infix: A* ( B + C )
Infix: A * ( B + C )
32
Infix to Prefix Conversion : Example - 2
Infix: A* ( B + C )
No. Expression Stack Postfix
0 (
1 A ( A
2 ( A
3 * ( * A
4 ( * A
5 B ( * ( A B
6 ( * ( A B
7 + ( * ( + A B
8 ( * ( + A B
9 C ( * ( + A B C
10 ) ( * A B C+
11 ) A B C+ *
33
Infix to Prefix Conversion : Example - 3
Infix: A*(B+C)/D
Infix: A * ( B + C ) / D
34
Infix to Prefix Conversion : Example - 3
Infix: A*(B+C)/D
No. Expression Stack Postfix
0 (
1 A ( A
2 * ( * A
3 B ( * ( A B
4 + ( * ( + A B
5 C ( * ( + A B C
6 ) ( * A B C +
7 / ( / A B C + *
8 D ( / A B C + * D
9 ) A B C + * D /
35
Infix to Prefix Conversion : Example - 4
Infix: A* B^C+ D
Infix: A * B ^ C + D
36
Infix to Prefix Conversion : Example - 4
Infix: A* B^C+ D
No. Expression Stack Postfix
0 ( When the '+' is
1 A ( A encountered in line 6, it is
2 ( A first compared to the '^'
on top of the stack. Since
3 * ( * A
it has lower precedence,
4 ( * A the '^' is popped and
5 B ( * A B printed.
6 ( * A B But instead of pushing
7 the '+' sign onto the stack
^ ( * ^ A B
now, we must compare it
8 ( * ^ A B with the new top of the
9 C ( * ^ A B C stack, the '*'.
10 ( * ^ A B C Since the operator also
11 + ( + A B C ^ * has higher precedence
than the '+', it also must
12 ( + A B C ^ * be popped and printed.
13 D ( + A B C ^ * D Now the stack is empty,
14 ( + A B C ^ * D so the '+' can be pushed
15 ) A B C ^ * D + onto the stack.
37
Infix to Prefix Conversion : Example - 5
Infix: A * ( B + C * D ) + E
Infix: A * ( B + C * D ) + E
38
Infix to Prefix Conversion : Example - 5
Infix: A * ( B + C * D ) + E
No. Expression Stack Postfix
0 (
1 A ( A
2 ( A
3 * ( * A
4 ( * A
5 B ( * ( A B
6 ( * ( A B
7 + ( * ( + A B
8 ( * ( + A B
9 C ( * ( + A B C
10 ( * ( + A B C
11 * ( * ( + * A B C
12 ( * ( + * A B C
13 D ( * ( + * A B C D
14 ) ( * A B C D * +
15 ( * A B C D * +
16 + ( + A B C D * + *
17 ( + A B C D * + *
18 E ( + A B C D * + * E
19 ( + A B C D * + * E
20 ) A B C D * + * E +
39
Infix to Prefix Conversion : Example - 6
Infix: A+(B*C-(D/E^F)*G)*H
Infix: A + ( B * C - ( D / E ^ F ) * G ) * H
40
Infix to Prefix Conversion : Example - 6
Infix: A+(B*C-(D/E^F)*G)*H
No. Expression Stack Postfix
0 (
1 A ( A
2 + ( + A
3 B ( + ( A B
4 * ( + ( * A B
5 C ( + ( * A B C
6 - ( + ( - A B C *
7 D ( + ( - ( A B C * D
8 / ( + ( - ( / A B C * D
9 E ( + ( - ( / A B C * D E
10 ^ ( + ( - ( / ^ A B C * D E
11 F ( + ( - ( / ^ A B C * D E F
12 ) ( + ( - A B C * D E F ^ /
13 * ( + ( - * A B C * D E F ^ /
14 G ( + ( - * A B C * D E F ^ / G
15 ) ( + A B C * D E F ^ / G * -
16 * ( + * A B C * D E F ^ / G * -
17 H ( + * A B C * D E F ^ / G * - H
18 ( + * A B C * D E F ^ / G * - H
19 ) A B C * D E F ^ / G * - H * +
41
Infix to Prefix Conversion : Example - 7
Infix: ( A/(B-C)*D+E)
42
Infix to Prefix Conversion : Example - 7
43
Application of Stack
46
# Python program to convert infix expression to postfix
print "".join(self.output)
49
Queues
50
Queues
51
Queues
52
Basic Operations of Queues
Queue operations may involve initializing or defining the
queue, utilizing it and then completing erasing it from
memory. Here we shall try to understand basic operations
associated with queues
1. enqueue − add store an item to the queue.
2. dequeue − remove access an item from the queue.
Few more functions are required to make above mentioned
queue operation efficient. These are
1. peek − get the element at front of the queue without
removing it.
2. isfull − checks if queue is full.
3. isempty − checks if queue is empty.
53
Algorithm of isempty function
else
return false
endif
end procedure
54
Algorithm of isfull function
else
return false
endif
end procedure
55
Algorithm of peek function
56
enqueue operation
As queue maintains two data pointers, front and rear, its
operations are comparatively more difficult to implement
than stack.
The following steps should be taken to enqueue insert data
into a queue
Step 1 − Check if queue is full.
Step 2 − If queue is full, produce overflow error and exit.
Step 3 − If queue is not full, increment rear pointer to point
next empty space.
Step 4 − Add data element to the queue location, where rear is
pointing.
57
Step 5 − return success.
enqueue operation
58
enqueue operation
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
59
dequeue operation
Accessing data from queue is a process of two tasks − access
the data where front is pointing and remove the data after
access. The following steps are taken to perform
dequeue operation
Step 1 − Check if queue is empty.
Step 2 − If queue is empty, produce underflow error and exit.
Step 3 − If queue is not empty, access data where front is
pointing.
Step 4 − Increment front pointer to point next available data
element.
Step 5 − return success. 60
dequeue operation
61
dequeue operation
62
Algorithm for dequeue operation
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front - 1
return true
end procedure
63
Algorithm to insert any element in a
queue
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT
64
Algorithm to delete an element from the
queue
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
65