Lists, Stacks, and Queues (plus Priority Queues)

Similar documents
Constructors - Cont. must be distinguished by the number or type of their arguments.

Introduction to Computing II (ITI1121) FINAL EXAMINATION

Lecture 4: Stacks and Queues

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Computer Science Introductory Course MSc - Introduction to Java

Stacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks

Solution suggestions for examination of Logic, Algorithms and Data Structures,

Queues. Principles of Computer Science II. Basic features of Queues

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Distributed Systems Part II Solution to Exercise Sheet 10

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

CMSC 132, Object-Oriented Programming II Summer Lecture 12

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

Declarative Computation Model. Conditional. Case statement. Procedure values (2) Procedure values. Sequential declarative computation model

ARE211, Fall2012. Contents. 2. Linear Algebra (cont) Vector Spaces Spanning, Dimension, Basis Matrices and Rank 8

Black-Box Testing Techniques III

Part I: Definitions and Properties

Algorithmic verification

Graph Search Howie Choset

MI-RUB Exceptions Lecture 7

ITI Introduction to Computing II

Element x is R-minimal in X if y X. R(y, x).

No Solution Equations Let s look at the following equation: 2 +3=2 +7

Design Patterns and Refactoring

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

The L Machines are very high-level, in two senses:

Lock using Bakery Algorithm

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

MIT Loop Optimizations. Martin Rinard

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Decentralized Control of Discrete Event Systems with Bounded or Unbounded Delay Communication

Python. chrysn

Skip Lists. What is a Skip List. Skip Lists 3/19/14

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Voortgezet Programmeren

ITI Introduction to Computing II

GOTO the past / programs choose their own adventure. CSE 505: Programming Languages

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Information Flow Inference for ML

Abstract Data Types. EECS 214, Fall 2017

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

CSC501 Operating Systems Principles. Deadlock

Modular Bisimulation Theory for Computations and Values

Lecture 4 Event Systems

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Exercise 1 (15 points)

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

process arrival time CPU burst time priority p1 0ms 25ms 3 p2 1ms 9ms 1 p3 20ms 14ms 4 p4 32ms 4ms 2

Context Free Languages: Decidability of a CFL

Warmup. A programmer s wife tells him, Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.

Distributed Algorithms (CAS 769) Dr. Borzoo Bonakdarpour

Math 3361-Modern Algebra Lecture 08 9/26/ Cardinality

Correctness of Concurrent Programs

Information System Design IT60105

Experiments with Measuring Time in PRISM 4.0 (Addendum)

Simply Typed Lambda Calculus

Real-Time and Embedded Systems (M) Lecture 5

A particle system takes small time-steps and updates each particle's location and velocity based on the forces acting on it.

CS 21 Decidability and Tractability Winter Solution Set 3

AP Physics 1 Summer Assignment

Outline. PeerSim: Informal introduction. Resources. What is PeerSim? Alberto Montresor Gianluca Ciccarelli

SFWR ENG 3S03: Software Testing

DRIVE vs. BALANCE August 10, By Michael Erlewine

Outline F eria AADL behavior 1/ 78

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Verifying Java-KE Programs

Hoare Logic for Realistically Modelled Machine Code

Homework 5: Parallelism and Control Flow : Types and Programming Languages Fall 2015 TA: Evan Cavallo

COMP 204. Exceptions continued. Yue Li based on material from Mathieu Blanchette, Carlos Oliver Gonzalez and Christopher Cameron

Solutions to EoPL3 Exercises

CS 453 Operating Systems. Lecture 7 : Deadlock

Programming Language Concepts, CS2104 Lecture 3

Data Structures and Algorithms Winter Semester

1 import java. u t i l. ArrayList ; 2 import java. u t i l. L i s t ; 4 /

Two Hanging Masses. ) by considering just the forces that act on it. Use Newton's 2nd law while

Did you read chapter 7? Housekeeping. Special Relativity Postulates. Famous quotes from Einstein. Symmetry. (Special Principle of Relativity) 5/9/2007

Pushing to the Top FMCAD 15. Arie Gurfinkel Alexander Ivrii

MEM380 Applied Autonomous Robots I Fall BUG Algorithms Potential Fields

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Space-aware data flow analysis

EE 144/244: Fundamental Algorithms for System Modeling, Analysis, and Optimization Fall 2016

CPU SCHEDULING RONG ZHENG

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Actors for Reactive Programming. Actors Origins

Übung Informatik I - Programmierung - Blatt 7

INF Models of concurrency

SFWR ENG/COMP SCI 2S03 Principles of Programming

CSE505, Fall 2012, Final Examination December 10, 2012

CS 4100 // artificial intelligence. Recap/midterm review!

Hypothesis Testing. ECE 3530 Spring Antonio Paiva

Clock-driven scheduling

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

Declarative Programming Techniques

Foundations of the X-machine Theory for Testing

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

CSC236 Week 2. Larry Zhang

Transcription:

