Advertisement
Guest User

LinkedDeque

a guest
Feb 26th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. /* THIS CODE IS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING
  2.  * A TUTOR OR CODE WRITTEN BY OTHER STUDENTS - Nathan Chou
  3.  */
  4.  
  5. // Homework: revise this file, see TODO items below.
  6.  
  7. // This LinkedDeque class is a linked list, and a partial
  8. // implementation of the java.util.Deque interface.  It is already
  9. // sufficient for its use by PathFinder.  You should improve it, by
  10. // fixing the TODO items.
  11.  
  12. // TODO: make this a doubly-linked list.  We already declared a
  13. // "previous" link in the Node class, but you need to properly
  14. // maintain those links.
  15.  
  16. // All methods here (except toString() and reverse()) should run in
  17. // constant time.   Iterators do not need to be fail-fast.
  18.  
  19. // As given, this is nearly a copy of LinkedQueue.java from our
  20. // textbook.  We added the second link in Node, We removed check() and
  21. // main().  We removed the "private" declarations, to allow
  22. // testing your code from external classes (like Test1.java,
  23. // which should still be able to compile when you are done).
  24.  
  25. import java.util.Iterator;
  26. import java.util.NoSuchElementException;
  27.  
  28. public class LinkedDeque<Item> implements Deque<Item>
  29. {
  30.     // Not private, just to allow testing.
  31.     int N;         // number of elements on deque
  32.     Node first;    // beginning of deque
  33.     Node last;     // end of deque
  34.  
  35.     // Linked list node.
  36.     class Node
  37.     {
  38.         Item item;
  39.         Node next;
  40.         Node previous;          // new!
  41.     }
  42.  
  43.     // Initialize empty deque
  44.     public LinkedDeque()
  45.     {
  46.         first = null;
  47.         last  = null;
  48.         N = 0;
  49.     }
  50.  
  51.     public boolean isEmpty() { return first == null; }
  52.  
  53.     public int size() { return N; }
  54.  
  55.     public void addLast(Item item)
  56.     {
  57.         // TODO: fix .previous links as necessary
  58.         // This is enqueue from LinkedQueue.java
  59.        
  60.         Node oldlast = last;
  61.         last = new Node();
  62.         last.item = item;
  63.         last.next = null;
  64.         if (isEmpty())
  65.             first = last;
  66.         else {
  67.             oldlast.next = last;
  68.             last.previous = oldlast;
  69.         }  
  70.         ++N;
  71.     }
  72.  
  73.     public void addFirst(Item item)
  74.     {
  75.         // TODO: fix .previous links as necessary
  76.         // This is push from LinkedStack.java (also updating last)
  77.         Node oldfirst = first;
  78.         first = new Node();
  79.         first.item = item;
  80.         first.next = oldfirst;
  81.         first.previous = null;
  82.         if (last == null)
  83.             last = first;
  84.         else
  85.             oldfirst.previous = first;
  86.         ++N;
  87.     }
  88.  
  89.     public Item removeFirst()
  90.     {
  91.         // TODO: fix .previous links as necessary
  92.         // This is dequeue from LinkedQueue.java
  93.         if (isEmpty()) throw new NoSuchElementException();
  94.         Item item = first.item;
  95.         first = first.next;
  96.         N--;
  97.         if (isEmpty())
  98.             last = null;
  99.         else
  100.             first.previous = null;
  101.         return item;
  102.     }
  103.  
  104.     public Item removeLast()
  105.     {
  106.         // TODO: implement this method
  107.         if (isEmpty()) throw new NoSuchElementException();
  108.         Item item = last.item;
  109.         last = last.previous;
  110.         N--;
  111.         if (isEmpty())
  112.             first = null;
  113.         else
  114.             last.next = null;
  115.         return item;
  116.         //throw new UnsupportedOperationException();
  117.     }
  118.  
  119.     // Return a string representation: "[first, second, last]".
  120.     public String toString()
  121.     {
  122.         // TODO: this takes time at least quadratic in N!
  123.         // Modify it to use a StringBuilder, so it takes time
  124.         // that is linear in the length of its output.
  125.         String s = "[", sep = "";
  126.         for (Item item: this) {
  127.             s += sep + item;
  128.             sep = ", ";
  129.         }
  130.         return s + "]";
  131.     }
  132.  
  133.     // A standard iterator (visits items from first to last).
  134.     public Iterator<Item> iterator() { return new Iter(); }
  135.  
  136.     private class Iter implements Iterator<Item>
  137.     {
  138.         private Node current = first;
  139.         public boolean hasNext()
  140.         {
  141.             return current != null;
  142.         }
  143.         public void remove()
  144.         {
  145.             // TODO(EC): implement this method.
  146.             // It should be constant time.
  147.             // Note remove() applies to the item last returned by
  148.             // next().  The user should make at most one call to
  149.             // remove() between two calls of next().  If remove()
  150.             // is called a second time (without another next()), it
  151.             // should throw an IllegalStateException.
  152.             throw new UnsupportedOperationException();
  153.         }
  154.         public Item next()
  155.         {
  156.             if (!hasNext()) throw new NoSuchElementException();
  157.             Item item = current.item;
  158.             current = current.next;
  159.             return item;
  160.         }
  161.     }
  162.  
  163.     // A reverse iterator (visits items from last to first).
  164.     public Iterator<Item> descendingIterator()
  165.     {
  166.         // TODO: implement this method.
  167.         throw new UnsupportedOperationException();
  168.     }
  169.  
  170.     public void reverse()
  171.     {
  172.         // TODO(EC): implement this method.
  173.         // You should reuse the existing Node objects, rather than
  174.         // creating new ones.  You should modify their next/previous
  175.         // links, not their item links.
  176.         throw new UnsupportedOperationException();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement