Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.” – Gloria Steinem
Doubly-, Circularly-Linked List with Header Node
Doubly-, Circularly-Linked List with Header Node An empty list: head
Advantages No special cases The list is never empty! We can move freely through the list in either direction Still cannot access elements “at random”
A Java Class for Deques Use a doubly-, circularly-linked list with a header node Class diagram: Deque header addLeft, addRight, removeLeft, removeRight, rightHead, leftHead, isEmpty
The Deque Class public class Deque<T> { private class DequeNode { public T data; public DequeNode lt, // Ptr to left node rt; // Ptr to right node } // inner class DequeNode private DequeNode header; // Ptr to header . . . } // class Deque
The Deque Class: Constructor public Deque () // Constructor { // Create header node header = new DequeNode(); header.lt = header; header.rt = header; } // Constructor header
The addLeft Method public void addLeft (T item) // Add item to left end { DequeNode newNode = new DequeNode(); newNode.data = item; newNode.rt = header.rt; newNode.lt = header; header.rt.lt = newNode; header.rt = newNode; } // addLeft newNode a header
Redrawing this: header a
The addLeft Method (cont.) public void addLeft (T item) // Add item to left end { DequeNode newNode = new DequeNode(); newNode.data = item; newNode.rt = header.rt; newNode.lt = header; header.rt.lt = newNode; header.rt = newNode; } // addLeft newNode b header a
Redrawing this: header a b
Comments No special cases The list is never empty public void addLeft (T item) // Add item to left end { DequeNode newNode = new DequeNode(); newNode.data = item; newNode.rt = header.rt; newNode.lt = header; header.rt.lt = newNode; header.rt = newNode; } // addLeft
Removing an Element public T removeLeft () // Remove item from left end { if (header.rt == header) throw new EmptyException(…); DequeNode tmpPtr = header.rt; T tmpData = tmpPtr.data; header.rt = tmpPtr.rt; tmpPtr.rt.lt = header; return tmpData; } // removeLeft
Examining an Element and Checking for an Empty Deque public T rightHead () // Return item at right end { if (header.lt == header) throw new EmptyException(…); return header.lt.data; } // rightHead public boolean isEmpty () // TRUE if no items in deque { return header.lt == header; } // isEmpty
Doubly-, Circularly-Linked List with Header Node Can be used for any type of list ADT not just deques Removes the need for special cases Simplifies algorithms no “previous” pointers required
Applications of Deques Very general data structure Can be used to implement stacks or queues Problem solving partly prioritise some possible solutions Applications needing more flexible list handling card games, etc.
Summary of List ADTs in Chapters Four and Five List of int Generic Lists Stacks Queues Deques ADT IntegerVector ArrayStack ArrayQueue Array IntegerList ObjectList GenericList ListStack ListQueue Deque Linked List