Lists, Stacks, and Queues (plus Priority Queues) The structures lists, stacks, and queues are composed of similar elements with different operations. Likewise, with mathematics: (Z, +, 0) vs. (Z,, 1) List Stack (LIFO) Queue (FIFO) Priority Queue create empty str. test emptiness add elem to head [push] add elem to end add elem access head elem [top] access front elem access highest prio. elem access tail [pop] top of stack remove front elem remove highest prio. elem modify head modify tail J.Carette (McMaster) CS/SE 2S03 1 / 1

Implementation a Stack via Wrapping a List c l a s s Stack { p r i v a t e L i s t c ; Stack ( f i n a l L i s t x ) { t h i s. c = x ; } } Stack ( ) { t h i s. c=n u l l ; } p u b l i c boolean i s e m p t y ( ) { r e t u r n t h i s. c == n u l l ; } p u b l i c void push ( i n t x ) { t h i s. c = new L i s t ( x, t h i s. c ) ; } p u b l i c i n t top ( ) { r e t u r n t h i s. c. hd ; } p u b l i c void pop ( ) { t h i s. c = t h i s. c. t l ; } } There are two bugs in this code! J.Carette (McMaster) CS/SE 2S03 2 / 1

Functional Stack c l a s s FStack { p r i v a t e f i n a l L i s t c ; FStack ( ) { t h i s. c = n u l l ; } FStack ( f i n a l L i s t l ) { t h i s. c = l ; } s t a t i c boolean i s e m p t y ( f i n a l FStack l ) { r e t u r n l. c == n u l l ; } s t a t i c FStack push ( f i n a l i n t a, f i n a l FStack l ) { r e t u r n new FStack (new L i s t ( a, l. c ) ) ; } s t a t i c FStack pop ( f i n a l FStack l ) { r e t u r n new FStack ( l. c. t l ) ; s t a t i c i n t top ( f i n a l FStack l ) { r e t u r n l. c. hd ; } } Note final FStack l means that l is never modified but its contents may be. J.Carette (McMaster) CS/SE 2S03 3 / 1

Exceptional Circumstances Two exceptional circumstances in FStack executions: pop of an empty stack top of an empty stack How to Deal With them: 1 figure out and deal with usual situations. 2 (Old Style Programming) let weird stuff happen otherwise: (Newer Style Programming) deal with unusual situations explicitly. J.Carette (McMaster) CS/SE 2S03 4 / 1

Exceptions - Cont. Safety conscious (Using Exceptions): 1 figure out unsafe situations. 2 deal with those first. 3 once safe do something usual. s t a t i c i n t top ( f i n a l FStack l ) throws E x c e p t i o n { i f ( l == n u l l ) throw new E x c e p t i o n ( ) ; r e t u r n l. c. hd ; } J.Carette (McMaster) CS/SE 2S03 5 / 1

Catching Exceptions. Specifying what to happen when the function raises an exception: The construct try p catch (Exception e) q; t r y { i n t x = top (new FStack ( ) ) ; } catch E x c e p t i o n e ) { system. out. p r i n t l n ( oh oh! ) ; } The exceptions will propagate until caught. Note. in {p 1 p 2 }, if p 1 throws exception, p 2 is not executed. (like return) J.Carette (McMaster) CS/SE 2S03 6 / 1

Exception Handling in Caml and C. Caml: throw e is written raise e. try with C: There are no exceptions in C. some constructs like long jumps have some similarities. J.Carette (McMaster) CS/SE 2S03 7 / 1

Exception - Cont. We can have new classes of exceptions (you can make your own) We can catch multiple exceptions try-catch-finally: The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. Error Messages in Java: System. out. p r i n t l n ( 1 / 0 ) ; throws the exception: j a v a. l a n g. A r i t h m e t i c E x c e p t i o n : / by z e r o J.Carette (McMaster) CS/SE 2S03 8 / 1

Objects and Classes - Dynamic Methods A class is a type plus some functions (methods) and constructors on that type. J.Carette (McMaster) CS/SE 2S03 9 / 1

Objects and Classes - Dynamic Methods A class is a type plus some functions (methods) and constructors on that type. Dynamic methods: void push ( f i n a l i n t a ) { c = new L i s t ( a, c ) ; } void pop ( ) { c = c. t l ; } We call these methods with p.pop(); p.push(5); dynamic belongs to an object. static belongs to a class. J.Carette (McMaster) CS/SE 2S03 9 / 1

Dynamic Methods - Cont. Stack p = new Stack ( ) ; p. push ( 5 ) ; p. push ( 6 ) ; System. out. p r i n t l n ( p. top ( ) ) ; p. pop ( ) ; The printed result of the above code is 6. If System.out.println(p.top()); is used after p.pop(); then the result is 5. Empty stack is an object with c == null. A common error is to write a dynamic method: L i s t f ( ) { i f ( t h i s == n u l l )... } (this == null) always false!: the method f cannot be called when the object is null. J.Carette (McMaster) CS/SE 2S03 10 / 1

Static Fields When we modify a static field, it is modified for all the class (global fields!). c l a s s M { s t a t i c i n t mem;... J.Carette (McMaster) CS/SE 2S03 11 / 1