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

Similar documents
Stacks. Revised based on textbook author s notes.

Stack Abstract Data Type

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

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

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

Lecture 12 ADTs and Stacks

March 13/2003 Jayakanth Srinivasan,

Stacks and their Applications

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

Stack and Its Implementation

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.

Stacks Fall 2018 Margaret Reid-Miller

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

STACKS AND QUEUES. Problem Solving with Computers-II

The Stack and Queue Types

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

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

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

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

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

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

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

Lab 7 1 Due Thu., 6 Apr. 2017

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

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CS 211 Programming Practicum Spring 2017

PA3 Design Specification

Stacks Calculator Application. Alexandra Stefan

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

Programming Abstractions

CS 211. Project 5 Infix Expression Evaluation

Linear Data Structure

Lecture Data Structure Stack

CS 211 Programming Practicum Spring 2018

CS 206 Introduction to Computer Science II

CSE 143. Lecture 4: Stacks and Queues

Stacks, Queues (cont d)

Problem with Scanning an Infix Expression

Data Structures and Algorithms

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

CS 206 Introduction to Computer Science II

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

1. Stack Implementation Using 1D Array

CS 211 Programming Practicum Fall 2018

CIS Fall Data Structures Midterm exam 10/16/2012

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

CSC 222: Computer Programming II. Spring 2005

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Lecture 4 Stack and Queue

Monday, November 13, 2017

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

COMP 250. Lecture 8. stack. Sept. 25, 2017

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Top of the Stack. Stack ADT

Assignment 5. Introduction

CISC

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

15. Stacks and Queues

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

COMP 250 Winter stacks Feb. 2, 2016

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

infix expressions (review)

Top of the Stack. Stack ADT

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

[CS302-Data Structures] Homework 2: Stacks

Infix to Postfix Conversion

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Building Java Programs

CSE 214 Computer Science II Stack

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Data Structure. Recitation IV

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

Advanced Java Concepts Unit 3: Stacks and Queues

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

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

A Simple Syntax-Directed Translator

Queues Fall 2018 Margaret Reid-Miller

Syntax and Grammars 1 / 21

An Introduction to Trees

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

12 Abstract Data Types

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

DEEPIKA KAMBOJ UNIT 2. What is Stack?

A simple syntax-directed

1. Algorithm. Data Structures & Algorithms. How many times you weight? Algo 2 : Half Cutting. How many times you weight? Algo.1 :Two coins at a time

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

Complexity, General. Standard approach: count the number of primitive operations executed.

Data Structures Week #3. Stacks

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

ECE Spring 2018 Problem Set #0 Due: 1/30/18

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Transcription:

Topic 15 Implementing and Using "stack n. The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted several times in the middle of a conversation, "My stack overflowed" means "I forget what we were talking about." -The Hacker's Dictionary Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Sharper Tools Lists 2

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 are limited: push (add item to stack) pop (remove top item from stack) top (get top item without removing it) isempty Described as a "Last In First Out" (LIFO) data structure 3

Stack Operations Assume a simple stack for integers. Stack<Integer> s = new Stack<Integer>(); s.push(12); s.push(4); s.push( s.top() + 2 ); s.pop(); s.push( s.top() ); //what are contents of stack? 4

Stack Operations Write a method to print out contents of stack in reverse order. 5

Uses of The runtime stack used by a process (running program) to keep track of methods in progress Search problems Undo, redo, back, forward 6

What is Output? Stack<Integer> s = new Stack<Integer>(); // put stuff in stack for(int i = 0; i < 5; i++) s.push( i ); // Print out contents of stack // while emptying it. // Assume there is a size method. for(int i = 0; i < s.size(); i++) System.out.print( s.pop() + ); A 0 1 2 3 4 D 2 3 4 B 4 3 2 1 0 C 4 3 2 E No output due to runtime error 7

Corrected Version Stack<Integer> s = new Stack<Integer>(); // put stuff in stack for(int i = 0; i < 5; i++) s.push( i ); // print out contents of stack // while emptying it int limit = s.size(); for(int i = 0; i < limit; i++) System.out.print( s.pop() + ); //or // while(!s.isempty() ) // System.out.println( s.pop() ); 8

