0% found this document useful (0 votes)
7 views4 pages

Stack Data Structure and Expression Conversion

This document discusses stacks as a data structure that holds multiple items in one name. It presents different forms of expressions (infix, postfix, prefix) and precedence rules for operators. It also provides examples of converting infix expressions to postfix using a stack method and evaluating postfix expressions by inspection by hand.

Uploaded by

Pushkar Kanth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Stack Data Structure and Expression Conversion

This document discusses stacks as a data structure that holds multiple items in one name. It presents different forms of expressions (infix, postfix, prefix) and precedence rules for operators. It also provides examples of converting infix expressions to postfix using a stack method and evaluating postfix expressions by inspection by hand.

Uploaded by

Pushkar Kanth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STACK (DATA STRUCTURE)

Data structure is a memory structure which holds multiple items in


one name.
1. ARRAY/LIST—STACK WILL BE IMPLEMENTED.
2. STRING
3. TUPPLE
4. DICTIONARY

FORMS OF EXPRESSIONS
1.)INFIX-----A+B When the operator lies between the operants.
2.)POSTFIX------AB+ When the lies post the two operants.
3.)PREFIX-------+AB When the operator lies pre the two operants.

ORDER OF PRECEDENCE
() -----BRACKET IS SOLVED FIRST
^ ------HIGHEST PRIORITY
*/------MIDDLE PRIORITY
+- -------LOWEST PRIORITY
(HH(WW))----- FIRST INNER BRACKET IS SOLVED
A+B/C-D (INFIX)
A+(B/C)-D
A+(BC/)-D
(A+(BC/))-D
(A(BC/)+)-D
(ABC/+)-D
((ABC/+)-D))
ABC/+D- (POSTFIX)
In this INFIX Form is converted to PREFIX Form.

STACK METHOD OF CONVERTING INFIX INTO POSTFIX


# A+ (B-C+D)/E^F (Mark the Bracket given in question)
A+((B-C)+D)/E^F

Rules:
1. All operands are to be PRINTED in Output column.
2. All operators are to be PUSHED in Stack column.
3. WHEN CLOSING BRACKET comes, POP and PRINT 1 operator
from the TOP of the stack.
4. Ignore the Opening Bracket if its closing bracket comes (from
next step onwards).
5.
NOT (HIGHEST)
AND (MIDDLE)
OR (LOWEST)
[(TRUE AND [NOT FALSE]) OR (FALSE OR[NOT TRUE])]
STEP INPUT ACTION STACK OUTPUT
1 ( PUSH (
2 TRUE PRINT ( TRUE
3 AND PUSHOT (AND TRUE
4 NOT PUSH (AND,NOT TRUE,FALSE
5 FALSE PRINT (AND,NOT TRUE,FALSE
POP AND (AND TRUE,FALSE,
PRINT

CONVERT INTO POSTFIX BY INSPECTION BY HAND METHOD-


ALSO CALCULATE THE EXPRESSION-
3*50-4+20/2^2
3*50-4+20/4
150-4+20/4
150-4+5
146+5
151
………………………………………………………………………………
3*50-4+20/2^2
(3,50*)-4+20/(2,2^)
(3,50*)-4+(20,2,2^/)
(3,50*4-)+(20,2,2^/)
3,50,*,4,-,20,2,2, ^,/,+
Rules:
1.)Rule- operands are to be pushed in stack.
2.)Operator-Process(POP 2 VALUES FROM STACK AND PUSH BACK
THE RESULT)

You might also like