Doubly-Linked Lists Cmput 115 - Lecture 16 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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
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)
Doubly Linked Lists Representation Space Analysis Creation and Insertion Traversal Deletion.
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.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
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.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
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.
Chapter 5 Linked Lists II
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
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.
Stack: a Linked Implementation
Java Methods Lists and Iterators Object-Oriented Programming
Doubly Linked List Review - We are writing this code
Activity Write the code fragment to reverse a singly linked list
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists [AJ 15].
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Doubly Linked List Implementation
Recursive Objects Singly Linked Lists.
Activity Write the code fragment to reverse a singly linked list
Doubly Linked List Implementation
Doubly Linked Lists Representation Space Analysis
Presentation transcript:

Doubly-Linked Lists Cmput 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 About This Lecture In this lecture we will learn about an implementation of the List Interface called Doubly-Linked List.

©Duane Szafron Outline Drawing Doubly Linked Lists DoublyLinkedListElement class DoublyLinkedList class

©Duane Szafron Doubly-Linked List Diagrams A doubly-linked list node has two links, one forward and one backward. doubly-linked list The doubly-linked list has references to its head and tail nodes. This symmetry makes the implementation simpler. “Fred”“Wilma”“Barney”3

©Duane Szafron Constructing a Node When a DoublyLinkedListElement (node) is constructed, four links may need to be set. “Fred” “Wilma” “Barney” If one or both of the “neighboring” nodes is null then fewer links must be set. “Wilma” “Barney” null

©Duane Szafron DoublyLinkedListElement 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 previousNodenextNode

©Duane Szafron DoublyLinkedListElement 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 DoublyLinkedList - State & 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 DoublyLinkedList - 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.head = null; this.tail = null; this.count = 0; } code based on Bailey pg. 108

©Duane Szafron DoublyLinkedList - 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 traversal while ((cursor != null) && (!cursor.value().equals(anObject)) cursor = cursor.next(); return cursor != null; } code based on Bailey pg. 114

©Duane Szafron DoublyLinkedList 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 head “Fred”“Wilma”“Barney” 3 DoublyLinkedList - addToHead() 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”

©Duane Szafron DoublyLinkedList - addToHead() 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 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 DoublyLinkedList - 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 necessary if (this.tail == null) this.tail = this.head; this.count++; } code based on Bailey pg. 119

©Duane Szafron DoublyLinkedList - 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 “Fred”“Wilma”“Barney”3 4 temp 4 3

©Duane Szafron DoublyLinkedList - removeFromHead() 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 “Fred” temp 4 Check the boundaries! If the list has one node, the list tail must be bound to null. 3

©Duane Szafron DoublyLinkedList - 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 DoublyLinkedList - 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 DoublyLinkedList - 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 “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 DoublyLinkedList - 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 Some Principles from the Textbook 12. Question asymmetry principles from Bailey ch. 6