Circularly-Linked Lists Cmput Lecture 17 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 About This Lecture In this lecture we will learn about an implementation of the List Interface called Circular List.
©Duane Szafron Outline Drawing Circular Lists CircularList class
©Duane Szafron Circular List Diagrams A circular list node is identical to a singly-linked list node. However, the circular list has a reference to its tail node instead of its head node. The tail node has a reference to the head node. This makes it easier to get to the head and tail nodes than in a Singly-linked list. “Fred”“Wilma”“Barney”3
©Duane Szafron CircularList - State and Constructor public class CircularList implements List { protected int count; protected SinglyLinkedListElement tail; public CircularList () { //post: initializes the list to be empty. this.tail = null; this.count = 0; } code based on Bailey pg. 122
©Duane Szafron CircularList - Store 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.tail = null; this.count = 0; } code based on Bailey pg. 108
©Duane Szafron CircularList - 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 SinglyLinkedListElement cursor; if (this.tail == null) return false; cursor = this.tail.next(); // partial traversal while ((cursor != tail) && (!cursor.value().equals(anObject)) cursor = cursor.next(); return cursor.value().equals(anObject); } code based on Bailey pg. 114
©Duane Szafron CircularList 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 // Similar to Singly-linked list removal. } code based on Bailey pg. 108
©Duane Szafron CircularList - addToHead() public void addToHead(Object anObject) { // pre: anObject is non-null // post: the object is added to the beginning of the list SinglyLinkedListElement temp; temp = new SinglyLinkedListElement(anObject); if (this.tail == null) { // empty list this.tail = temp; this.tail.setNext(this.tail); } else { temp.setNext(this.tail.next()); this.tail.setNext(temp); } this.count++; } code based on Bailey pg “Fred”“Wilma”“Barney”3 4“Pebbles” temp Check the boundaries! If the list is empty, bind the tail to the new node. 3
©Duane Szafron CircularList - removeFromHead() public Object removeFromHead() { // pre: list is not empty // post: removes and returns first object from the list SinglyLinkedListElement temp; temp = this.tail.next(); if (this.tail == this.tail.next()) //means a single node list this.tail = null; else this.tail.setNext(temp.next()); this.count--; return temp.value(); } code based on Bailey pg “Fred”“Wilma”“Barney”3 2 temp Check the boundaries! If the list has one node, bind the tail null.
©Duane Szafron SinglyLinkedList - 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.tail.next().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 CircularList - addToTail() public void addToTail (Object anObject) { // pre: anObject is non-null // post: the object is added at the end of the list this.addToHead(anObject); this.tail = this.tail.next(); } code based on Bailey pg. 123 “Fred”“Wilma”“Barney”3434“Pebbles” Note that “Fred” remains as Head node/
©Duane Szafron CircularList - removeFromTail() 1 public Object removeFromTail() { // pre: list is not empty // post: the last object in the list is removed and returned SinglyLinkedListElement cursor; SinglyLinkedListElement temp; Assert.pre(!this.isEmpty(), “List is not empty”); cursor = this.tail; while (cursor.next() != this.tail) cursor = cursor.next(); //move cursor to 2nd last node temp = this.tail; code based on Bailey pg. 123 “Fred”“Wilma”“Barney”3 cursor 1 1 temp 2 2
©Duane Szafron CircularList - removeFromTail() 2 if (cursor == this.tail) // one node list this.tail = null; else { cursor.setNext(this.tail.next()); this.tail = cursor; } this.count--; return temp.value(); } code based on Bailey pg. 123 “Fred”“Wilma”“Barney”3 2 cursor 1 temp