EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

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

Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
Stacks Chapter 11.
Prefix, Postfix, Infix Notation
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
Stacks Example: Stack of plates in cafeteria.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
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.
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
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Data Structures and Algorithms
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Stack Applications Qamar Rehman.
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.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
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.
Stacks Stacks.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures and Algorithms
CSC 172 DATA STRUCTURES.
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks.
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures and Algorithms 2/2561
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:

EC-211 DATA STRUCTURES LECTURE 8

STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Postfix Example: Infix: A+(B*C) Convert to Postfix A+(B*C) =A+(BC*) =ABC*+ which is the required postfix form

Postfix Example: Infix: (A+B)*(C-D) Convert to Postfix (A+B)*(C-D) =(AB+)*(CD-) =AB+CD-* which is the required postfix form

Evaluating a Postfix Expression Each operator in a postfix expression refers to the previous two operands in the expression. Each time we reach an operand we push it onto a stack. When we reach an operator, its operands will be the top two elements on the stack. We then pop these two elements, perform the indicated operation on them, and push the result on the stack. In the end, the result of the expression is left as the only element in the stack, which is popped and returned as the result of evaluation

Algorithm for Evaluating a Postfix Expression opndstk=the empty stack; //scan the input string one element a time into symb while (not end of input) { symb=next input character; if (symb is an operand) push(opndstk, symb); else { //symb is an operator opnd2=pop(opndstk); opnd1=pop(opndstk); value=result of applying symb to opnd1 and opnd2; push(opndstk, value); }//end else }//end while return (pop(opndstk));

Example symbopnd1opnd2valueopndstk 66 26,2 36,2, , ,3 81,3,8 21,3,8,2 /8241,3, ,7 * / + *

Converting an Expression from Infix to Postfix opstk=the empty stack; while (not end of input){ symb=next input character; if (symb is an operand) add symb to the postfix string else{ while (!empty(opstk) && (operator at top of the stack has precedence greater than or equal symb)){ topsymb=pop(opstk); add topsymb to the postfix string; } push(opstk, symb); }//end else }//end while //output any remaining operator while (!empty(opstk){ topsymb=pop(opstk); add topsymb to the postfix string; }//end while

Example SymbPostfix stringopstk AA +A+ BAB+ * +* CABC+* ABC*+ ABC*+ Convert to Postfix:A+B*C

Example SymbPostfix stringopstk AA *A* BAB* +AB*+ CAB*C+ * +* DAB*CD+* AB*CD*+ AB*CD*+ Convert to Postfix:A*B+C*D

CLASS WORK Suppose S is a non-empty stack of integers. Write a main() function that appropriately utilizes calls to push, pop, and empty to remove the bottom element from the stack. Rest of the stack stays the same. Remember, you do not know the number of elements currently present in stack Prototypes of stack functions: – void push( int) – int pop(); – bool empty();

Solution void main() { stack T; int element; while (!S.empty()) { element=S.pop(); T.push( element); } element=T.pop(T); while (!T.empty(){ element=T.pop(); S.push( element); }