17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.

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.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
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.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
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.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
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.
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.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
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(),
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.
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Data Structures – Week #3
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.
Stacks, Queues, and Deques
More About Stacks: 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
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures – Week #3
More About Stacks: Stack Applications
Stacks.
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:

17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED

17CS1102 (Data Structures) Topic: Applications of stack Indicator:4 At the end of this session, Student will be able to: Apply stack to check if the given expression has balanced symbols or not. (balancing symbols) Evaluate Postfix Expression Convert Infix to Postfix Apply stack in function calls

Applications of Stacks Matching Balancing Parenthesis Evaluating postfix expression Infix to postfix expression Function Calls Applications of Stacks

Matching Balancing Parenthesis Given an expression of ‘(‘ and ‘)’, a ‘(‘ must match with a ‘)’, or else it is illegal. ( )( ), ( ( ( ) ) ), ( ( ( ) ) ( ) ) are all legal, while ( ( ) (, ) ( ) ( are all illegal. Obviously, counting the numbers of ‘(‘ and ‘)’ in the expression is not enough. Applications of Stacks

Matching Balancing Parenthesis Insights  By convention, the expression is reading from left-to-right. A ‘)’ is matched with the closest, unmatched ‘(‘ on its left. For example, ( ( ( ) ) ( ) ) Applications of Stacks

Matching Balancing Parenthesis Suppose we have a ‘)’. How do we know which ‘(’ is closest and unmatched? If we read the expression from left-to-right, the MOST RECENTLY UNMATCHED ‘(’ is cancelled with ‘)’. How can we keep track of the MOST RECENTLY READ (LAST) ‘(’ ? (keep in mind there are many pending unmatched ‘(’). Which data structure is keeping track of the most recent item ? Stack  LIFO structure Applications of Stacks

Matching Balancing Parenthesis When we see a ‘(‘, we push it into a stack. When we see a ‘)’, we pop ‘(‘ from the stack. This ‘(‘ matches with current ‘)’. What happen if the stack is empty (meaning there is no such ‘(’) at that moment ? What happen if we have finished reading the expression, but the stack is not empty ? It means there are more ‘(‘ than ‘)’ in the expression. It means the input is illegal. It means the input is illegal. Applications of Stacks

Postfix Expression Evaluation Infix expression Suppose the expression only has operators ‘*’ and ‘+’; and ‘*’ has a higher precedence than ‘+’. So, 5+2+3 = 10, and 1+2*4=9, etc. The expression may also have parenthesis to complicate the problem, i.e., (1+2)*4=12 (1+2*5+1)*3=36. (1+2*(5+1))*3=39. Applications of Stacks

Postfix Expression Evaluation 1 3 + 1 2 4 * + 1 2 + 4 * 6 5 2 3 + 8 * + 3 + * are all postfix expressions. No ‘(‘, ‘)’ is in the expression. To evaluate a postfix expression, we need a stack. Applications of Stacks

Postfix Expression Evaluation Evaluate a postfix expression. Read the expression from left-to-right. When a number is seen, it is pushed onto the stack. When an operator is seen, the operator is applied to the two numbers that are popped from the stack. The result is pushed on the stack. Applications of Stacks

Infix to Postfix Expressions Example : 6 5 2 3 + 8 * + 3 + * Applications of Stacks

Infix to Postfix Expressions How to convert an infix expression to a postfix expression? Use a stack. Read the infix expression from left-to-right. When an operand (number) is read, output it. If an operator (other than ‘(‘, ‘)’) is read, pop the stack (and output the operator) until a lower precedence operator or ‘(‘ is on the top of stack. Then push the current operator on the stack. If ‘(‘ is read, push it on the stack. If a ‘)‘ is read, pop the stacks (and output the operators), until we meet ‘(’. Pop that ‘(’ (do no output it). Finally, if we read the end of the expression, pop the stack (and output the operators) until the stack is empty. Applications of Stacks

Infix to Postfix Expressions Example : (1+2*(5+1))*3 postfix expression  1 2 5 1 + * + 3 * Applications of Stacks