Stacks Fall 2018 Margaret Reid-Miller

Similar documents
Queues Fall 2018 Margaret Reid-Miller

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

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

Stack Abstract Data Type

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

Stacks. Revised based on textbook author s notes.

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

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

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

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

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

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.

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

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

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

Stack and Its Implementation

CS350: Data Structures Stacks

Project 1: Implementation of the Stack ADT and Its Application

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

Lecture 12 ADTs and Stacks

The Stack and Queue Types

Top of the Stack. Stack ADT

March 13/2003 Jayakanth Srinivasan,

STACKS AND QUEUES. Problem Solving with Computers-II

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Top of the Stack. Stack ADT

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

CS 211 Programming Practicum Spring 2017

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

Linear Data Structure

Stacks, Queues (cont d)

CS 211 Programming Practicum Spring 2018

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

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

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Stacks, Queues and Hierarchical Collections

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Lab 7 1 Due Thu., 6 Apr. 2017

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

CMPT 225. Lecture 9 Stack

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Data Structures And Algorithms

Lecture Data Structure Stack

CSE 214 Computer Science II Stack

CS 206 Introduction to Computer Science II

Stacks and their Applications

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

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks and Queues. David Greenstein Monta Vista

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

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

CSC 273 Data Structures

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

CSE 143. Lecture 4: Stacks and Queues

Lists are great, but. Stacks 2

CMSC 132: Object-Oriented Programming II. Stack and Queue

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

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

csci 210: Data Structures Stacks and Queues

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

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

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

Postfix (and prefix) notation

Programming Abstractions

Problem with Scanning an Infix Expression

Assignment 5. Introduction

Stack Abstract Data Type. Section 3.1

Programming Abstractions

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CS 211 Programming Practicum Fall 2018

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

[CS302-Data Structures] Homework 2: Stacks

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

CS W3134: Data Structures in Java

1. Stack Implementation Using 1D Array

infix expressions (review)

CMPS 390 Data Structures

Lecture No.04. Data Structures

Plan for Week. Linear structures: Stack, Queue. Friday: More examples of recursion and alternatives. Guide to using practice-it for APT-like problems

An Introduction to Trees

12 Abstract Data Types

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

CSC 222: Computer Programming II. Spring 2005

CS 206 Introduction to Computer Science II

Building Java Programs

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Midterm Exam 2 CS 455, Spring 2011

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Interfaces & Generics

More Data Structures (Part 1) Stacks

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Plan for Week. What is a linear structure? Stacks Queues and Deques, Oh My!

CSC148H Week 3. Sadia Sharmin. May 24, /20

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

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

Transcription:

Stacks 15-121 Fall 2018 Margaret Reid-Miller

Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack Application Fall 2018 15-121 (Reid-Miller) 2

Recursive add public void add(e obj) { if (head == null) head = new Node(obj, null); else add(head, obj); } // precondition: list!= null private void add(node list, E obj) { if (list.next == null) list.next = new Node(obj, null); else add(list.next, obj); } Fall 2018 15-121 (Reid-Miller) 3

Elegant Recursive add public void add(e obj) { head = add(head, obj); } private Node add(node list, E obj) { if (list == null) return new Node(obj, null); else list.next = add(list.next, obj); return list; } Fall 2018 15-121 (Reid-Miller) 4

Interlude: ADT vs. Data Structure Abstract Data Types Formal description of the behavior (semantics) of a data type. Our first ADT: List (an ordered sequence of elements that can be accessed by index). Data Structures A concrete representation/organization of data e.g., array, class, ArrayList, LinkedList Fall 2018 15-121 (Reid-Miller) 5

Fall 2018 15-121 (Reid-Miller) 6

Stack ADT last in, first out (LIFO) Operations: PUSH adds an element onto the top of the stack POP removes the element from the top of the stack PEEK returns the element on the top of the stack without removing it (optional operation). Test if the stack is EMPTY Why have a Stack ADT? Less power than arrays or Lists Use them for more discipline, simple, fast operations Fall 2018 15-121 (Reid-Miller) 7

Stack last in, first out (LIFO) Uses: A word processor s undo feature Parenthesis balancing Evaluating subexpressions, like: 2*4 + 6*3 The run-time stack (activation records/stack frames) Fall 2018 15-121 (Reid-Miller) 8

Possible interface for Stack public interface LIFOStack<E> { void push(e obj); E pop(); E peek(); boolean isempty(); } Fall 2018 15-121 (Reid-Miller) 9

Stack Implementations Want O(1) for all operations, so what underlying data structure can we use? array? Yes, if top is last element of array ArrayList? Yes, if top is size()-1 linked list? Yes, with top at head of the list Fall 2018 15-121 (Reid-Miller) 10

Array Implementation fields public class ArrayStack<E> implements LIFOStack<E>{ private E[] dataarray; private int top; } // methods (next slide) Index of top stack element in array Fall 2018 15-121 (Reid-Miller) 11

Array Implementation Constructor & isempty public ArrayStack<E>() { } dataarray = (E[]) new Object[1]; top = ; -1 Indicates an empty stack public boolean isempty() { return ( ); top == -1 } Fall 2018 15-121 (Reid-Miller) 12

