Circularly-Linked Lists Cmput 115 - Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Stacks, Queues, and Linked Lists
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Data Abstraction - Interfaces and Implementations Cmput Lecture 1 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Section 2.6 Linked List StringLog ADT Implementation
Efficiency of in Binary Trees
Doubly Linked List Review - We are writing this code
Week 3 - Friday CS221.
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Linked Lists [AJ 15].
Copyright © Aiman Hanna All rights reserved
Hashing in java.util
Web Design & Development Lecture 6
Presentation transcript:

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