Class 26: Linked Lists

Size: px
Start display at page:

Download "Class 26: Linked Lists"

Transcription

1 Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many data structures that we are also going to discuss and implement in a simpler way in class. You are welcome to use the standard Java implementations in problem sets. They are more elaborate and abstract than the implementations we will develop. Learning how to implement a few data structures makes you more sophisticated in how you use all data structures. We will cover the Java implementations in a later lecture. 1

2 Goals We will examine and build a new kind of data structure (a Linked List) using linking rather than arrays. The most important theme, however, is that implementation affects how useful a data structure is as much as its interface (set of methods). 3 Lists as an Abstract Data Type A list is a collection of elements that has a particular order. It can have arbitrary length. You should be able to insert or delete an element anywhere. You should be able to go through the list in order an element at a time. 4 2

3 A List Interface public interface List<E> public boolean isempty(); public void addfirst( E e ); public void addlast( E e ); public boolean contains( E e ); public boolean remove( E e ); public E removefirst() throws NoSuchElementException; public void clear(); public int size(); 5 Lists and Ordinal Position There are certain obvious things we would like to do with lists that we can't do using only this interface. Two examples: How would you access the elements of the list if you did not know them in advance? How would you insert an item into the list at any other position than the beginning and the end? One approach is to number or index the positions in the list. 6 3

4 Indexed Lists We could then add three methods public E get( int n ) throws IndexOutOfBoundsException; public E remove( int n ) throws IndexOutOfBoundsException; public void add( E e, int n ) throws IndexOutOfBoundsException; The following code block will traverse an indexed list, mylist: for ( int i = 0; i < mylist.size(); i++ ) E e = mylist.get( i );... 7 Indexed Lists, 2 Lists implemented using arrays (e.g., Java s ArrayList) often provide such methods because they are easy to implement. The idea of using an index to access list members can lead to problems, however. Since the index depends on ordinal position, it can change every time an item is added to or deleted from the list. If the list is not implemented on top of an indexed data structure like an array, accessing an indexed element is slow. When we use large lists like a telephone directory in everyday life, we don't consider the index of an entry, only its relative position. 8 4

5 Iterators An iterator is a helper class designed to be used with a List or other collection class. Think of it as a movable bookmark. It has methods to return the collection's members one at a time. Iterators can also implement methods that allow the collection to be modified relative to the current position of the iterator. 9 SListIterator Interface public interface SListIterator<E> public boolean hasnext(); public E next() throws NoSuchElementException; public void remove() throws IllegalStateException; public void add( E e ); public void set( E e ) throws IllegalStateException 10 5

6 Iterator Methods The type of iterator we present here returns a new element and advances to the next with the same operation, next(). There is no way to go back using this interface. The Java ListIterator allows you to go forwards and backwards. The most recent element returned by next() is the current element. remove() will delete the current element from the underlying collection. set() will change it. add() will insert a new element after the current element and before the element that would be returned by the following call to next(). After a call to add(), the inserted element becomes the new current. A call to next() will return the element after the inserted one. The first call to next() returns the first element of the list. Before the first call to next(), there is no current element. 11 Iterator and its Underlying List An iterator is an object based on an underlying collection so we need a way to create an iterator for a collection. We do this by adding a method to our List interface: public SListIterator slistiterator(); Can you have 2 iterators over the same List? 12 6

7 Using an Iterator List<String> mylist = new SLinkedList<String>();... SListIterator<String> iter = mylist.slistiterator();... while ( iter.hasnext() ) String s = iter.next(); An SListIterator in Action New Iterator After 1st call to current next() green red purple orange undefined green red purple orange current is green After adding After 2nd call to black current is next() black green black red purple orange green black red purple orange current is red 14 7

8 An SListIterator in Action, 2 After calling remove() green black purple orange current is undefined After 3rd call to next() green black purple orange current is purple 15 List Implementation Stategies There are many ways to implement a list. In array-based implementations like the Java Arraylist inserting an element at any place except the end of the list is very expensive because all the elements from the point of insertion until the end must be moved back to make room for the new entry. There is a similar problem with deletion. For this reason, lists frequently use a linked implementation (e.g., Java s LinkedList) 16 8