Implementing a stack need an underlying collection to hold the elements of the stack 2 obvious choices array (native or ArrayList) linked list Adding a layer of abstraction. A big idea. array implementation linked list implementation 9

Applications of

Mathematical Calculations What does 3 + 2 * 4 equal? 2 * 4 + 3? 3 * 2 + 4? The precedence of operators affects the order of operations. A mathematical expression cannot simply be evaluated left to right. A challenge when evaluating a program. Lexical analysis is the process of interpreting a program. What about 1-2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3 11

Infix and Postfix Expressions The way we are use to writing expressions is known as infix notation Postfix expression does not require any precedence rules 3 2 * 1 + is postfix of 3 * 2 + 1 evaluate the following postfix expressions and write out a corresponding infix expression: 2 3 2 4 * + * 1 2 3 4 ^ * + 1 2-3 2 ^ 3 * 6 / + 2 5 ^ 1-12

Clicker Question 2 What does the following postfix expression evaluate to? 6 3 2 + * A. 18 B. 36 C. 24 D. 11 E. 30 13

Evaluation of Postfix Expressions Easy to do with a stack given a proper postfix expression: get the next token if it is an operand push it onto the stack else if it is an operator pop the stack for the right hand operand pop the stack for the left hand operand apply the operator to the two operands push the result onto the stack when the expression has been exhausted the result is the top (and only element) of the stack 14

Infix to Postfix Convert the following equations from infix to postfix: 2 ^ 3 ^ 3 + 5 * 1 11 + 2-1 * 3 / 3 + 2 ^ 2 / 3 Problems: Negative numbers? parentheses in expression 15

Infix to Postfix Conversion Requires operator precedence parsing algorithm parse v. To determine the syntactic structure of a sentence or other utterance Operands: add to expression Close parenthesis: pop stack symbols until an open parenthesis appears Operators: Have an on stack and off stack precedence Pop all stack symbols until a symbol of lower precedence appears. Then push the operator End of input: Pop all remaining stack symbols and add to the expression 16

Simple Example Infix Expression: 3 + 2 * 4 PostFix Expression: Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 17

Simple Example Infix Expression: + 2 * 4 PostFix Expression: 3 Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 18

Simple Example Infix Expression: 2 * 4 PostFix Expression: 3 Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 19

Simple Example Infix Expression: * 4 PostFix Expression: 3 2 Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 20

Simple Example Infix Expression: 4 PostFix Expression: 3 2 Operator Stack: + * Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 21

Simple Example Infix Expression: PostFix Expression: 3 2 4 Operator Stack: + * Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 22

Simple Example Infix Expression: PostFix Expression: 3 2 4 * Operator Stack: + Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 23

Simple Example Infix Expression: PostFix Expression: 3 2 4 * + Operator Stack: Precedence Table Symbol Off Stack On Stack Precedence Precedence + 1 1-1 1 * 2 2 / 2 2 ^ 10 9 ( 20 0 24

Example 11 + 2 ^ 4 ^ 3 - ((4 + 5) * 6 ) ^ 2 Show algorithm in action on above equation 25

Balanced Symbol Checking In processing programs and working with computer languages there are many instances when symbols must be balanced { }, [ ], ( ) A stack is useful for checking symbol balance. When a closing symbol is found it must match the most recent opening symbol of the same type. Applicable to checking html and xml tags! 26

Algorithm for Balanced Symbol Checking Make an empty stack read symbols until end of file if the symbol is an opening symbol push it onto the stack if it is a closing symbol do the following if the stack is empty report an error otherwise pop the stack. If the symbol popped does not match the closing symbol report an error At the end of the file if the stack is not empty report an error 27

Algorithm in practice list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) + foo( list[i - 1] ) ) / 2 * ) - list[ method(list[0])]; Complications when is it not an error to have non matching symbols? Processing a file Tokenization: the process of scanning an input stream. Each independent chunk is a token. Tokens may be made up of 1 or more characters 28