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

Similar documents
Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

CSCD 326 Data Structures I Stacks

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

CS350: Data Structures Stacks

CSIS 10B Lab 2 Bags and Stacks

Basic Data Structures

Announcements Queues. Queues

Basic Data Structures 1 / 24

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

A queue is a linear collection whose elements are added on one end and removed from the other

Queue rear tail front head FIFO

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

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

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

LECTURE OBJECTIVES 6-2

Stacks. Common data structures - useful for organizing data for specific tasks Lists Stacks - an Abstract Data Type

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

CMPT 225. Lecture 9 Stack

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

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

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Inheritance. Transitivity

CS/ENGRD 2110 SPRING 2018

Java Review: Objects

Today's Topics. CISC 458 Winter J.R. Cordy

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

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

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

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.

Data Structures And Algorithms

Introduction to Computer Science II (ITI 1121) Midterm Examination

Exceptions and Design

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

Stacks Fall 2018 Margaret Reid-Miller

Derive From A Linear List Class. Stacks. Derive From ArrayLinearList. Derive From ArrayLinearList. Derive From ArrayLinearList.

CSC 273 Data Structures

Midterm Exam 2 CS 455, Spring 2011

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CmpSci 187: Programming with Data Structures Spring 2015

Stacks and Queues. David Greenstein Monta Vista

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

ECE Fall st Midterm Examination (120 Minutes, closed book)

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

CSE 143 SAMPLE MIDTERM

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.

CE204 Data Structures and Algorithms Part 2

Stacks and Their Applications

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Dynamic Memory Allocation

Has-a Relationships. A pen has a or contains a ball

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

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

1. Stack Implementation Using 1D Array

Subclassing for ADTs Implementation

Topic 6: Inner Classes

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Array Based Lists. Collections

ITI Introduction to Computing II

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

What can go wrong in a Java program while running?

Midterm Exam (REGULAR SECTION)

ITI Introduction to Computing II

Stacks and Queues. Chapter Stacks

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

An Introduction to C++

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Introduction to Linked Data Structures

NATIONAL UNIVERSITY OF SINGAPORE

CIS Fall Data Structures Midterm exam 10/16/2012

CS 211. Project 5 Infix Expression Evaluation

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

About this exam review

+ Abstract Data Types

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

csci 210: Data Structures Stacks and Queues

Standard ADTs. Lecture 19 CS2110 Summer 2009

CSE 143. Lecture 4: Stacks and Queues

IT 4043 Data Structures and Algorithms

16 Multiple Inheritance and Extending ADTs

Array. Prepared By - Rifat Shahriyar

ITI Introduction to Computing II

Data Structures G5029

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

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

infix expressions (review)

Building Java Programs

Top of the Stack. Stack ADT

COMP 103 Introduction to Data Structures and Algorithms

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Transcription:

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

Implementing Stacks With Linked Lists Overview: The LinkedStack Class from L&C The Fields and Constructors The push Method The pop Method The Other Methods L&C s Version of the Maze Search

Overview: The LinkedStack Class From L&C Now that we know how linear linked structures work, we can see that they are well suited to implement a stack. We only need to change pointers on elements near the top to push or pop, giving us O(1) time for these operations in the worst case. (The ArrayStack needed O(n) time to resize on some pushes, though these operations only took O(1) time per push on average.) We ll use the LinearNode class the we defined last lecture, where a node points to the next node and points to its contents, from some class T. The ArrayStack already kept track of its size through the field top, but we will need a field to keep the size of our stack explicitly.

The Fields and Constructors We have two fields, the more important of which is the pointer that starts the linear structure. We also keep track of the number of elements in the stack. Note that constructors for the generic class do not have the <T> in their name when they are declared, even if they depend on T (as this one doesn t). I got this wrong earlier. But at any given time there can be only one type version of a generic around, because two classes can t have the same name in the same scope. public class LinkedStack<T> implements StackADT<T>{ private int count; private LinearNode<T> top; public LinkedStack( ) { count = 0; top = null;}

The push Method The basic idea is simple -- we create a new node with the contents provided, then link it into the top of the stack and update the size. Although the constructor had no <T> when we defined it, it has one now when we are calling it -- the compiler needs to know that we are creating something that can fit into a variable of the type LinearNode<T>. This is of course an O(1) time operation -- we have no idea how big the stack might be when we do this. public void push (T element) { LinearNode<T> temp = new LinearNode<T> (element); temp.setnext(top); top = temp; count ++;}

The pop Method To pop, all we need to do is save a pointer to the top element, reset the top pointer to bypass that element, and update the size counter. But if the stack happens to be empty we need to throw the exception, and include a throws clause to let this be handled in the calling method if desired. public T pop ( ) throws EmptyCollectionException { if (isempty( )) throw new EmptyCollectionException( Stack ); T result = top.getelement( ); top = top.getnext( ); count--; return result;}

The Other Methods The rest of the five basic StackADT methods are simple and also O(1) time. The tostring( ) method would naturally take O(n) time, as we want to print something for each element of the stack -- exactly what would be a style decision but it would include getelement( ).tostring( ) for each node in turn. Clearly we would want to use only peeks, not pushes or pops. public T peek ( ) throws EmptyCollectionException { if (isempty( )) throw new EmptyCollectionException( Stack ); return top.getelement( );} public boolean isempty ( ) {return (top == null);} public int size ( ) {return count;}

L&C s Version of the Maze Search In Section 4.5 L&C create a Maze class where the entries of the twodimensional array are Integer objects, holding numbers that are code for our open and seen boolean fields. They put Position objects on the stack, where a Position is an (x, y) pair. But they only search for paths from the top left to the bottom right. Note StackADT<Position> = new LinkedStack<Position>( ) -- they keep the stack in a StackADT<Position> variable so that the rest of the code doesn t care which implementation is used. We could replace LinkedStack<Position> with ArrayStack<Position> in this one line and the rest of the code would work perfectly well with the new implementation. Our use of Stack<SCell> committed us to arrays.