9 Singly Linked Lists Linked lists are like freight trains. Each item to be put on the list is contained in an instance of a new type of object called the link corresponding to a freight car. In a singly linked list, the link not only contains the listed item, but it also points to the next item in the list as one freight car is coupled to the next. The last link in the list points to nothing. 17 Singly Linked List Diagram List first last Link 1 Link 2... Link n null Item 1 Item 2 Item n 18 9

10 Singly Linked Lists, 2 The list object itself points to the link holding the first listed item, and often holds an additional reference to the last link of the list to make it easier to append items. We say that a link contains or points to, and the list instance points or holds a pointer. In Java these are all synonyms for holding a reference. So the link doesn't really contain the item. It simply holds a reference to the listed item. The last link holds a null reference in the field that points to the next element. 19 A Singly Linked List Demonstration List: SListIterator: SLinkedList: SLinkedListApp: SLinkedListView: IteratorView: List interface List iterator interface List implementation main() application List GUI List iterator GUI 20 10

11 The Link Inner Class public class SLinkedList<E> implements List<E> private static class SLink<E> E item; SLink<E> next; SLink( E e, SLink<E> n ) item = e; next = n; SLink( E e ) this( e, null ); The SLinkedList Data Members Only first is necessary. last and length could be found by traversing the list, but having these members and keeping them up to date makes the calls size() and append() much faster. private int length = 0; private SLink<E> first = null; private SLink<E> last = null; 22 11

12 == vs the Object equals method contains( E e ) and remove( E e ) must search for E e in the list. What does it means to find it? Must the list contain a reference to the identical object ( ==)? Or is it sufficient that the list contain a reference to an object that is equal but possibly distinct? static private <E> boolean objectequals( E a, E b ) if ( a == null ) return ( b == null ); else return a.equals( b ); 23 Beware the Special Case The tricky part about implementing a linked list is not implementing the normal case for each of the methods, for instance, removing an object from the middle of the list. What's tricky is making sure that your methods will work in the exceptional and boundary cases. For each method, you should think through whether the implementation will work on an empty list, a list with only one or two elements, on the first element of a list, on the last element of a list

13 addfirst(), before List first last Link 1... Link n null Item 1 Item n 25 addfirst(), after List first last Link 0 Link 1... Link n null Item 0 Item 1 Item n 26 13

14 addfirst(), special case before List first last after List first last null Link 1 null Item 1 27 addfirst(object o) public void addfirst(e e) if ( first == null ) // if the list is empty first = last = new SLink<E>( e ); else first = new SLink<E>( e, first ); length++; 28 14

15 removefirst(), before List first last Link 1 Link 2... Link n null Item 1 Item 2 Item n 29 removefirst(), after List first last Link 1 Link 2... Link n null Item 1 Item 2 Item n 30 15

16 removefirst(), special case before after List List first last first last Link 1 null Link 1 null Item 1 Item 1 31 removefirst() public E removefirst() throws NoSuchElementException if ( first == null ) // if list is empty throw new NoSuchElementException(); else SLink<E> t = first; first = first.next; // if list had 1 element and is now empty if ( first == null ) last = null; length--; return t.item; 32 16

17 SListIterator as Inner Class public class SLinkedList<E>... public SListIterator<E> slistiterator() return new SLinkedListIterator(); public class SLinkedListIterator implements SListIterator<E> contains() public boolean contains(e e) SListIterator<E> iter = new SLinkedListIterator(); while ( iter.hasnext() ) if ( objectequals( iter.next(), e )) return true; return false; 34 17

