linked list a linked list consists of a number of links, each of which has a reference to the next...

35
Linked List A linked list consists of a number of links, each of which has a reference to the next link. 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

Upload: agustin-eliot

Post on 30-Mar-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Linked List

• A linked list consists of a number of links, each of which has a reference to the next link.

• 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

Page 2: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

linked list

Page 3: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Java's LinkedList class

http://java.sun.com/j2se/1.4.2/docs/api/index.html

Some of the simpler methods:

o void addFirst(Object obj) o void addLast(Object obj)o Object getFirst() o Object getSecond() o Object removeFirst() o Object removeLast()

Page 4: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

listIterator provides a way to access all links: ListIterator listIterator(int index)

• listIterator method provides an object of type ListIterator ListIterator is an interface, so the return object from this method provides all the methods in this interface

ListIterator Interface: void add(Object o) boolean hasNext(), boolean hasPrev() Object next(), int nextIndex() Object previous(), int prevIndex() void remove(), void set(Object)

* provide a way to move forward thru list elements

Page 5: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

A ListIterator object

Page 6: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

hasNext() method ..

• hasNext returns true if there is a next element

LinkedList list = new LinkedList();

// other calls

ListIterator iterator = list.listIterator(0); if (iterator.hasNext())

………

Page 7: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

next() method ..

• The next method moves the iterator

ListIterator iterator = list.listIterator(0); if (iterator.hasNext())

iterator.next();

• next throws a NoSuchElementException if you are already past the end of the list

• The next method returns the object of the link that it is passing

while (iterator.hasNext()) {

Object obj = iterator.next(); Dog mydog = (Dog)obj;

mydog.draw();

} •

Page 8: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

other iterator methods.. • To move the list position backwards, use:

o hasPrevious o Previous

• The add method: o Adds an object after the iterator o Moves the iterator position past the new element iterator.add("Juliet");

• The remove method: o Removes ando Returns the object that was returned by the last call to next or

previous

Page 9: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Adding and Removing from a LinkedList

• This loop removes all objects that fulfill a certain condition

while (iterator.hasNext()) {

Object obj = iterator.next(); if (obj fulfills condition) iterator.remove();

}

Page 10: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

File ListTest.java

• ListTest is a sample program that

o inserts elements into a list

o iterates through the list, adding and removing elements

o prints the list

Page 11: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

File ListTest.java01: import java.util.LinkedList;

02: import java.util.ListIterator;

03:

04: /**

05: A program that demonstrates the LinkedList class

06: */

07: public class ListTest

08: {

09: public static void main(String[] args)

10: {

11: LinkedList staff = new LinkedList();

12: staff.addLast("Dick");

13: staff.addLast("Harry");

14: staff.addLast("Romeo");

15: staff.addLast("Tom");

16:

17: // | in the comments indicates the iterator position

Page 12: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

18:

19: ListIterator iterator = staff.listIterator(); // |DHRT

20: iterator.next(); // D|HRT

21: iterator.next(); // DH|RT

22:

23: // add more elements after second element

24:

25: iterator.add("Juliet"); // DHJ|RT

26: iterator.add("Nina"); // DHJN|RT

27:

28: iterator.next(); // DHJNR|T

29:

30: // remove last traversed element

31:

32: iterator.remove(); // DHJN|T

33:

34: // print all elements

35:

36: iterator = staff.listIterator();

37: while (iterator.hasNext())

Page 13: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

38: System.out.println(iterator.next());

39: }

40: }

Page 14: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Implementing Linked Lists • LinkedList class has a private inner class Link

class LinkedList {

private class Link {

public Object data; public Link next;

} }

Page 15: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Implementing Linked Lists • LinkedList class

o Holds a reference first to the first link

o Has a method to get the first element

Page 16: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Implementing Linked Lists class LinkedList {

public LinkedList() {

first = null; }

public Object getFirst() {

if (first == null) throw new

NoSuchElementException(); return first.data;

} . . . private Link first;

}

Page 17: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Adding a New First Element

• When a new link is added to the list o It becomes the head of the list o The old first link becomes the next

link

Page 18: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Adding a New First Element • The addFirst method

class LinkedList {

. . . public void addFirst(Object obj) {

Link newLink = new Link(); newLink.data = obj;

newLink.next = first; first = newLink;

} ...

}

Page 19: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Adding a Link to the Head of a Linked List

Page 20: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Removing the First Element

• When the first element is removed o The data of the first link are saved and later

returned as the method result

o The successor of the first link becomes the first link of the shorter list

o The link will be garbage collected when there are no further references to it

Page 21: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Removing the First Element • The removeFirst method

class LinkedList { . . .

public Object removeFirst() {

if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj;

} . . .

}

Page 22: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Removing the First Link from a Linked List

Page 23: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkedListIterator

• Private inner class of LinkedList

• Implements a simplified ListIterator interface

• Has access to the first field and private Link class

Page 24: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkedListIteratorThe LinkListIterator class

class LinkedList { . . .

public ListIterator listIterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { public LinkedListIterator() {

position = null; previous = null;

} . . . private Link position; private Link previous;

} . . .

}

Page 25: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's next Method • position reference is advances to position.next

• Old position is remembered as previous

• If the iterator points before the first element of the list, then the old position is

null and position must be set to first

Page 26: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's next Method private class LinkedListIterator implements ListIterator { . . .

public Object next() { if (!hasNext())

throw new NoSuchElementException(); previous = position; // remember for remove if (position == null)

position = first; else

position = position.next; return position.data; } . . .

}

Page 27: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's hasnext Method

• The next method should only be called when the iterator is not at the end of the list

• The iterator is at the end

o if the list is empty (first == null)

o if there is no element after the current position (position.next == null)

Page 28: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's hasnext Method

private class LinkedListIterator implements ListIterator {

. . . public boolean hasNext() {

if (position == null) return first != null;

else return position.next != null;

}

}

Page 29: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's remove Method

• If the element to be removed is the first element, call removeFirst

• Otherwise, the link proceeding the element to be removed needs to have its next reference updated to skip the removed element

• If the previous reference is null:

o this call does not immediately follow a call to next

o throw an IllegalArgumentException

• Set previous reference to null

Page 30: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's remove Method private class LinkedListIterator implements ListIterator { . . .

public void remove() { if (position == first)

{ removeFirst(); position = first; }

else {

if (previous == null) throw new IllegalStateException();

previous.next = position.next; position = previous;

} previous = null; }

. . . }

Page 31: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Removing a Link From the Middle of a Linked List

Page 32: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's set Method • Changes the data stored in the previously visited

element • The set method

private class LinkedListIterator implements ListIterator{ . . .

public void set(Object obj) { if (position == null) throw new NoSuchElementException(); position.data = obj; } . . .

}

Page 33: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's add Method

• Inserts the new link after the current position

• Sets the successor of the new link to the successor of the current position

Page 34: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

LinkListIterator's add Method private class LinkedListIterator implements ListIterator{ . . . public void add(Object obj) { if (position == null) { addFirst(obj); position = first; } else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; } previous = null; } . . .}

Page 35: Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of

Adding a Link to the Middle of a Linked List