Stack Applications Qamar Rehman.

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

Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
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.
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.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
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.
Section 5.2 Defining Languages. © 2005 Pearson Addison-Wesley. All rights reserved5-2 Defining Languages A language –A set of strings of symbols –Examples:
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.
Evaluation of Expressions
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)
2 -1 Chapter 2 The stack Stack and Queue Stack: Last-In-First-Out (LIFO) Last-Come-First-Serve (LCFS) only one end Queue: First-In-First-Out (FIFO)
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)
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
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.
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.
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.
Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
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 CS-102
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
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.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures and Algorithms
Lecture No.06 Data Structures Dr. Sohail Aslam
CSC 172 DATA STRUCTURES.
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks.
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
PART II STACK APPLICATIONS
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
Data Structures – Week #3
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:

Stack Applications Qamar Rehman

Example of Stacks INFIX, POSTFIX and PREFIX Infix: A+B-C Postfix: AB+C- Prefix: -+ABC Order of Precedence of Operators Exponentiation Multiplication/Division Addition/Subtraction

Infix: ( (A+B)*C-(D-E) ) $ (F+G) Conversion to Postfix Expression ( (AB+)*C-(DE-) ) $ (FG+) ( (AB+C*)-(DE-) ) $ (FG+) (AB+C*DE--) $ (FG+) AB+C*DE- -FG+$ Exercise: Convert the following to Postfix ( A + B ) * ( C – D) A $ B * C – D + E / F / (G + H)

Conversion to Prefix Expression The precedence rules for converting an expression from infix to prefix are identical. The only change from postfix is that the operator is placed before the operands rather than after them. Evaluating a Postfix Expression Each operator in a postfix string refers to the previous two operands in the string.

Algorithm to Evaluate a Postfix Expression Example: Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 + opndstk = the empty stack /* scan the input string reading one element */ /* at 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 */ op2 = pop(opndstk); op1 = pop(opndstk); value = result of applying symb to op1 & op2 push(opndstk, value); } /* end else */ } /* end while */ return (pop(opndstk)); symb opnd1 opnd2 value opndstk 6 2 6,2 3 6,2,3 + 5 6,5 - 1 1,3 8 1,3,8 1,3,8,2 / 4 1,3,4 7 1,7 * 7,2 $ 49 49,3 52

Conversion of Infix Expression to postfix A+B*C = ABC*+ (A+B)*C = AB+C* There must be a precedence function. prcd(op1, op2), where op1 and op2 are characters representing operators. This function returns TRUE if op1 has precedence over op2 when op1 appears to the left of op2 in an infix expression without parenthesis. prcd(op1,op2) returns FALSE otherwise. For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’) is FALSE. prcd(‘$’,’$’) = FALSE prcd( ‘(‘ , op) = FALSE for any operator op prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’ prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘ prcd( ‘)‘ ,op ) = undefined for any operator op (an error)

Algorithm to Convert 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { Example-1: A+B*C symb Postfix string opstk A + B AB * + * C ABC ABC* ABC*+

Algorithm to Convert 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { Example-2: (A+B)*C symb Postfix string opstk ( A + ( + B AB ) AB+ * C AB+C AB+C*

Algorithm to Convert 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { Example-2: (A+B)*C symb Postfix string opstk ( A + ( + B AB ) AB+ * C AB+C AB+C*

Algorithm to Convert 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { Example-3: ( (A-(B+C) ) *D ) $ (E+F)

Algorithm to Convert Infix to Postfix Example-3: ( (A-(B+C) ) *D ) $ (E+F) 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { symb Postfix string opstk ( (( A - ((- ((-( B AB + ((-(+ C ABC ) ABC+ ABC+- * (* D ABC+-D ABC+-D* $ $( E ABC+-D*E $(+ F ABC+-D*EF ABC+-D*EF+ ABC+-D*EF+$

Algorithm to Convert 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) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ If (empty(opstk) || symb != ‘)’ ) push(opstk, symb); else } /* end else */ /* output any remaining operators */ while (!empty(opstk) ) { Example-4: ( A + B ) * ( C – D) symb Postfix string opstk

Assignment # 2 Develop an expression evaluator that read input from file and perform following operations: Infix to postfix and prefix Prefix to infix and Postfix Postfix to infix and Prefix Due Date : 28/10/13