COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Double-Linked Lists and Circular Lists
John Hurley Cal State LA
The List ADT Textbook Sections
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
M180: Data Structures & Algorithms in Java
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Sprint 2010CS 2251 Lists and the Collection Interface Chapter 2.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Lists Based on content from: Java Foundations, 3rd Edition.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Lists and the Collection Interface Review inheritance & collections.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
LinkedList Many slides from Horstmann modified by Dr V.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COMP 121 Week 11: Linked Lists.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 5: The List Abstract Data Type
Linked Lists Chapter 5 (continued)
John Hurley Cal State LA
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
JAVA COLLECTIONS LIBRARY
COMP 121 Week 9: ArrayList.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Lists and the Collection Interface
Linked Lists Chapter 5 (continued)
Iterators Dan Fleck.
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

COMP 121 Week 11: Linked Lists

Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList class Understand the Iterator interface Understand the ListIterator interface Become familiar with another piece of the Java Collection framework

Linked List ArrayList: add/remove methods operate in linear time O(n)  Require a loop to shift elements in the underlying array LinkedList:  Overcomes this by providing ability to add or remove items anywhere in the list in constant time O(1)  Each element (node) in a linked list stores information and a link to the next, and, optionally, previous node

Single-Linked List Node A node contains a data item and one or more links A link is a reference to a node A node is defined inside of List class, making it an inner class The details of a node should be private

Single-Linked List

Adding an Element

Removing an Element

Single-Linked List - Limitations Insertion at positions other than the first is O(n)  Insertion at the front of the list is O(1) Can insert a node only after a referenced node Can remove a node only if we have a reference to its predecessor node Can traverse the list only in the forward direction Solution: double-linked list

Double-Linked List Node

Double-Linked List

Adding an Element to a Double- Linked List (Steps 1 and 2)

Adding an Element to a Double- Linked List (Steps 3 and 4)

Removing an Element from a Double-Linked List

Circular-Linked Lists Links the last node of a double-linked list to the first node and the first to the last Advantages:  Can traverse in forward or reverse direction even after you reach the last or first node  Can visit all list elements from any starting point  Can never fall off the end of a list Disadvantage:  If not careful, can cause an infinite loop!

Circular Linked List

The LinkedList Class Part of the Java API Implements the List interface using a double-linked list

The Iterator Interface The interface Iterator is defined as part of API package java.util The List interface declares the method iterator(), which returns an Iterator object that will iterate over the elements of that list An Iterator does not refer to or point to a particular node at any given time, but points between nodes

The Iterator Interface (cont’d)

The ListIterator Interface Iterator limitations:  Can only traverse the List in the forward direction  Provides only a remove method  Must advance an Iterator using your own loop if starting position is not at the beginning of the list ListIterator is an extension of the Iterator interface that overcomes the above limitations Like Iterator, a ListIterator should be thought of as being positioned between elements of the linked list

ListIterator

The java.util.ListIterator Interface

LinkedList Methods that Return ListIterators

ListIterator vs. Index nextIndex returns the index value of the item that would be returned by a subsequent call to next() previousIndex returns the index value of the item that would be returned by a subsequent call to previous() listIterator(int index) is a method of the LinkedList class:  Returns a ListIterator whose subsequent call to next() will return the item at position index

Case Study: Writing a Program to Maintain a List of Homework Assignments When an assignment is assigned, add it to the list, and when it is completed, remove it. Keep track of the due date. The program should provide the following services:  Add a new assignment  Remove an assignment  Provide a list of the assignments in the order they were assigned  Find the assignment with the earliest due date

UML Class Diagram Assignment description dueDate compareTo() HomeworkList assignmentList add() remove() displayAssignments() findEarliest()

HomeworkList Class import java.util.ListIterator; import java.util.LinkedList; public class HomeworkList { private LinkedList assignmentList; private LinkedList assignmentList; public HomeworkList() public HomeworkList() { assignmentList = new LinkedList (); assignmentList = new LinkedList (); }

HomeworkList Class (cont’d) public void add(Assignment assignment) public void add(Assignment assignment) { assignmentList.addLast(assignment); assignmentList.addLast(assignment); } public void remove(Assignment assignment) public void remove(Assignment assignment) { assignmentList.remove(assignment); assignmentList.remove(assignment); }

HomeworkList Class (cont’d) public void displayAssignments() public void displayAssignments() { String message; String message; int i = 1; int i = 1; for (Assignment assignment : assignmentList) for (Assignment assignment : assignmentList) { message = "Assignment #" + (i++) + message = "Assignment #" + (i++) + ":\n" + ":\n" + assignment.getDescription() + assignment.getDescription() + "\nDue date: " + "\nDue date: " + assignment.getDueDate(); assignment.getDueDate(); System.out.println(message); System.out.println(message); } }

HomeworkList Class (cont’d) public Assignment findEarliest() { Assignment earliest = null; Assignment earliest = null; Assignment current; Assignment current; ListIterator iter = ListIterator iter = assignmentList.listIterator(); assignmentList.listIterator(); if (iter.hasNext()) if (iter.hasNext()) { earliest = iter.next(); earliest = iter.next(); while (iter.hasNext()) while (iter.hasNext()) { current = iter.next(); current = iter.next(); if (current.compareTo(earliest) < 0) if (current.compareTo(earliest) < 0) { earliest = current; earliest = current; } } } return earliest; return earliest;}

Summary A linked list consists of a set of nodes, each of which contains its data and a reference to the next node Locating an item at a position indicated by an index in a linked list requires traversing the list from the beginning until the item at the specified index is found An Iterator gives with the ability to access the items in a List sequentially The ListIterator interface is an extension of the Iterator interface The Java API provides the LinkedList class, which uses a double-linked list to implement the List interface

Questions?