Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”

Similar presentations


Presentation on theme: "Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”"— Presentation transcript:

1 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

2 Doubly-, Circularly-Linked List with Header Node

3 Doubly-, Circularly-Linked List with Header Node
An empty list: head

4 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”

5 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

6 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

7 The Deque Class: Constructor
public Deque () // Constructor { // Create header node header = new DequeNode(); header.lt = header; header.rt = header; } // Constructor header

8 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

9 Redrawing this: header a

10 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

11 Redrawing this: header a b

12 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

13 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

14 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

15 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

16 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.

17 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

18


Download ppt "Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”"

Similar presentations


Ads by Google