Download presentation
Presentation is loading. Please wait.
1
Circularly-Linked Lists Cmput 115 - 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
2
©Duane Szafron 2000 2 About This Lecture In this lecture we will learn about an implementation of the List Interface called Circular List.
3
©Duane Szafron 2000 3 Outline Drawing Circular Lists CircularList class
4
©Duane Szafron 2000 4 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
5
©Duane Szafron 2000 5 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
6
©Duane Szafron 2000 6 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
7
©Duane Szafron 2000 7 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
8
©Duane Szafron 2000 8 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
9
©Duane Szafron 2000 9 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. 122 3 4 1 2 “Fred”“Wilma”“Barney”3 4“Pebbles” temp 1 2 4 Check the boundaries! If the list is empty, bind the tail to the new node. 3
10
©Duane Szafron 2000 10 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. 110 1 2 3 “Fred”“Wilma”“Barney”3 2 temp 1 2 3 Check the boundaries! If the list has one node, bind the tail null.
11
©Duane Szafron 2000 11 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
12
©Duane Szafron 2000 12 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” 1 1 2 2 Note that “Fred” remains as Head node/
13
©Duane Szafron 2000 13 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
14
©Duane Szafron 2000 14 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 2 3 3 4 4 5 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.