cse 501n fall ‘09 10: introduction to collections and linked lists 29 september 2009 nick...
DESCRIPTION
3 Arrays The shortcomings Fixed size Insertion into the middle of a contiguous array Removal from the middle of a contiguous arrayTRANSCRIPT
![Page 1: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/1.jpg)
CSE 501NFall ‘0910: Introduction to Collections and Linked Lists
29 September 2009Nick Leidenfrost
![Page 2: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/2.jpg)
2
Lecture Outline
Data Structures Linked List Iterators
![Page 3: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/3.jpg)
3
ArraysThe shortcomings
Fixed size Insertion into the middle of a contiguous
array Removal from the middle of a contiguous
array
![Page 4: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/4.jpg)
4
Data Structures
Objects whose purpose is to organize data (primitives or other objects)
Allow us to store large amounts of information when it is not desirable to do so with an array
Can grow and shrink as needed Can be more efficient to search than arrays in
certain cases
![Page 5: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/5.jpg)
5
Data StructuresJava Libraries
Java provides several data structures in its librariesContained in the java.util package
[ To the JavaDoc, Batman! ] In the next few weeks we will be exploring
some of these Data Structures and learning their strengths and weaknessesWhen it is appropriate to use a particular
structure
![Page 6: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/6.jpg)
6
Linked Lists One of the most basic data structures is the
linked list A linked list is a data structure which consists of
a number of nodes, each of which has some contents and a reference to the next node
(storage) (storage) (storage) (storage)
List
Node Node Node Node
![Page 7: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/7.jpg)
7
Linked ListsCode View
A linked list is a data structure which consists of a number of nodes (instances of a Node class), each of which has some contents (instance variable) a reference to the next node (instance variable)
Object storage;Node next;
class Node
Object storage;Node next;
Object storage;Node next;
class SinglyLinkedList
Node head;
![Page 8: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/8.jpg)
8
Linked Lists vs. Arrays Adding and removing elements in the middle of a linked list is efficient
Visiting the elements of a linked list in sequential order is efficient
Random access is not efficient
Lists can grow arbitrarily large (Resizing is efficient)
![Page 9: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/9.jpg)
9
Linked ListsImplementation
To keep it simple, we will look at implementing a singly-linked list Called “Singly Linked” because Nodes only
know about the Node after them (References, or links only go in one direction)
You will be asked to implement a Doubly Linked List in Lab 4
![Page 10: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/10.jpg)
10
Linked ListsImplementation Node class
Holds a reference to the next Node Holds a reference to the Object it is storing
SinglyLinkedList class Holds a reference head to the first node
(storage) (storage) (storage) (storage)
List
Node Node Node Node
![Page 11: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/11.jpg)
11
ListsIn Code
public class Node {
Object contents; Node nextNode;
public Node (Object contents) { this.contents = contents; }
}public class SinglyLinkedList {
Node head;
public SinglyLinkedList () { this.head = null; }
}
![Page 12: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/12.jpg)
12
Adding a New First Element When a new Node is added to the list
It becomes the head of the list The old list head becomes its next Node
public class SinglyLinkedList {
// . . .
public void addFirst (Object add) { Node newNode = new Node(add); newNode.next = this.head; this.head = newNode; }
}
![Page 13: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/13.jpg)
13
Removing the First Element
When the first element is removed The data of the first node are saved and later
returned as the method result The successor of the list head becomes the
list headThe old node will be garbage collected when
there are no further references to it
![Page 14: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/14.jpg)
14
Removing the First Element The removeFirst method
public class SinglyLinkedList {
// . . .
public Object removeFirst() {
if (head == null)
return null;
Object obj = this.head.contents;
this.head = this.head.next;
return obj; } }
![Page 15: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/15.jpg)
15
Getting an Element by Index The getNth method
public Object getNth (int index) {
Node search = this.head;
for (int i=0; i<index; i++) {
if (search == null)
return null;
search = search.next;
}
return search.contents;
}
![Page 16: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/16.jpg)
16
Iterating Over Data Structures Iterating over a Linked List
We could just use a regular for loop:
But what is this doing internally?[ Example on Board ]
Very inefficient
for (int i=0; i<list.getSize(); i++) {
Object nth = list.getNth(i);
... // Do something with ‘nth’
}
![Page 17: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/17.jpg)
17
IteratorsHelpers for Stepping through Data Structures
Let’s define a way for users to iterate through our data structure (SinglyLinkedList), one element at a time Internal Iterator
[ Example on Board ] But what if multiple different entities want
to iterate over the Data Structure?Position is invalidated for other users
![Page 18: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/18.jpg)
18
IteratorsSafely Exporting State
What we need is a way to encapsulate (package up) and export a position in the Data Structure…Allow multiple different simultaneous iterators
over the Data StructureWe need to have a trusted way of providing a
reference to our internal representation Exporting our internal representation
How is this done?
![Page 19: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/19.jpg)
19
Levels of EncapsulationExporting State Safely
We want the internal structure (Nodes) of the linked list to be as protected as possible
We want to be able to export some notion of a position within the list The easiest way to do this would be a reference to a Node
But we don’t want to expose the Node object itself Write an encapsulation for the behavior we want to
export
![Page 20: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/20.jpg)
20
Packages and Access ModifiersClasses that Work Together Closely
Put our SinglyLinkedList, our Node, and our ListIterator classes inside of package
Declare Node head in SinglyLinkedList with the protected access modifier
ListIterator can access protected fields of the List, because List and ListIterator are in the same package
Make the constructor of ListIterator protected so that it must be created via the SinglyLinkedList
![Page 21: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/21.jpg)
22
Iterators
ListIterator type Gives access to elements inside a linked list Encapsulates the concept of a ‘position’
anywhere inside the linked list Protects the internal structure of the linked list
while giving access
![Page 22: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/22.jpg)
23
A List Iterator
![Page 23: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/23.jpg)
24
List Iterator Think of an iterator as pointing between two elements
in a data structure Analogy: like the cursor in a word processor points
between two characters
The iterator method of the SinglyLinkedList class gets a new ListIterator Points to the beginning of the list
SinglyLinkedList employeeNames = new SinglyLinkedList();
ListIterator iterator = employeeNames.iterator();
![Page 24: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/24.jpg)
25
A Conceptual View of a List Iterator
![Page 25: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/25.jpg)
26
List Iterator
Initially, the iterator points before the first element
hasNext returns true if there is a next element
The next method moves the iterator and returns the current item
Object next = iterator.next();
if (iterator.hasNext()) iterator.next();
![Page 26: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/26.jpg)
27
List Iterator
The next method returns the element that the iterator is passing
while (iterator.hasNext()) { Object obj = iterator.next(); // Do something with obj}
![Page 27: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/27.jpg)
28
LinkedListIterator The LinkListIterator class
package listutil;
public class LinkedListIterator {
Node previous, position;
List list;
public LinkedListIterator(List list) {
this.list = list; this.position = null; this.previous = null; }
}
}
![Page 28: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/28.jpg)
29
Linked List Iterator hasNext Method
The next method should only be called when the iterator is not at the end of the list If hasNext returns true
The iterator is at the end if… the list is empty (list.head == null) there is no element after the current position
(position.next == null)
![Page 29: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/29.jpg)
30
The Linked List IteratorhasNext Method
private class LinkedListIterator { //. . .
public boolean hasNext() { if (position == null) return (list.head != null); else return (position.next != null); }
//. . .}
![Page 30: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/30.jpg)
31
The Linked List Iteratornext Method
position: reference to the last visited Node previous: reference to the Node before position
next method: position reference is advanced to position.next
Last position is remembered in previous If the iterator points before the first element
of the list, then the previous is null and position must be set to list.head
![Page 31: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/31.jpg)
32
The Linked List Iteratornext Method
public Object next() { if (!hasNext()) return null; previous = position; // Remember for remove
if (position == null) position = list.head; else position = position.next;
return position.contents;}
![Page 32: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/32.jpg)
33
The Linked List Iteratorremove Method
If the element to be removed is the first element, call list.removeFirst
Otherwise, the node preceding the element to be removed needs to have its next reference updated to skip the removed element
![Page 33: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/33.jpg)
34
The Linked List Iteratorremove Method
public void remove() {
if (previous == position) return; // Invalid state!
if (position == list.head) { list.removeFirst(); }
else {
previous.next = position.next;
}
position = previous; }
![Page 34: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/34.jpg)
35
Removing a Node From the Middle of a Linked List
![Page 35: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/35.jpg)
36
The Linked List Iteratorremove Method: Caveats
If the previous equals position: Then the call to remove does not immediately follow a call to next Method cannot execute properly
(throw an IllegalArgumentException) It is illegal to call remove twice in a row remove sets the previous reference to position
![Page 36: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/36.jpg)
37
The Linked List Iteratorset Method
Changes the data stored in the previously visited element
The set method
public Object set (Object obj) {
Object contents = null; if (position != null) {
contents = position.contents; position.contents = obj;
}
return contents;}
![Page 37: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/37.jpg)
38
The Linked List Iteratoradd Method
add inserts the new node after the current position
The most complex operation is the addition of a node
Sets the successor of the new node to the successor of the current position
![Page 38: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/38.jpg)
39
The Linked List Iterator's add Method
public void add(Object obj) { if (position == null) { list.addFirst(obj); position = list.head;
previous = null; } else { Node newNode = new Node(obj); newNode.next = position.next; position.next = newNode;
previous = position; position = newNode; }
}
![Page 39: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/39.jpg)
40
Adding a Node to the Middle of a Linked List
![Page 40: CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost](https://reader036.vdocuments.site/reader036/viewer/2022062911/5a4d1bd67f8b9ab0599da633/html5/thumbnails/40.jpg)
41
Conclusion
Lab 3 assigned todayArrays & IterationLab 2 due by midnight I will be in lab now