18 Linked List Uses and Variations Since our List interface possesses addlast() and removefirst() methods you can implement a trivial queue on top of the SLinkedList concrete data type. How would the implementation be changed if each link had a back reference (previous) as well as a forward reference (next)? Such lists are called (you guessed it) doubly linked lists. What operations become easier? 35 Linked Lists vs. Array Lists Lists that are relatively static (few additions or deletions) are frequently more efficient as array lists. Lists accessed through ordinal position are usually implemented as array lists. Highly dynamic lists are often more efficient as linked lists. The time for many basic operations on a linked list (adding an element, removing an element) are fixed, but can vary widely for array lists

19 Iterator Caution Let's carry out a thought experiment. You create and populate a list. You create an iterator over that list. You call removefirst() on the list to delete the first element of the list. Now call next() on the iterator. What happens? What should happen? Although our implementation is reasonably robust, iterators assume that they are called over a fixed list. That is, they do NOT guarantee correct results if you modify the list after iterator construction using either list or another iterator instances methods. How might you "fix" this? What does fixing mean? Would it be better to have an iterator that always gave "correct" results or an iterator that threw an exception if the underlying list had been modified? 37 LinkedList, Exercise Download the Java files in Lecture26.zip from the class web site and save them to a new directory. Create a new Eclipse project based on the Lecture26 files. Compile and run. SLinkedListApp contains main(). Play with the simulation. Look at the implementation in SLinkedList and List. The rest of the files manage the simulation. You don't need to look at them unless you are curious. The addlast and remove buttons don't work because the corresponding method implementations have been removed

20 LinkedList, Exercise, 2 Look at the addfirst() method, and then write addlast(). Beware of special cases ( what are they?). Compile and test using the simulation. Now look at the contains() method, and then write remove(). Compile and test. The main List view also has a new red button labelled "double". Click it. It doesn't do anything. Yet. Clicking the double button calls a method in a new class ListUtil: public static void doublelist( SLinkedList<integer> l ) doublelist() is almost empty. Write an implementation for doublelist() that uses an iterator for the list, l, and doubles each Integer on the list. Compile and test

Active Learning: Streams

Active Learning: Streams Lecture 29 Active Learning: Streams The Logger Application 2 1 Goals Using the framework of the Logger application, we are going to explore three ways to read and write data using Java streams: 1. as text

More information

An Introduction to Data Structures

An Introduction to Data Structures An Introduction to Data Structures Advanced Programming ICOM 4015 Lecture 17 Reading: Java Concepts Chapter 20 Fall 2006 Adapded from Java Concepts Companion Slides 1 Chapter Goals To learn how to use

More information

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes. CS231 - Spring 2017 Linked Lists List o Data structure which stores a fixed-size sequential collection of elements of the same type. o We've already seen two ways that you can store data in lists in Java.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract

More information

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC Implementing a List in Java CSC 143 Java List Implementation Using Arrays Updated with Java 5.0 Generics Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked

More information

Outline. iterator review iterator implementation the Java foreach statement testing

Outline. iterator review iterator implementation the Java foreach statement testing Outline iterator review iterator implementation the Java foreach statement testing review: Iterator methods a Java iterator only provides two or three operations: E next(), which returns the next element,

More information

Topic 10: The Java Collections Framework (and Iterators)

Topic 10: The Java Collections Framework (and Iterators) Topic 10: The Java Collections Framework (and Iterators) A set of interfaces and classes to help manage collections of data. Why study the Collections Framework? very useful in many different kinds of

More information

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

COMP 250. Lecture 8. stack. Sept. 25, 2017 COMP 250 Lecture 8 stack Sept. 25, 2017 1 get(i) set(i,e) add(i,e) remove(i) remove(e) clear() isempty() size() What is a List (abstract)? // Returns the i-th element (but doesn't remove it) // Replaces

More information

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology Lists and Iterators CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology Announcements Should be making progress on VectorGraphics. Should have turned in CRC cards,

More information

Tutorial 2: Linked Lists

Tutorial 2: Linked Lists Tutorial 2: Linked Lists ? 2 Agenda Introduction of Linked List Arrays Vs Linked Lists Types of Linked Lists Singly Linked List Doubly Linked List Circular Linked List Operations Insertion Deletion 3 Data

