Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Infix to postfix conversion Use a loop to read the tokens one by one from a vector infixVect of tokens (strings) representing an infix expression. For.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Data Structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Stacks  Introduction  Applications  Implementations  Complex Applications.
EENG212 Algorithms and Data Structures
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Stacks.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Stack: restricted variant of list
Data Structures – Week #3
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
Stack and Queues Stack implementation using Array
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
CSCE 3110 Data Structures & Algorithm Analysis
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures – Week #3
Infix to postfix conversion
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression If the character x is a left or right parenthesis  If the character is “(“ Push it into the stack  if the character is “)” Repeatedly pop and output all the operators/characters until “(“ is popped from the stack. If the character x is a is a regular operator Step 1: Check the character y currently at the top of the stack. Step 2: If Stack is empty or y=‘(‘ or y is an operator of lower precedence than x, then push x into stack. Step 3: If y is an operator of higher or equal precedence than x, then pop and output y and push x into the stack. When all characters in infix expression are processed repeatedly pop the character(s) from the stack and output them until the stack is empty.

Infix Expression Postfix Expression ( a + b - c ) * d – ( e + f ) Stack Infix to postfix conversion

a + b - c ) * d – ( e + f ) ( Infix Expression Postfix Expression Stack Infix to postfix conversion

+ b - c ) * d – ( e + f ) ( a Infix Expression Postfix Expression Stack Infix to postfix conversion

b - c ) * d – ( e + f ) ( a + Infix Expression Postfix Expression Stack Infix to postfix conversion

- c ) * d – ( e + f ) ( a b + Infix Expression Postfix Expression Stack Infix to postfix conversion

c ) * d – ( e + f ) ( a b + - Infix Expression Postfix Expression Stack Infix to postfix conversion

) * d – ( e + f ) ( a b + c - Infix Expression Postfix Expression Stack Infix to postfix conversion

* d – ( e + f ) a b + c - Infix Expression Postfix Expression Stack Infix to postfix conversion

d – ( e + f ) a b + c - * Infix Expression Postfix Expression Stack Infix to postfix conversion

– ( e + f ) a b + c - d * Infix Expression Postfix Expression Stack Infix to postfix conversion

( e + f ) a b + c – d * - Infix Expression Postfix Expression Stack Infix to postfix conversion

e + f ) a b + c – d * - ( Infix Expression Postfix Expression Stack Infix to postfix conversion

+ f ) a b + c – d * e - ( Infix Expression Postfix Expression Stack Infix to postfix conversion

f ) a b + c – d * e - ( + Infix Expression Postfix Expression Stack Infix to postfix conversion

) a b + c – d * e f - ( + Infix Expression Postfix Expression Stack Infix to postfix conversion

a b + c – d * e f + - Infix Expression Postfix Expression Stack Infix to postfix conversion

a b + c – d * e f + - Infix Expression Postfix Expression Stack Infix to postfix conversion

#include #define STACKSIZE 20 typedef struct{ int top; char items[STACKSIZE]; }STACK; void push(STACK *, char); char pop(STACK *); void main() { int i; char x,y, E[20] ; /* Assume that Infix Expression E contains single-digit integers/parenthesis/operators*/ STACK s; s.top = -1; /* Initialize the stack is */ printf("Enter the Infix Expression:"); scanf("%s",E); for(i=0;E[i] != '\0';i++){ x= E[i];

if(x =’a’) /* Consider all lowercase letter operands from a to z */ printf(“%c”,x); else if(x == ’(’) push(&s,x); else if(x == ’)’){ y=pop(&s) ; while(y != ‘(‘){ printf(“%c”,y); y=pop(&s) ; } else { if(s.top ==-1 || s.items[s.top] == ‘(‘) push(&s,x); else { y = s.items[s.top]; /* y is the top operator in the stack*/ if( y==’*’ || y==’/’){ /* precedence of y is higher/equal to x*/ printf(“%c”, pop(&s)); push(&s,x); }

else if ( y==’+’ || y==’-’) if( x==’+’ || x==’-’) { /* precedence of y is equal to x*/ printf(“%c”, pop(&s)); push(&s,x); } else /* precedence of y is less than x*/ push(&s,x); } while(s.top != -1) printf(“%c”,pop(&s)); } void push(STACK *sptr, char ps) /*pushes ps into stack*/ { if(sptr->top == STACKSIZE-1){ printf("Stack is full\n"); exit(1); /*exit from the function*/ } else sptr->items[++sptr->top]= ps; }

char pop(STACK *sptr) { if(sptr->top == -1){ printf("Stack is empty\n"); exit(1); /*exit from the function*/ } else return sptr->items[sptr->top--]; }