CSE 501 N Fall 09 10 Introduction to

  • Slides: 40
Download presentation
CSE 501 N Fall ‘ 09 10: Introduction to Collections and Linked Lists 29

CSE 501 N Fall ‘ 09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost

Lecture Outline n Data Structures n Linked List Iterators n 2

Lecture Outline n Data Structures n Linked List Iterators n 2

Arrays The shortcomings Fixed size n Insertion into the middle of a contiguous array

Arrays The shortcomings Fixed size n Insertion into the middle of a contiguous array n Removal from the middle of a contiguous array n 3

Data Structures n n Objects whose purpose is to organize data (primitives or other

Data Structures n n 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 4

Data Structures Java Libraries n Java provides several data structures in its libraries ¨

Data Structures Java Libraries n Java provides several data structures in its libraries ¨ Contained in the java. util package [ To the Java. Doc, Batman! ] n In the next few weeks we will be exploring some of these Data Structures and learning their strengths and weaknesses ¨ When it is appropriate to use a particular structure 5

Linked Lists n n One of the most basic data structures is the linked

Linked Lists n n 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 List Node (storage) 6

Linked Lists Code View n A linked list is a data structure which consists

Linked Lists Code View n 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) class Singly. Linked. List Node head; class Node Object storage; Node next; 7

Linked Lists vs. Arrays n Adding and removing elements in the middle of a

Linked Lists vs. Arrays n Adding and removing elements in the middle of a linked list is efficient n Visiting the elements of a linked list in sequential order is efficient n Random access is not efficient n Lists can grow arbitrarily large ¨ (Resizing is efficient) 8

Linked Lists Implementation n To keep it simple, we will look at implementing a

Linked Lists Implementation n 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 9

Linked Lists Implementation n Node class ¨ Holds a reference to the next Node

Linked Lists Implementation n Node class ¨ Holds a reference to the next Node ¨ Holds a reference to the Object it is storing n Singly. Linked. List class ¨ Holds a reference head to the first node List Node (storage) 10

Lists In Code public class Node { Object contents; Node next. Node; } public

Lists In Code public class Node { Object contents; Node next. Node; } public Node (Object contents) { this. contents = contents; } public class Singly. Linked. List { Node head; public Singly. Linked. List () { this. head = null; } } 11

Adding a New First Element n When a new Node is added to the

Adding a New First Element n 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 Singly. Linked. List { //. . . public void add. First (Object add) { Node new. Node = new Node(add); new. Node. next = this. head; this. head = new. Node; } } 12

Removing the First Element n When the first element is removed ¨ The data

Removing the First Element n 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 head ¨ The old node will be garbage collected when there are no further references to it 13

Removing the First Element n The remove. First method public class Singly. Linked. List

Removing the First Element n The remove. First method public class Singly. Linked. List { //. . . public Object remove. First() { if (head == null) return null; Object obj = this. head. contents; this. head = this. head. next; return obj; } } 14

Getting an Element by Index n The get. Nth method public Object get. Nth

Getting an Element by Index n The get. Nth method public Object get. Nth (int index) { Node search = this. head; for (int i=0; i<index; i++) { if (search == null) return null; search = search. next; } return search. contents; } 15

Iterating Over Data Structures n Iterating over a Linked List ¨ We could just

Iterating Over Data Structures n Iterating over a Linked List ¨ We could just use a regular for loop: for (int i=0; i<list. get. Size(); i++) { Object nth = list. get. Nth(i); . . . // Do something with ‘nth’ } n But what is this doing internally? [ Example on Board ] ¨ Very inefficient 16

Iterators Helpers for Stepping through Data Structures n Let’s define a way for users

Iterators Helpers for Stepping through Data Structures n Let’s define a way for users to iterate through our data structure (Singly. Linked. List), one element at a time ¨ Internal Iterator [ Example on Board ] n But what if multiple different entities want to iterate over the Data Structure? ¨ Position is invalidated for other users 17

Iterators Safely Exporting State n What we need is a way to encapsulate (package

Iterators Safely Exporting State n 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 Structure ¨ We need to have a trusted way of providing a reference to our internal representation n n Exporting our internal representation How is this done? 18

Levels of Encapsulation Exporting State Safely n n We want the internal structure (Nodes)

Levels of Encapsulation Exporting State Safely n n 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 n But we don’t want to expose the Node object itself ¨ Write an encapsulation for the behavior we want to export 19

Packages and Access Modifiers Classes that Work Together Closely n n Put our Singly.

Packages and Access Modifiers Classes that Work Together Closely n n Put our Singly. Linked. List, our Node, and our List. Iterator classes inside of package Declare Node head in Singly. Linked. List with the protected access modifier List. Iterator can access protected fields of the List, because List and List. Iterator are in the same package Make the constructor of List. Iterator protected so that it must be created via the Singly. Linked. List 20

Iterators n List. Iterator type ¨ Gives access to elements inside a linked list

Iterators n List. Iterator 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 22

A List Iterator 23

A List Iterator 23

List Iterator n Think of an iterator as pointing between two elements in a

List Iterator n 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 n The iterator method of the Singly. Linked. List class gets a new List. Iterator ¨ Points to the beginning of the list Singly. Linked. List employee. Names = new Singly. Linked. List(); List. Iterator iterator = employee. Names. iterator(); 24

A Conceptual View of a List Iterator 25

A Conceptual View of a List Iterator 25

List Iterator n n Initially, the iterator points before the first element has. Next

List Iterator n n Initially, the iterator points before the first element has. Next returns true if there is a next element if (iterator. has. Next()) iterator. next(); n The next method moves the iterator and returns the current item Object next = iterator. next(); 26

List Iterator n The next method returns the element that the iterator is passing

List Iterator n The next method returns the element that the iterator is passing while (iterator. has. Next()) { Object obj = iterator. next(); // Do something with obj } 27

Linked. List. Iterator n The Link. List. Iterator class package listutil; public class Linked.

Linked. List. Iterator n The Link. List. Iterator class package listutil; public class Linked. List. Iterator { Node previous, position; List list; public Linked. List. Iterator(List list) { this. list = list; this. position = null; this. previous = null; } } } 28

Linked List Iterator has. Next Method n The next method should only be called

Linked List Iterator has. Next Method n The next method should only be called when the iterator is not at the end of the list ¨ If n has. Next 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) 29

The Linked List Iterator has. Next Method private class Linked. List. Iterator { //.

The Linked List Iterator has. Next Method private class Linked. List. Iterator { //. . . public boolean has. Next() { if (position == null) return (list. head != null); else return (position. next != null); } //. . . } 30

The Linked List Iterator next Method n n n position: reference to the last

The Linked List Iterator next Method n n n 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 31

The Linked List Iterator next Method public Object next() { if (!has. Next()) return

The Linked List Iterator next Method public Object next() { if (!has. Next()) return null; previous = position; // Remember for remove if (position == null) position = list. head; else position = position. next; return position. contents; } 32

The Linked List Iterator remove Method n If the element to be removed is

The Linked List Iterator remove Method n If the element to be removed is the first element, call list. remove. First n Otherwise, the node preceding the element to be removed needs to have its next reference updated to skip the removed element 33

The Linked List Iterator remove Method public void remove() { if (previous == position)

The Linked List Iterator remove Method public void remove() { if (previous == position) return; // Invalid state! if (position == list. head) { list. remove. First(); } else { previous. next = position. next; } position = previous; } 34

Removing a Node From the Middle of a Linked List 35

Removing a Node From the Middle of a Linked List 35

The Linked List Iterator remove Method: Caveats n If the previous equals position: ¨

The Linked List Iterator remove Method: Caveats n If the previous equals position: ¨ Then the call to remove does not immediately follow a call to next ¨ Method cannot execute properly n (throw an Illegal. Argument. Exception) ¨ It is illegal to call remove twice in a row ¨ remove sets the previous reference to position 36

The Linked List Iterator set Method Changes the data stored in the previously visited

The Linked List Iterator set Method Changes the data stored in the previously visited element n The set method n public Object set (Object obj) { Object contents = null; if (position != null) { contents = position. contents; position. contents = obj; } return contents; } 37

The Linked List Iterator add Method add inserts the new node after the current

The Linked List Iterator add Method add inserts the new node after the current position n The most complex operation is the addition of a node n Sets the successor of the new node to the successor of the current position n 38

The Linked List Iterator's add Method public void add(Object obj) { if (position ==

The Linked List Iterator's add Method public void add(Object obj) { if (position == null) { list. add. First(obj); position = list. head; previous = null; } else { Node new. Node = new Node(obj); new. Node. next = position. next; position. next = new. Node; previous = position; position = new. Node; } } 39

Adding a Node to the Middle of a Linked List 40

Adding a Node to the Middle of a Linked List 40

Conclusion n Lab 3 assigned today ¨ Arrays & Iteration ¨ Lab 2 due

Conclusion n Lab 3 assigned today ¨ Arrays & Iteration ¨ Lab 2 due by midnight ¨ I will be in lab now 41