More information

Lecture 4. The Java Collections Framework

Lecture 4. The Java Collections Framework Lecture 4. The Java s Framework - 1 - Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework - 2 - Learning Outcomes From this lecture you should

More information

The Java Collections Framework. Chapters 7.5

The Java Collections Framework. Chapters 7.5 The Java s Framework Chapters 7.5 Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework Outline Introduction to the Java s Framework Iterators Interfaces,

More information

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Iterators What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

Class 27: Nested Classes and an Introduction to Trees

Class 27: Nested Classes and an Introduction to Trees Introduction to Computation and Problem Solving Class 27: Nested Classes and an Introduction to Trees Prof. Steven R. Lerman and Dr. V. Judson Harward Goals To explain in more detail the different types

More information

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding

More information

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

Recursive Objects. Singly Linked List (Part 2)

Recursive Objects. Singly Linked List (Part 2) Recursive Objects Singly Linked List (Part 2) 1 Operations at the head of the list operations at the head of the list require special handling because there is no node before the head node 2 Adding to

More information

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 DDL & Queue Lecture 20 October 22, 2018 Prof. Zadia Codabux 1 Agenda Mid Semester Analysis Doubly Linked List Queue 2 Administrative None 3 Doubly Linked

More information

Linked Lists

Linked Lists Linked Lists 2-17-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? Can you tell me what a linked list is and what

More information

Lecture 27. Binary Search Trees. Binary Search Trees

Lecture 27. Binary Search Trees. Binary Search Trees Lecture Binary Search Trees Binary Search Trees In the previous lecture, we defined the concept of binary search tree as a binary tree of nodes containing an ordered key with the following additional property.

More information

doubly linked lists Java LinkedList

doubly linked lists Java LinkedList COMP 250 Lecture 5 doubly linked lists Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev element Each node has a reference to the next node and to the previous node. head tail 2 class DNode

More information

CS 3 Introduction to Software Engineering. 5: Iterators

CS 3 Introduction to Software Engineering. 5: Iterators CS 3 Introduction to Software Engineering 5: Iterators Questions? 2 PS1 Discussion Question You are to choose between two procedures, both of which compute the minimum value in an array of integers. One

More information

Notes on access restrictions

Notes on access restrictions Notes on access restrictions A source code file MyClass.java is a compilation unit and can contain at most one public class. Furthermore, if there is a public class in that file, it must be called MyClass.

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util; package weiss.util; // Fig 6.9-10, pg 192-4. public interface Collection extends java.io.serializable int size( ); boolean isempty( ); boolean contains( Object x ); boolean add( Object x ); boolean remove(

More information

List ADT. B/W Confirming Pages

List ADT. B/W Confirming Pages wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array

More information

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss The Collections API Mark Allen Weiss Lecture Objectives To learn how to use the Collections package in Java 1.2. To illustrate features of Java that help (and hurt) the design of the Collections API. Tuesday,

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

More information

Section Lists and Sets

Section Lists and Sets Section 10.2 Lists and Sets IN THE PREVIOUS SECTION, we looked at the general properties of collection classes in Java. In this section, we look at some specific collection classes and how to use them.

More information

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

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Data Structure: Lists and Iterators 2017 Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Contents Data structures to be covered in this lecture Array lists (aka Vectors) Node lists

More information

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists. Announcements MODULE WEB-SITE: http://www.csc.liv.ac.uk/ michele/teaching/comp102/comp102.html FIRST ASSIGNMENT DEADLINE: Wednesday, February 1st, 14.30, LAB 7 Boxes (late submissions to be left in the

More information

Class 32: The Java Collections Framework

Class 32: The Java Collections Framework Introduction to Computation and Problem Solving Class 32: The Java Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward Goals To introduce you to the data structure classes that come

More information

Iterators and Sequences

Iterators and Sequences Iterators and Sequences Iterators and Sequences 1 Iterators An iterator abstracts the process of scanning through a collection of elements It maintains a cursor that sits between elements in the list,

More information

CH7. LIST AND ITERATOR ADTS

CH7. LIST AND ITERATOR ADTS CH7. LIST AND ITERATOR ADTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) LISTS LIST ADT A List

More information

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Data structure is an organization of information, usually in memory, for better algorithm efficiency. Lecture 01 Introduction Wednesday, 29 July 2015 12:59 pm Data structure is an organization of information, usually in memory, for better algorithm efficiency. Abstract data types (ADTs) are a model for

More information

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017 COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element Each node has a reference to the next node and to the previous node. head

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2015S-05 Abstract Data Types and Lists David Galles Department of Computer Science University of San Francisco 05-0: Abstract Data Types Recall that an Abstract Data

More information

CS S-05 Abstract Data Types and Lists 1

CS S-05 Abstract Data Types and Lists 1 CS245-2016S-05 Abstract Data Types and Lists 1 05-0: Abstract Data Types Recall that an Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface

More information

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk How Can We Improve the State of Experimental Evaluation in Computer Siene Peter Sweeney IBM Researh, TJ Watson Friday, April 12, 12:00 Kendade 307 1 Doubly LinkedList is Symmetrial! insert insert remove

More information

Linked List Nodes (reminder)

Linked List Nodes (reminder) Outline linked lists reminders: nodes, implementation, invariants circular linked list doubly-linked lists iterators the Java foreach statement iterator implementation the ListIterator interface Linked

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/22/04 Lecture 4 1 Containers Powerful tool for programming data structures Provides a library of container classes to hold your objects 2

More information

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/30/03 Lecture 7 1 Linear Lists It is an ordered collection of elements. Lists have items, size or length. Elements may have an index. Main

More information

Implementing Lists, Stacks, Queues, and Priority Queues

Implementing Lists, Stacks, Queues, and Priority Queues Implementing Lists, Stacks, Queues, and Priority Queues CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To design common features of lists in

More information

In this lecture Simple List Algorithms

In this lecture Simple List Algorithms In this lecture Simple List Algorithms 15-211 Fundamental Data Structures and Algorithms Ananda Guna & Klaus Sutner January 16, 2003 Based on lectures given by Peter Lee, Avrim Blum, Danny Sleator, William

More information

Advanced Java Concepts Unit 2: Linked Lists.

Advanced Java Concepts Unit 2: Linked Lists. Advanced Java Concepts Unit 2: Linked Lists. The List interface defines the structure of a linear collection. Here are some of its methods. boolean add( E element ) Appends the element to the end of the

More information

Lecture 7: Implementing Lists, Version 2

Lecture 7: Implementing Lists, Version 2 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 7: Implementing Lists, Version 2 Contents 1 The Impact of addfirst on Lists 1 2 Mutating List Contents 2 2.1 The Basic List Classes...................................

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

Introduction to Programming in C Department of Computer Science and Engineering

Introduction to Programming in C Department of Computer Science and Engineering Introduction to Programming in C Department of Computer Science and Engineering In this lecture, we will see slightly more advanced data type, then a singly link list. We will briefly go over one or two

More information

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3. Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);

More information

Insertion Sort: an algorithm for sorting an array

Insertion Sort: an algorithm for sorting an array Insertion Sort: an algorithm for sorting an array Let s use arrays to solve a problem that comes up often in programming, namely sorting. Suppose we have an array of objects that is in no particular order

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

More information

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Lecture 16 Jérôme Waldispühl School of Computer Science McGill University Slides by Mathieu BlancheIe Recap from last lecture

More information

CS Introduction to Data Structures Week 1 Thursday

CS Introduction to Data Structures Week 1 Thursday CS 367 - Introduction to Data Structures Week 1 Thursday We assume that you are proficient at object-oriented programming in Java. Please enroll in CS300 if you do not know Java and have not written object-oriented

More information

Lists. The List ADT. Reading: Textbook Sections

