Escolar Documentos
Profissional Documentos
Cultura Documentos
Stack
Stack is a linear data structure in which the insertion and
deletion operations are performed at only one end. In a
stack, adding and removing of elements are performed at
single position which is known as "top". That means, new
element is added at top of the stack and an element is
removed from the top of the stack. In stack, the insertion and
deletion operations are performed based on LIFO (Last In
First Out) principle.
@VKSLearningHub
Page 1
Page 2
Page 3
Page 4
Page 5
Page 6
In linked list implementation of a stack, every new element is inserted as 'top' element.
That means every newly inserted element is pointed by 'top'. Whenever we want to
remove an element from the stack, simply remove the node which is pointed by 'top' by
moving 'top' to its next node in the list. The next field of the first element must be
always NULL.
Example
In above example, the last inserted node is 99 and the first inserted node is 25. The order of
elements inserted is 25, 32,50 and 99.
@VKSLearningHub
Page 7
Page 8
Page 9
Page 10
Page 11
Infix notation.
Prefix notation.
Postfix notation
To add A, B, we write
Instead of saying "A plus B", Another alternative is
A+B
we could say "add A,B " and to put the operators
To multiply A, B, we write write + A B
after the operands as
A*B
"Multiply A,B" would be
in
AB+
The operators ('+' and '*')
written * A B
and A B *
go in between the
The operators ('+' and '*') The operators ('+' and '*')
operands ('A' and 'B')
go in before the operands
go in after the operands
This is Infix notation.
('A' and 'B')
('A' and 'B')
Evaluate 2+3*5.
+ First:
(2+3)*5
+2*35
235*+
=+2*35
=235*+
= + 2 15 = 17
= 2 15 + = 17
= 5*5 = 25
* First:
*+235=
2+(3*5)
=*+235
= 2+15 = 17
= * 5 5 = 25
(( A + B)*(C+D))
@VKSLearningHub
No parentheses
needed!
23+5*
23+5*
= 5 5 * = 25
No parentheses
needed here either
(( A + B)*(C+D))
(( A + B)*(C+D))
(+AB) *(+CD)
(AB+) *(+CD)
*+AB+CD
AB+CD+*
Page 12
INPUT
(
(
A
+
B
)
STACK
(
((
((
((+
((+
(
POSTFIX
*
C
+
(*
(*
(+
AB+
AB+C
AB+C*
D
/
(
E
*
G
)
(+
(+/
(+/(
(+/(
(+/(*
(+/(*
(+/
AB+C*D
AB+C*D
AB+C*D
AB+C*DE
AB+C*DE
AB+C*DEG
AB+C*DEG*
H
)
((EMPTY
AB+C*DEG*/+
AB+C*DEG*/+H
AB+C*DEG*/+H-
@VKSLearningHub
A
A
AB
AB+
EXPLANATION
( Left Parenthesis will go in stack
A (operand) will go to postfix
+ (operator) will be pushed to stack at top
B operand) will go to postfix after A
) Right Parenthesis will make all operator(s) till previous
left parentheses to pop from top one by one and added
to end of Postfix
*(operator) will be pushed to stack to top
C (operand) will go to postfix
+ (operator) will be pushed to stack topv after pop out *
Operator from stack as it has higher precedence than +
D (operand) will go to postfix
/ (operator) will be pushed to stack at top
( Left Parenthesis will go in stack at top
E (operand) will go to postfix
*(operator) will be pushed to stack to top
G (operand) will go to postfix
) Right Parenthesis will make all operator(s) till previous
left parentheses will pop from top one by one and added
to end of Postfix
-(operator) will be pushed to stack to top
H (operand) will go to postfix
) Right Parenthesis will make all operator(s) till previous
left parentheses will pop from top one by one and added
to end of Postfix /As this make Stack empty, the current
Postfix String is the resultant POSTFIX NOTATION
Page 13
@VKSLearningHub
Page 14
Example 2
@VKSLearningHub
Page 15
20
10
+
5
2
*
10
3
*
7
1
*
25
8
3
/
6
*
5
20
15
*
25
2
5
3
2
4
+
5
*
TRUE OR FALSE AND NOT FALSE OR FALSE
TRUE FALSE NOT FALSE TRUE OR AND
NOT A OR B NOT B AND NOT C
10
23
10
*
+
/
+
+
+
6
X-Y/(Z+U)*V
A*(B+(C+D)*(E+F)/G)*H
(A+B*C)/D+E/(F*G+H/I)
A+B*C+(D*E+F)
(a+b-c)*d(e+f)
A - B - C * (D + E / F - G) H
A + ((B - C * D) / E ) + F - G / H
(A * B - (C - D)) / (E + F)
A + ((B - C * D) / E ) + F - G / H
Page 16
@VKSLearningHub
Page 17