SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
DATA STRUCTURE ASSIGNMENT #2

Comsats institute Of Information Technology Islamabad

Subject :

Data Structure

Assignment#:

2

Submitted to:

Sir Zaheer Ul Hassan Sani

Submitted by:

Haq Nawaz

Reg#:

Sp12-bcs-089

Submission date:

2 November 2013

SP12-BCS-089

Page 1
DATA STRUCTURE ASSIGNMENT #2

Task a:
We have infix expression in the form of:

((A-B)+C*(D+E))-(F+G)
Now reading expression from right to left and pushing operators into stack and
variables to output stack

Input
)
G
+
F
(
)
)
E
+
D
(
*
C
+
)
B
A
(
(
EMPTY
SP12-BCS-089

Output_stack
EMPTY
G
G
GF
GF+
GF+
GF+
GF+
GF+E
GF+E
GF+ED
GF+ED+
GF+ED+
GF+ED+C
GF+ED+C*
GF+ED+C*
GF+ED+C*B
GF+ED+C*B
GF+ED+C*BA
GF+ED+C*BAGF+ED+C*BA-+
GF+ED+C*BA-+-

Stack
)
)
)+
)+
EMPTY
-)
-))
-))
-))+
-))+
-)
-)*
-)*
-)+
-)+)
-)+)
-)+)-)+)-)+
EMPTY
Page 2
DATA STRUCTURE ASSIGNMENT #2

Out put_stack = GF+ED+C*BA-+Reversing the output_stack we get prefix expression:

-+-AB*C+DE+FG

Task b:
Evaluating the prefix expression:
Assigning the values to variables

A=7
B=6
C=5
D=4
Reading expression from right to left:
reading
1
2
+
3
4
+
5
*
6
7
+
-

operation
1
12
12+
33
334
3 3 4+
375
3 7 5*
3 35 6
3 35 6 7
3 35 6 73 35 1+
3 36 -

E=3

F=2

G=1

Stack_output
Empty
3
3
3
37
37
3 7 35
3 35
3 35
3 35 1
3 36
33

Pop stack_output : 33

Result=33

SP12-BCS-089

Page 3
DATA STRUCTURE ASSIGNMENT #2

Comparing both expressions evaluations:

((A-B)+C*(D+E))-(F+G)
((7-6)+5(4+3))-(2+1)
(1+5*7)-3
1+35-3
36-3
33

SP12-BCS-089

Page 4

More Related Content

PPTX
Context free grammar
PPTX
Linked list
PPTX
Types of Constructor in C++
PPT
Expression evaluation
PPT
PPTX
Unit 3. Input and Output
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PDF
Infix to Prefix (Conversion, Evaluation, Code)
Context free grammar
Linked list
Types of Constructor in C++
Expression evaluation
Unit 3. Input and Output
Conversion of Infix to Prefix and Postfix with Stack
Infix to Prefix (Conversion, Evaluation, Code)

What's hot (20)

PPTX
Stack data structure
PPTX
Syntax Analysis in Compiler Design
PPTX
PDF
file handling c++
PPTX
sorting and its types
PPTX
If else statement in c++
PPTX
Exception Handling in C++
PPT
Function overloading(c++)
PPTX
Circular link list.ppt
PPTX
STACKS IN DATASTRUCTURE
PPTX
Evaluation of prefix expression with example
PPTX
Semantic net in AI
PPTX
Top down parsing
PPTX
File in C language
PPSX
Stacks Implementation and Examples
PDF
Managing I/O in c++
PPTX
Java swing
PPTX
Context free grammar
PDF
Object oriented approach in python programming
Stack data structure
Syntax Analysis in Compiler Design
file handling c++
sorting and its types
If else statement in c++
Exception Handling in C++
Function overloading(c++)
Circular link list.ppt
STACKS IN DATASTRUCTURE
Evaluation of prefix expression with example
Semantic net in AI
Top down parsing
File in C language
Stacks Implementation and Examples
Managing I/O in c++
Java swing
Context free grammar
Object oriented approach in python programming
Ad

