Home
» write an algorithm to convert infix expression to postfix expression.
» Stack algorithms of push operation
Monday, September 12, 2016
Stack algorithms of push operation
This algorithm is for inserting an element in stack also known as push element in stack:
Algorithms
of Stack as a linked list:
This algorithm is for inserting an element in stack:
1.
Input
value in ITEM
2.
Create a new node as temp= new node
3.
temp→data = num
4.
temp→link = top
5.
top = temp
6.
Exit
|
This
algorithm is for converting infix expression to postfix expression it is also
known as transform:
Description: Here I is
an arithmetic expression written in infix notation and P is
the equivalent postfix expression generated by this algorithm.
1.
Push “(“ left parenthesis onto
stack.
2.
Add “)” right parenthesis to the end
of expression I.
3.
Scan I from left to right and repeat
step 4 for each element of I
Until the stack becomes empty.
4.
If the scanned
element is:
(a) An operand then add it to P.
(b)
A left parenthesis then push it onto stack.
(c) an operator then:
(i) Pop from stack and add to P each operator
which has
the same or higher precedence then
the scanned operator.
(ii) Add newly scanned operator to stack.
(d) a right parenthesis then:
(i) Pop from stack and add to P each operator
until a left parenthesis is encountered.
(ii) Remove the left parenthesis.
[End of Step 4 If]
[End of step 3 For Loop]
5.
Exit.
|
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment