COMP 121 Week 11 Linked Lists Objectives Understand
COMP 121 Week 11: Linked Lists
Objectives Understand how single-, double-, and circular-linked list data structures are implemented n Understand the Linked. List class n Understand the Iterator interface n Understand the List. Iterator interface n n Become familiar with another piece of the Java Collection framework
Linked Lists n Array List ¨ The add and remove methods operate in linear time O(n) n n Require a loop to shift elements in the underlying array Linked List ¨ 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 Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Single-Linked List Node A node contains a data item and one or more links n A link is a reference to a node n A node is generally defined inside of another class, making it an inner class n The details of a node should be private n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Single-Linked List Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Add a Node in a Single-Linked List Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Remove a Node from a Single. Linked List Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Double-Linked Lists n Limitations of a single-linked list include: ¨ Insertion n 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 n Limitations are overcome by adding a reference in each node to the previous node (double-linked list) Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Double-Linked List Node Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Double-Linked List Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Add a Node to a Double-Linked List (Steps 1 and 2) Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Add a Node to a Double-Linked List (Steps 3 and 4) Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Remove a Node from a Double. Linked List Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Circular Lists n Circular-linked list ¨ Links the last node of a double-linked list to the first node and the first to the last n 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 n Disadvantage ¨ Possibility of an infinite loop! Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Circular Lists Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The Linked. List<E> Class Part of the Java API n Implements the List<E> interface using a double-linked list n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The Iterator<E> Interface The interface Iterator is defined as part of API package java. util n The List interface declares the method iterator(), which returns an Iterator object that will iterate over the elements of that list n An Iterator does not refer to or point to a particular node at any given time but points between nodes n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The Iterator<E> Interface (cont’d) Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The List. Iterator<E> Interface n Iterator limitations n Can only traverse the List in the forward direction Provides only a remove method n Must advance an iterator using your own loop if starting position is not at the beginning of the list n List. Iterator<E> is an extension of the Iterator<E> interface that overcomes the above limitations n Like Iterator, a List. Iterator should be thought of as being positioned between elements of the linked list n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The List. Iterator Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
The List. Iterator Interface Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Linked. List Methods that Return List. Iterators Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Comparison of Iterator and List. Iterator n List. Iterator is a subinterface of Iterator ¨ Classes that implement List. Iterator provide the capabilities of both n Iterator interface ¨ Requires fewer methods ¨ Iterates over more general data structures n n n Only in one direction (forward) Iterator is required by Collection interface List. Iterator is required by List interface Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Conversion between a List. Iterator and an Index The next. Index method returns the index value of the item that would be returned by a call to the next method n The previous. Index method returns the index value of the item that would be returned by a call to the previous method n The list. Iterator(int index) method is a method in Linked. List n ¨ Returns a List. Iterator whose next call to next() will return the item at position index Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Project: Using a Linked. List n Develop 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 Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Class Diagram Assignment Homework. List description due. Date the. List compare. To() add() remove() show. Assignments() find. Earliest() Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Homework. List Class import java. util. List. Iterator; import java. util. Linked. List; public class Homework. List { private Linked. List<Assignment> the. List; public Homework. List() { the. List = new Linked. List<Assignment>(); } Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Homework. List Class (cont’d) public void add(Assignment assignment) { the. List. add. Last(assignment); } public void remove(Assignment assignment) { the. List. remove(assignment); } Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Homework. List Class (cont’d) public void show. Assignments() { String message; int i = 1; for (Assignment assignment : the. List) { message = "Assignment #" + (i++) + ": n" + assignment. get. Description() + "n. Due date: " + assignment. get. Due. Date(); System. out. println(message); } } Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Homework. List Class (cont’d) public Assignment find. Earliest() { Assignment earliest = null; Assignment current; List. Iterator<Assignment> iter = the. List. list. Iterator(); if (iter. has. Next()) { earliest = iter. next(); while (iter. has. Next()) { current = iter. next(); if (current. compare. To(earliest) < 0) { earliest = current; } } } return earliest; } Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Summary A linked list consists of a set of nodes, each of which contains its data and a reference to the next node n To find 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 n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Summary (cont’d) An iterator gives with the ability to access the items in a List sequentially n The List. Iterator interface is an extension of the Iterator interface n The Java API provides the Linked. List class, which uses a double-linked list to implement the List interface n Koffman, E. B. & Wolfgang, P. A. T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5. 0. New York: John Wiley & Sons.
Any Questions?
- Slides: 33