Array Implementation push public void push(e obj) { } if ( ) top == dataarray.length()-1 reallocate(); top = top + 1; dataarray[top] = obj; Fall 2018 15-121 (Reid-Miller) 13

Array Implementation pop // Complete pop method public E pop() { if ( ) throw new EmptyStackException(); } Fall 2018 15-121 (Reid-Miller) 14

Array Implementation pop public E pop() { } if (top < 0) throw new EmptyStackException(); E obj = dataarray[top]; dataarray[top] == null; top--; return obj; Fall 2018 15-121 (Reid-Miller) 15

Array Implementation peek public E peek() { if (top < 0) } throw new EmptyStackException(); return dataarray[top]; Fall 2018 15-121 (Reid-Miller) 16

Nested Parentheses Goal: Determine if a string containing a mathematical expression is nested correctly. Parenthetical symbols include {}, [], (). Algorithm: Process the expression from left to right. If left parenthetical symbol, push it on the stack. If right parenthetical symbol, pop the stack to see if it is a matching left parenthetical symbol. If not, the parenthetical nesting is invalid. When done processing, if the stack is empty, the parenthetical nesting is valid. Fall 2018 15-121 (Reid-Miller) 17

Examples Fall 2018 15-121 (Reid-Miller) 18

RPN (Postfix Notation) Some modern calculators use Reverse Polish Notation (RPN) Developed in 1920 by Jan Lukasiewicz Can write mathematical formulas without using parentheses. Example: 5 * ( 6 + 7 ) becomes in RPN: 5 6 7 + * Fall 2018 15-121 (Reid-Miller) 19

Evaluating postfix with a stack Note: Assume the postfix expression is valid. Each entry in a postfix expression is a token. 1. Let S be an empty stack. 2. For each token in the expression: a. If the token is a number, push it on the stack S. b. Otherwise (the token is an operator): i. Pop a token off stack S and store it in y. ii. Pop a token off stack S and store it in x. iii. Evaluate x operator y. iv. Push result on stack S. 3. Pop the stack for the final answer. Fall 2018 15-121 (Reid-Miller) 20

Evaluating postfix: Example Postfix (RPN): 5 7 + 8 6 - / 3 9 * + Fall 2018 15-121 (Reid-Miller) 21

Converting Infix to Postfix Note: Assume the infix expression is valid. Each entry in a infix expression is a token. 1. Let S be an empty stack. 2. Let postfix be an empty string. 3. For each token in the infix expression: a. If the token is a number, append it to the postfix string. b. Otherwise (the token is an operator) Process the operator (see next 2 slides) 4. While the stack S is not empty: a. Pop an operator off the stack S and append it to the posfix string. Fall 2018 15-121 (Reid-Miller) 22

Processing the operators (converting infix to postfix, cont d) Each operator has a precedence. *, / multiplicative higher precedence +, - additive lower precedence Operators of the same precedence are evaluated left to right Assign precedence values to each operator. Operator * Precedence 2 / 2 + 1-1 Fall 2018 15-121 (Reid-Miller) 23

Process the operator (converting infix to postfix, cont d) Let nextop be the operator token. 1. If stack S is empty, push nextop on stack S. 2. Otherwise: a. Let topop = peek(s). b. If prec(nextop) > prec(topop), push nextop on stack S. c. Otherwise: i. While S is not empty and prec(nextop) prec(topop): a) Let topop = pop(s). b) Append topop to postfix string. c) If stack S is not empty, let topop = peek(s). ii. Push nextop on stack S. Fall 2018 15-121 (Reid-Miller) 24

Converting: An example Infix: 5 + 4 * 3 / 2-1 Fall 2018 15-121 (Reid-Miller) 25

Processing Parentheses (converting infix to postfix, cont d) Each pair of parentheses marks a subexpression that must be completely converted before previous operators are processed. Assign the lowest precedence to left and right parentheses so only a right parenthesis can pop off a left parenthesis. Do not put parentheses in the postfix string. Operator Precedence * 2 / 2 + 1-1 ( -1 ) -1 Fall 2018 15-121 (Reid-Miller) 26

Process the operator - revised (converting infix to postfix, cont d) Let nextop be the operator token. 1. If stack S is empty or nextop is (, push nextop on stack S. 2. Otherwise: a. Let topop = peek(s). b. If prec(nextop) > prec(topop), push nextop on stack S. c. Otherwise: i. While S is not empty and prec(nextop) prec(topop): a) Let topop = pop(s). b) If topop is (, exit loop immediately. c) Append topop to postfix string. d) If stack S is not empty, let topop = peek(s). ii. If nextop is not ), push nextop on stack S. Fall 2018 15-121 (Reid-Miller) 27

Converting: An example Infix: 7 + 5 * ( 4 / 2 + 1) - 6 Fall 2018 15-121 (Reid-Miller) 28

Invalid expressions Assume that tokens are only operators and integers. Evaluating a postfix expression: How would you know that the postfix expression is not valid? Converting an infix expression to postfix: How would you know that the infix expression is not valid? Fall 2018 15-121 (Reid-Miller) 29