Review Use of Stack Introduction Stack in our life Stack Operations

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.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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.
D ATA S TRUCTURE MSc.IT Ali Abdul Karem Habib. S TACK A PPLICATIONS Web browser: stores the addresses of recently visited sites on a stack. Each time.
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
Department of Technical Education Andhra Pradesh
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
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.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
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.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Stack Applications Qamar Rehman.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Reverse Polish Notation Written by J.J. Shepherd.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Data Structures & Algorithm CS-102
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Lecture No.06 Data Structures Dr. Sohail Aslam
Objectives In this lesson, you will learn to: Define stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
Stacks.
Stack application: postponing data usage
Algorithms and Data Structures
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.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
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.
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

Review Use of Stack Introduction Stack in our life Stack Operations Stack Implementation Stack Using Array Stack Using Linked List Use of Stack

Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix

Prefix, Infix, Postfix Two other ways of writing the expression are + A B prefix (Polish Notation) A B + postfix (Reverse Polish Notation) The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Consider the infix expression A + B * C We “know” that multiplication is done before addition. The expression is interpreted as A + ( B * C ) Multiplication has precedence over addition. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Precedence of Operators The five binary operators are: addition, subtraction, multiplication, division and exponentiation. The order of precedence is (highest to lowest) Exponentiation  Multiplication/division *, / Addition/subtraction +, - A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Precedence of Operators For operators of same precedence, the left-to-right rule applies: A+B+C means (A+B)+C. For exponentiation, the right-to-left rule applies A  B  C means A  ( B  C ) A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ End of lecture 6

Infix to Postfix Note that the postfix form an expression that does not require parenthesis. Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. The postfix forms are: 4+3*5 435*+ (4+3)*5 43+5* A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’. The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Handling parenthesis When an open parenthesis ‘(‘ is read, it must be pushed on the stack When an operand is read, place it in the postfix string When an operator is read, check the priority and then act accordingly When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string Both the ‘(‘ and the ‘)’ must be discarded A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: (A + B) * C symb postfix stack ( ( A A ( + A ( + B AB ( + ) AB + * AB + * C AB + C * AB + C * A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Summary Polish Notation Precedence of Operators Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix