doubly-linked lists cmput 115 - lecture 16 department of computing science university of alberta...

21
Doubly-Linked Lists Doubly-Linked Lists Cmput 115 - Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/24/0

Post on 19-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Doubly-Linked ListsDoubly-Linked Lists

Cmput 115 - Lecture 16

Department of Computing Science

University of Alberta©Duane Szafron 2000

Some code in this lecture is based on code from the book:Java Structures by Duane A. Bailey or the companion structure package

Revised 2/24/00

©Duane Szafron 2000

2

About This LectureAbout This Lecture

In this lecture we will learn about an implementation of the List Interface called Doubly-Linked List.

©Duane Szafron 2000

3

OutlineOutline

Drawing Doubly Linked Lists

DoublyLinkedListElement class

DoublyLinkedList class

©Duane Szafron 2000

4

Doubly-Linked List DiagramsDoubly-Linked List Diagrams A doubly-linked list node has two links, one forward

and one backward.

The doubly-linked listdoubly-linked list has references to its head and tail nodes.

This symmetry makes the implementation simpler.

“Fred” “Wilma” “Barney”

3

©Duane Szafron 2000

5

Constructing a NodeConstructing a Node When a DoublyLinkedListElement (node) is

constructed, four links may need to be set.

“Fred”

“Wilma”

“Barney”

12

34

If one or both of the “neighboring” nodes is null then fewer links must be set.

“Wilma”

“Barney”

12

3

null

©Duane Szafron 2000

6

DoublyLinkedListElement 1DoublyLinkedListElement 1

public class DoublyLinkedListElement {protected Object data;protected DoublyLinkedListElement next;protected DoublyLinkedListElement previous;

public DoublyLinkedListElement (Object element, DoublyLinkedListElement nextNode, DoublyLinkedListElement previousNode) {//post: initializes the node to contain the given// object and link to the given next and previous nodes.

this.data = element;this.next = nextNode;this.previous = previousNode;

code based on Bailey pg. 118

element

previousNode nextNode

©Duane Szafron 2000

7

DoublyLinkedListElement 2DoublyLinkedListElement 2

public DoublyLinkedListElement (Object element) {//post: initializes the receiver node to be the

// only node in a list.this(element, null, null);

}}

code based on Bailey pg. 118

if (nextNode != null) nextNode.previous = this;if (previousNode != null) previousNode.next = this;}

©Duane Szafron 2000

8

DoublyLinkedList - DoublyLinkedList - State & ConstructorState & Constructor

public class DoublyLinkedList implements List{

protected int count;protected DoublyLinkedListElement head;protected DoublyLinkedListElement tail;

public DoublyLinkedList() {//post: initializes the list to be empty.

this.head = null;this.tail = null;this.count = 0;

}

code based on Bailey pg. 119

©Duane Szafron 2000

9

DoublyLinkedList - DoublyLinkedList - Store InterfaceStore Interface

/* Interface Store Methods */public int size() {//post: returns the number of elements in the list.

return this.count; }

public boolean isEmpty() {// post: returns the true iff store is empty.

return this.size() == 0 }

public void clear() {// post: clears the list so that it contains no elements.

this.head = null;this.tail = null;this.count = 0; }

code based on Bailey pg. 108

©Duane Szafron 2000

10

DoublyLinkedList - Collection Interface 1DoublyLinkedList - Collection Interface 1

/* Interface Collection Methods */public boolean contains (Object anObject);// pre: anObject is non-null// post: returns true iff the collection contains the object

DoublyLinkedListElement cursor;

cursor = this.head; // partial traversalwhile ((cursor != null) &&

(!cursor.value().equals(anObject)) cursor = cursor.next();return cursor != null;

}

code based on Bailey pg. 114

©Duane Szafron 2000

11

DoublyLinkedList Collection Interface 2DoublyLinkedList Collection Interface 2

public void add (Object anObject) {// pre: anObject is non-null// post: the object is added to the collection. The // replacement policy is not specified

this.addToHead(anObject);}public Object remove (Object anObject);// pre: anObject is non-null// post: removes object “equal” to anObject and returns it,// otherwise returns nil

// Read the textbook after understanding this lecture.}public Iterator elements() {// post: return an iterator for traversing the collection

// Ignore this one until textbook Chapter 8! }code based on Bailey pg. 108

©Duane Szafron 2000

12

head

“Fred” “Wilma” “Barney”

3

DoublyLinkedList -DoublyLinkedList - addToHead() vers 1addToHead() vers 1

/* Interface List Methods */public void addToHead(Object anObject) {// pre: anObject is non-null// post: the object is added to the beginning of the list

this.head = new DoublyLinkedListElement(anObject,this.head, null);

this.count++;}

code based on Bailey pg. 119

“Pebbles”2

3

1 2

3

4

4

1

4

©Duane Szafron 2000

13

DoublyLinkedList DoublyLinkedList - - addToHead() problemaddToHead() problem

/* Interface List Methods */public void addToHead (Object anObject) {// pre: anObject is non-null// post: the object is added to the beginning of the list

this.head = newDoublyLinkedListElement(anObject,this.head, null);

this.count++;}

code based on Bailey pg. 119

1 2

3

4

4 Check the boundaries!If the list is empty, the list tail must be bound to the new node.

0 1

“Pebbles”

3

12

©Duane Szafron 2000

14

DoublyLinkedList - DoublyLinkedList - addToHead()addToHead()

/* Interface List Methods */public void addToHead (Object anObject) {// pre: anObject is non-null// post: the object is added to the beginning of the list

this.head = new DoublyLinkedListElement(anObject, this.head, null);

// fix tail, if necessaryif (this.tail == null)

this.tail = this.head;this.count++;}

code based on Bailey pg. 119

©Duane Szafron 2000

15

DoublyLinkedListDoublyLinkedList - removeFromHead() vers 1- removeFromHead() vers 1

public Object removeFromHead() {// pre: list is not empty// post: removes and returns first object from the list

DoublyLinkedListElement temp;temp = this.head;this.head = this.head.next();if (this.head != null)

this.head.previous = null;this.count--;return temp.value();

}

code based on Bailey pg. 119

12

3

2

4

1

“Fred” “Wilma” “Barney”

3 4

temp

4

3

©Duane Szafron 2000

16

DoublyLinkedList -DoublyLinkedList - removeFromHead() problemremoveFromHead() problem

public Object removeFromHead() {// pre: list is not empty// post: removes and returns first object from the list

DoublyLinkedListElement temp;temp = this.head;this.head = this.head.next();if (this.head != null)

this.head.previous = null;this.count--;return temp.value();

}

code based on Bailey pg. 110

12

3

“Fred”

0 12

4

1temp

4

Check the boundaries!If the list has one node, the list tail must be bound to null.

3

©Duane Szafron 2000

17

DoublyLinkedListDoublyLinkedList - - removeFromHead()removeFromHead()

public Object removeFromHead() {// pre: list is not empty// post: removes and returns first object from the list

DoublyLinkedListElement temp;

temp = this.head;this.head = this.head.next();if (this.head != null) this.head.previous = null;else // clean up tail if list is now empty this.tail = null;this.count--;return temp.value();}

code based on Bailey pg. 110

©Duane Szafron 2000

18

DoublyLinkedList DoublyLinkedList - - peek() and tailPeek()peek() and tailPeek()

public Object peek () {// pre: list is not empty// post: returns the first object in the list without // modifying the list

return this.head.value();}

public Object tailPeek () {// pre: list is not empty// post: returns the last object in the list without // modifying the list

return this.tail.value();}

code based on Bailey pg. 111

©Duane Szafron 2000

19

DoublyLinkedList - DoublyLinkedList - addToTail()addToTail()

public void addToTail (Object anObject) {// pre: anObject is non-null// post: the object is added at the end of the list

this.tail = new DoublyLinkedListElement(anObject, null, this.tail);

if (this.head == null)this.head = this.tail;

this.count++;}

code based on Bailey pg. 119

1 23

4

2

3

4

“Fred” “Wilma” “Barney”

3 4 “Pebbles”Look at the symmetry with addToHead()!

Check the boundaries!If the list was empty, bind the head to the new node.

1

©Duane Szafron 2000

20

DoublyLinkedList - DoublyLinkedList - removeFromTail()removeFromTail()public Object removeFromTail() {// pre: list is not empty// post: the last object in the list is removed and returned

DoublyLinkedListElement temp;

Assert.pre(!this.isEmpty(), “List is not empty”);temp = this.tail;this.tail = this.tail.previous();if (this.tail == null) this.head = null;else //must set next for the previously last node this.tail.setNext(null);this.count--;return temp.value();}

code based on Bailey pg. 119

Just copy from removeFromHead() based on symmetry!

©Duane Szafron 2000

21

Some Principles from the TextbookSome Principles from the Textbook

12. Question asymmetry

principles from Bailey ch. 6