Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Similar documents
STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Formal Languages and Automata Theory, SS Project (due Week 14)

Prefix/Infix/Postfix Notation

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Lab 7 1 Due Thu., 6 Apr. 2017

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CS 206 Introduction to Computer Science II

Stack Abstract Data Type

Stacks. Revised based on textbook author s notes.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT

CISC-235. At this point we fnally turned our atention to a data structure: the stack

Lecture Data Structure Stack

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

STACKS AND QUEUES. Problem Solving with Computers-II

CS W3134: Data Structures in Java

Content: Learning Objectives

Infix to Postfix Conversion

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

DEEPIKA KAMBOJ UNIT 2. What is Stack?

BBM 201 DATA STRUCTURES

The Stack and Queue Types

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

BBM 201 DATA STRUCTURES

Types of Data Structures

Linear Data Structure

Lecture 12 ADTs and Stacks

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Lecture No.04. Data Structures

Stacks and their Applications

Stacks II. Adventures in Notation. stacks2 1

CSE 214 Computer Science II Stack

Data Structures Week #3. Stacks

Stack and Its Implementation

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

March 13/2003 Jayakanth Srinivasan,

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Lecture 4 Stack and Queue

A Simple Syntax-Directed Translator

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

12 Abstract Data Types

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

Stacks Fall 2018 Margaret Reid-Miller

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

PA3 Design Specification

This book is licensed under a Creative Commons Attribution 3.0 License

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

LECTURE 17. Expressions and Assignment

Stacks, Queues (cont d)

Assessment of Programming Skills of First Year CS Students: Problem Set

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

CDA 3103 Computer Organization Homework #7 Solution Set

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

infix expressions (review)

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Stacks Calculator Application. Alexandra Stefan

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Operators & Expressions

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering

An Introduction to Trees

Postfix (and prefix) notation

CS 211 Programming Practicum Spring 2018

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSE 230 Intermediate Programming in C and C++

17CS33:Data Structures Using C QUESTION BANK

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stating the obvious, people and computers do not speak the same language.

Data Structure - Stack and Queue-

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

[CS302-Data Structures] Homework 2: Stacks

Lecture Chapter 6 Recursion as a Problem Solving Technique

CS 211 Programming Practicum Spring 2017

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Problem with Scanning an Infix Expression

Algorithms and Data Structures

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

UNIT 3

CISC

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Transcription:

Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1

Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2

What is Polish Notation? Conventionally, we use the operator symbol between its two operands in an arithmetic expression. A+B C D*E A*(B+C) We can use parentheses to change the precedence of the operators. Operator precedence is pre-defined. This notation is called INFIX notation. Parentheses can change the precedence of evaluation. Multiple passes required for evaluation. Spring Semester 2007 Programming and Data Structure 3

Polish notation Named after Polish mathematician Jan Lukasiewicz. Polish POSTFIX notation Refers to the notation in which the operator symbol is placed after its two operands. AB+ CD* AB*CD+/ Polish PREFIX notation Refers to the notation in which the operator symbol is placed before its two operands. +AB *CD /*AB-CD Spring Semester 2007 Programming and Data Structure 4

How to convert an infix expression to Polish form? Write down the expression in fully parenthesized form. Then convert stepwise. Example: A+(B*C)/D-(E*F)-G (((A+((B*C)/D))-(E*F))-G) Polish Postfix form: A B C * D / + E F * - G - Polish Prefix form: Try it out. Spring Semester 2007 Programming and Data Structure 5

Advantages: No concept of operator priority. Simplifies the expression evaluation rule. No need of any parenthesis. Hence no ambiguity in the order of evaluation. Evaluation can be carried out using a single scan over the expression string. Using stack. Spring Semester 2007 Programming and Data Structure 6

Evaluation of a Polish Expression Can be done very conveniently using a stack. We would use the Polish postfix notation as illustration. Requires a single pass through the expression string from left to right. Polish prefix evaluation would be similar, but the string needs to be scanned from right to left. Spring Semester 2007 Programming and Data Structure 7

while (not end of string) do { a = get_next_token(); if (a is an operand) push (a); if (a is an operator) { y = pop(); x = pop(); push (x a y); } } return (pop()); Spring Semester 2007 Programming and Data Structure 8

Parenthesis Matching Spring Semester 2007 Programming and Data Structure 9

The Basic Problem Given a parenthesized expression, to test whether the expression is properly parenthesized. Whenever a left parenthesis is encountered, it is pushed in the stack. Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match. Works for multiple types of parentheses ( ), { }, [ ] Spring Semester 2007 Programming and Data Structure 10

while (not end of string) do { a = get_next_token(); if (a is ( or { or [ ) push (a); if (a is ) or } or ] ) { if (isempty()) { print ( Not well formed ); exit(); } x = pop(); if (a and x do not match) { print ( Not well formed ); exit(); } } } if (not isempty()) print ( Not well formed ); Spring Semester 2007 Programming and Data Structure 11

Converting an INFIX expression to POSTFIX Spring Semester 2007 Programming and Data Structure 12

Basic Idea Let Q denote an infix expression. May contain left and right parentheses. Operators are: Highest priority: ^ (exponentiation) Then: * (multiplication), / (division) Then: + (addition), (subtraction) Operators at the same level are evaluated from left to right. In the algorithm to be presented: We begin by pushing a ( in the stack. Also add a ) at the end of Q. Spring Semester 2007 Programming and Data Structure 13

The Algorithm (Q:: given infix expression, P:: output postfix expression) push ( ( ); Add ) to the end of Q; while (not end of string in Q do) { a = get_next_token(); if (a is an operand) add it to P; if (a is ( ) push(a); if (a is an operator) { Repeatedly pop from stack and add to P each operator (on top of the stack) which has the same or higher precedence than a ; push(a); } Spring Semester 2007 Programming and Data Structure 14

if (a is ) ) { Repeatedly pop from stack and add to P each operator (on the top of the stack) until a left parenthesis is encountered; } } Remove the left parenthesis; Spring Semester 2007 Programming and Data Structure 15

Q: A + (B * C (D / E ^ F) * G) * H ) Q A + ( B * C - ( D / E ^ F ) STACK ( ( + ( + ( ( + ( ( + ( * ( + ( * ( + ( - ( + ( - ( ( + ( - ( ( + ( - ( / ( + ( - ( / ( + ( - ( / ^ ( + ( - ( / ^ ( + ( - Output Postfix String P A A A A B A B A B C A B C * A B C * A B C * D A B C * D A B C * D E A B C * D E A B C * D E F A B C * D E F ^ / Spring Semester 2007 Programming and Data Structure 16

Q STACK Output Postfix String P * G ) * H ) ( + ( - * ( + ( - * ( + ( + * ( + * A B C * D E F ^ / A B C * D E F ^ / G A B C * D E F ^ / G * - A B C * D E F ^ / G * - A B C * D E F ^ / G * - H A B C * D E F ^ / G * - H * + Spring Semester 2007 Programming and Data Structure 17

Some Other Applications Spring Semester 2007 Programming and Data Structure 18

Reversing a string of characters. Generating 3-address code from Polish postfix (or prefix) expressions. Handling function calls and returns, and recursion. Spring Semester 2007 Programming and Data Structure 19