Lists. The List ADT. Reading: Textbook Sections Lists The List ADT Reading: Textbook Sections 3.1 3.5 List ADT A list is a dynamic ordered tuple of homogeneous elements A o, A 1, A 2,, A N-1 where A i is the i-th element of the list The position of

More information

Inner Classes. CMSC 433 Programming Language Technologies and Paradigms Spring Example: The Queue Class. Example: The Queue Class (cont d)

Inner Classes. CMSC 433 Programming Language Technologies and Paradigms Spring Example: The Queue Class. Example: The Queue Class (cont d) CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Inner Classes & Iterators Mar. 6, 2007 Inner Classes Classes can be nested inside other classes These are called inner classes Within

More information

CS61BL Summer 2013 Midterm 2

CS61BL Summer 2013 Midterm 2 CS61BL Summer 2013 Midterm 2 Sample Solutions + Common Mistakes Question 0: Each of the following cost you.5 on this problem: you earned some credit on a problem and did not put your five digit on the

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 24, 2013 Abstract These lecture notes are meant to be looked

More information

Lab 5 Random numbers!

Lab 5 Random numbers! Lab 5 Random numbers! ADT:s as Programming Tools! D0010E! Lecture 13! Iterators! MasterMind Reminder: Groups must have their designs approved before any actual programming is allowed to start. Some review

More information

CSC 321: Data Structures. Fall 2017

CSC 321: Data Structures. Fall 2017 CSC 321: Data Structures Fall 2017 Linked structures nodes & recursive fields singly-linked list doubly-linked list LinkedList implementation iterators 1 ArrayLists vs. LinkedLists to insert or remove

More information

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures Collections CSE 143 Java Collections Most programs need to store and access collections of data Collections are worth studying because... They are widely useful in programming They provide examples of

More information

Collections and Iterators. Collections