Viewers also liked (7)

PPT
Conversion of Infix To Postfix Expressions
PPTX
Infix to postfix
PPTX
Infix-Postfix expression conversion
PPTX
My lecture infix-to-postfix
PDF
Infix prefix postfix expression -conversion
DOC
Infix to-postfix examples
PPTX
Infix to postfix conversion
Conversion of Infix To Postfix Expressions
Infix to postfix
Infix-Postfix expression conversion
My lecture infix-to-postfix
Infix prefix postfix expression -conversion
Infix to-postfix examples
Infix to postfix conversion
Ad

Similar to Conversion from infix to prefix using stack (6)

DOCX
Junaid program assignment
PPTX
Stack_Application_Infix_Prefix.pptx
PDF
Lab Manual IV (1).pdf on C++ Programming practice
PDF
Data structure and algorithm.(dsa)
PPTX
STACK Applications in DS
PDF
How to code?
Junaid program assignment
Stack_Application_Infix_Prefix.pptx
Lab Manual IV (1).pdf on C++ Programming practice
Data structure and algorithm.(dsa)
STACK Applications in DS
How to code?

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Computing-Curriculum for Schools in Ghana
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
Abdominal Access Techniques with Prof. Dr. R K Mishra
Sports Quiz easy sports quiz sports quiz
Computing-Curriculum for Schools in Ghana
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing

Conversion from infix to prefix using stack

  • 1. DATA STRUCTURE ASSIGNMENT #2 Comsats institute Of Information Technology Islamabad Subject : Data Structure Assignment#: 2 Submitted to: Sir Zaheer Ul Hassan Sani Submitted by: Haq Nawaz Reg#: Sp12-bcs-089 Submission date: 2 November 2013 SP12-BCS-089 Page 1
  • 2. DATA STRUCTURE ASSIGNMENT #2 Task a: We have infix expression in the form of: ((A-B)+C*(D+E))-(F+G) Now reading expression from right to left and pushing operators into stack and variables to output stack Input ) G + F ( ) ) E + D ( * C + ) B A ( ( EMPTY SP12-BCS-089 Output_stack EMPTY G G GF GF+ GF+ GF+ GF+ GF+E GF+E GF+ED GF+ED+ GF+ED+ GF+ED+C GF+ED+C* GF+ED+C* GF+ED+C*B GF+ED+C*B GF+ED+C*BA GF+ED+C*BAGF+ED+C*BA-+ GF+ED+C*BA-+- Stack ) ) )+ )+ EMPTY -) -)) -)) -))+ -))+ -) -)* -)* -)+ -)+) -)+) -)+)-)+)-)+ EMPTY Page 2
  • 3. DATA STRUCTURE ASSIGNMENT #2 Out put_stack = GF+ED+C*BA-+Reversing the output_stack we get prefix expression: -+-AB*C+DE+FG Task b: Evaluating the prefix expression: Assigning the values to variables A=7 B=6 C=5 D=4 Reading expression from right to left: reading 1 2 + 3 4 + 5 * 6 7 + - operation 1 12 12+ 33 334 3 3 4+ 375 3 7 5* 3 35 6 3 35 6 7 3 35 6 73 35 1+ 3 36 - E=3 F=2 G=1 Stack_output Empty 3 3 3 37 37 3 7 35 3 35 3 35 3 35 1 3 36 33 Pop stack_output : 33 Result=33 SP12-BCS-089 Page 3
  • 4. DATA STRUCTURE ASSIGNMENT #2 Comparing both expressions evaluations: ((A-B)+C*(D+E))-(F+G) ((7-6)+5(4+3))-(2+1) (1+5*7)-3 1+35-3 36-3 33 SP12-BCS-089 Page 4