Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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.
 Balancing Symbols 3. Applications
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 3 Stacks.
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
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.
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.
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.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
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 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+
Stack Any Other Data Structure Array Linked List
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
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.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Lecture No.06 Data Structures Dr. Sohail Aslam
Stacks.
Stacks.
Data Structures – Week #3
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
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lecture No.07 Data Structures Dr. Sohail Aslam
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures – Week #3
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:

Applications of Stack Maitrayee Mukerji

Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(), Pop(), Top(), IsEmpty() IsFull() Implementation ◦ Arrays ◦ Linked Lists (SLL/DLL)

Stacks Define a Stack ADT Define a Class Stack that can store any type of basic data – integer, character etc.

Applications of Stack Reversing a String Parentheses Matching Arithmetic Expressions ◦ Infix to Postfix conversion ◦ Infix to Prefix conversion ◦ Evaluation of Postfix arithmetic expressions Matching HTML tags Number conversions from ◦ Decimal to other base (2,8,16)

Reversing a String main() { string x = "hello"; stack s; int n = x.length(); //Put characters from x onto the stack for(int i=0; i<n; ++i) s.push(x[i]); //take characters off of stack and put them back into x for(int i=0; !s.empty(); ++i, s.pop()) x[i]=s.top(); cout << x << endl; }

Matching Parentheses Balanced parentheses and brackets ◦ every open parenthesis/bracket is matched with a corresponding close parenthesis ◦ Parenthesis/bracket are properly nested Example (x(y)(z))- balanced a( {(b)}c)- balanced w)(x)- not balanced p({(q)r)}- not balanced

Matching Parentheses Scan expression from left to right, one character at a time If left/opening parenthesis, push corresponding closing parenthesis onto the stack If right/closing parenthesis then ◦ If stack empty then  error, exit ◦ Else  If it matches with the topmost element on the stack then  pop stack and continue  else  report error and exit

Matching Parenthesis Draw the status of the stack for following cases: ◦ a ( b { d e [ f ] g { h } I } j k ) l m ◦ ( a b { c } d ( [ e [ f ] g ] ) ( j ) )

Matching Parenthesis ◦ Reading the string, from Left to Right, the status of stack is: ) a ( b { d e [ f ] g { h } I } j k ) l m ) } ) } ] ) } ) } } ) } )

Arithmetic Expressions A + B : Infix +AB : Prefix AB+ : Postfix Stack Applications ◦ Conversions ◦ Evaluation

Evaluation of Arithmetic Expressions Precedence rules ◦ Exponentiation ◦ Multiplication/Division ◦ Addition / Subtraction Use parentheses to overide precedence rules When operators of same precedence are scanned (without brackets) ◦ the order is assumed to be left to right, the order in which they are read

Example: Infix to Postfix Example 1 A + B * C => (A + (B*C)) => (A + (BC*)) => A (BC*) + => ABC * + Example 2 (A + B) * C => (A + B) *C) => (AB+) * C) => (AB+) C* => AB + C *

Example A + B – C ◦ AB+C- (Postfix) ◦ - + ABC (Prefix) (A+B) * (C-D) ◦ AB+ CD - * (Postfix) ◦ *+AB – CD (Prefix)

Conversion: infix to postfix A + B * C – D / E Output: A + B Pre (*) > Pre (+) * C Pre (-) < Pre (*) * Pre (-) == Pre (+) + - D Pre (/) > Pre (-) / E /-

Conversion: infix to postfix Scan the input string from left to right one character at a time (token) If the token is:  Left “(“  Push on the stack  Operand (number/letter)  Write directly to output string  Right “)”  Pop stack till corresponding “(” is found  If Stack Empty then “error”  Else while stack not empty  pop stack and write to output string

Conversion: infix to postfix Operator: ◦ If stack empty or sTop = “(“ then  push token/ operator onto stack ◦ If precedence (token) > precedence (sTop) then  push token/ operator onto stack ◦ If precedence (token) <= precedence (sTop) then  While stack not empty  Pop stack and write to output  Push token onto stack ◦ If sTop = “)” then  Pop and write till corresponding “(“  If stack empty then “error”  else while stack not empty  pop and write

Conversion: infix to postfix A * B – (C+D) + E ◦ Push (*) ◦ Pre (-) Pop (*) & Push (-) ◦ Push “(“ ◦ Push (+) since STop = “(“ ◦ Since “)” => Pop (+), Pop “(” ◦ Pre (+) == Pre (-) => Pop (-), Push (+) ◦ Since end pop till stack empty AB * CD+ - E+ * - - ( - (+ - +

Evaluating a postfix expression Each operator in a postfix string refers to the previous two operands in the string One of the operand may itself be the result of applying a previous operator

Evaluating a postfix expression using a Stack Read the input string from Left to Right Each time an operand is read-> ◦ Push it on the stack When an operator is read ◦ Pop the two operands from the top of the stack ◦ Perform the indicated operation on the two operands ◦ Push the result back onto the stack, so that it is available as an operand for the next operator

Evaluating a postfix expression using a Stack – Example * ◦ Push (6) ◦ Push (3) + ◦ Pop (3) ◦ Pop (6) ◦ Calculate ( 6+3 = 9) ◦ Push (9) ◦ Push (2) * ◦ Pop (2) ◦ Pop (9) ◦ Calculate (9 * 2 = 18) ◦ Push (18)

Conversion: Infix to Prefix Reverse the input string Scan the input string from left to right one character at a time (token) Call modified infix to postfix algorithm Reverse the output string to get the prefix expression

Conversion: Infix to Prefix If token is Left “(“ Push on the stack Operand (number/letter) Write directly to output string Right “)” Pop stack till corresponding “(” is found If Stack Empty then “error” Else while stack not empty pop stack and write to output string

Conversion: Infix to Prefix Operator If stack empty or sTop = “(“ then push token/ operator onto stack If precedence (token) >= precedence (sTop) then push token/ operator onto stack If precedence (token) < precedence (sTop) then while stack not empty Pop stack and write to output Push token onto stack If sTop = “)” then Pop and write till corresponding “(“ If stack empty then “error” else while stack not empty pop and write

Conversion: Infix to Prefix A – (B/C) * D- Infix A – (/BC) * D A – [(/BC) * D] A - */BCD -A*/BCD- Prefix

Conversion: Infix to Prefix Infix: A – (B/C) * D Reverse:D * (C/B) – A Convert to postfix: ◦D◦D ◦ Push (*) ◦ Push “(“ ◦ DC ◦ Push (/) since sTop == “(“ ◦ DCB ◦ Pop (/), Pop “(“  => DCB/ ◦ Pre (-) Pop (*)  => DCB/*,  Push (-) ◦ DCB/*A ◦ Pop (-) => DCB/*A- Reverse:- A * / B C D * * ( * (/ * -

Evaluating Prefix Read the input string from Right to Left Each time an operand is read-> ◦ Push it on the stack When an operator is read ◦ Pop the two operands from the top of the stack ◦ Perform the indicated operation on the two operands ◦ Push the result back onto the stack, so that it is available as an operand for the next operator

Assignments Matching HTML tags Converting numbers from Decimal to Binary