Collections and Iterators. Collections Collections and Iterators Based on the notes from David Fernandez-Baca and Steve Kautz Based on The Java Tutorial (http://docs.oracle.com/javase/tutorial/java/) Bryn Mawr College CS206 Intro to Data Structures

More information

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013 Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Model Solutions COMP 103: Test

More information

CMSC 206: Data Structures Final Exam Reference May 2018

CMSC 206: Data Structures Final Exam Reference May 2018 CMSC 206: Data Structures Final Exam Reference May 2018 public interface BMCSet /** Adds a new item to the set * @param item The new item to add to the set * @return true if the item is a new item added

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I What are they? Abstraction of a list: i.e. a sequence of nodes in which each node is linked to the node

More information

Introducing Design Patterns

Introducing Design Patterns Introducing Design Patterns Prof. Dr. Eric Dubuis Berner Fachhochschule, @ Biel Course "UML and Design Patterns" of module "Software Engineering and Design", version April 2009 (X) BFH/TI/Software Engineering

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

Linked lists (6.5, 16)

Linked lists (6.5, 16) Linked lists (6.5, 16) Linked lists Inserting and removing elements in the middle of a dynamic array takes O(n) time (though inserting at the end takes O(1) time) (and you can also delete from the middle

More information

Need to access each element of a list. Use index to access an element of the list. It s quadratic, while:

Need to access each element of a list. Use index to access an element of the list. It s quadratic, while: ! " # $ $ % & ' () * + (, -. /* 0 * / (1 2 3-4 1 5 6 5 ' 7 8 * / 9 : ; < 8 * /' 8 - /4 J K L M N O PQ M R S After studying this chapter you should understand the following: the role of iterators in container

More information

Introduction to Computer Science II (ITI 1121) Final Examination

Introduction to Computer Science II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École d ingénierie et de technologie de l information University of Ottawa Faculty of Engineering School of Information Technology and Engineering Introduction to Computer

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

Introducing Design Patterns

Introducing Design Patterns Introducing Design Patterns Prof. Dr. Eric Dubuis, @ Biel Course "UML and Design Patterns" of module "Software Engineering and Design", version October 2007 BFH/TI/UML and Design Patterns/Software Engineering

More information

CS11 Java. Winter Lecture 8

CS11 Java. Winter Lecture 8 CS11 Java Winter 2010-2011 Lecture 8 Java Collections Very powerful set of classes for managing collections of objects Introduced in Java 1.2 Provides: Interfaces specifying different kinds of collections

More information

CS/CE 2336 Computer Science II

CS/CE 2336 Computer Science II CS/CE 2336 Computer Science II UT D Session 11 OO Data Structures Lists, Stacks, Queues Adapted from D. Liang s Introduction to Java Programming, 8 th Ed. 2 What is a Data Structure? A collection of data

More information

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019 CS18 Integrated Introduction to Computer Science Fisler Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019 Contents 1 Overview of Generic/Parameterized Types 2 2 Double the Fun with Doubly-Linked Lists

More information

Linked List. ape hen dog cat fox. tail. head. count 5

Linked List. ape hen dog cat fox. tail. head. count 5 Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points

More information

Fun facts about recursion

Fun facts about recursion Outline examples of recursion principles of recursion review: recursive linked list methods binary search more examples of recursion problem solving using recursion 1 Fun facts about recursion every loop

More information

EECS 2011 M: Fundamentals of Data Structures

EECS 2011 M: Fundamentals of Data Structures EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19 Iterators and

More information

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering Readings and References Java Collections References» "Collections", Java tutorial» http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Winter 2003 Software Engineering http://www.cs.washington.edu/education/courses/403/03wi/

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

Lists. ordered lists unordered lists indexed lists 9-3

Lists. ordered lists unordered lists indexed lists 9-3 The List ADT Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations 9-2 Lists A list is a

More information

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Related classes Consider classes for

More information

COMP 250 Midterm #2 March 11 th 2013

COMP 250 Midterm #2 March 11 th 2013 NAME: STUDENT ID: COMP 250 Midterm #2 March 11 th 2013 - This exam has 6 pages - This is an open book and open notes exam. No electronic equipment is allowed. 1) Questions with short answers (28 points;

More information

Linked Lists. Chapter 12.3 in Savitch

Linked Lists. Chapter 12.3 in Savitch Linked Lists Chapter 12.3 in Savitch Preliminaries n Arrays are not always the optimal data structure: q An array has fixed size needs to be copied to expand its capacity q Adding in the middle of an array

More information

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues CSE 373 Data Structures and Algorithms Lecture 2: Queues Queue ADT queue: A list with the restriction that insertions are done at one end and deletions are done at the other First-In, First-Out ("FIFO

More information

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28 Computer Science E-119 Fall 2012 Due prior to lecture on Wednesday, November 28 Getting Started To get the files that you will need for this problem set, log into nice.harvard.edu and enter the following

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ ABSTRACT DATATYPES 2 Abstract Datatype (ADT)

More information

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

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

A Linked Structure. Chapter 17

A Linked Structure. Chapter 17 Chapter 17 A Linked Structure This chapter demonstrates the OurList interface implemented with a class that uses a linked structure rather than the array implementation of the previous chapter. The linked

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1 Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing

More information

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation 1.00 Lecture 32 Hashing Reading for next time: Big Java 18.1-18.3 Motivation Can we search in better than O( lg n ) time, which is what a binary search tree provides? For example, the operation of a computer

More information

CS 314 Final Fall 2011

CS 314 Final Fall 2011 Points off 1 2A 2B 2C 3 4 5 Total off Net Score CS 314 Final Fall 2011 Your Name_ Your UTEID Instructions: 1. There are 5 questions on this test. 2. You have 3 hours to complete the test. 3. You may not

More information

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1 CS61B Lecture #19 Administrative: HKN midterm review session, 320 Soda, 6:30-8 Tuesday (10/14). Review session Wednesday 5:30 6:30PM in 306 Soda. Need alternative test time? Make sure you send me mail.

More information

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other? CSM B Hashing & Heaps Spring 0 Week 0: March 0, 0 Motivation. (a) In the worst case, how long does it take to index into a linked list? Θ(N) (b) In the worst case, how long does it take to index into an

More information