Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4. public class Deque<Item> implements Iterable<Item>
  5. {
  6.     private int N;          // size of the deque
  7.     private Node first;     // top of deque
  8.     private Node last;      // tail of deque
  9.  
  10.    private class Node // helper linked list class
  11.    {
  12.       private Item item;
  13.       private Node next;
  14.       private Node prev;
  15.    }
  16.    public Deque()                           // construct an empty deque
  17.    {
  18.  
  19.       first = null;
  20.       last = null;
  21.       N = 0;
  22.    }
  23.    public boolean isEmpty()                 // is the deque empty?
  24.    {
  25.       return first == null;
  26.    }
  27.    public int size()                        // return the number of items on the deque
  28.    {
  29.       return N;
  30.    }
  31.    public void addFirst(Item item)          // add the item to the front
  32.    {
  33.         if (item == null) throw new NullPointerException();
  34.         Node oldfirst = first;
  35.         first = new Node();
  36.         first.item = item;
  37.         first.next = oldfirst;
  38.         first.prev = null;
  39.         if (size() == 0) last = first;
  40.         else oldfirst.prev = first;
  41.         N++;
  42.  
  43.    }
  44.    public void addLast(Item item)           // add the item to the end
  45.    {
  46.      if (item == null) throw new NullPointerException();
  47.      Node oldlast = last;
  48.      last = new Node();
  49.      last.item = item;
  50.      last.prev = oldlast;
  51.      last.next = null;
  52.      if (size() == 0) first = last;
  53.      else   oldlast.next = last;
  54.      N++;
  55.    }
  56.    public Item removeFirst()                // remove and return the item from the front
  57.    {
  58.       if (isEmpty()) throw new NoSuchElementException("Stack underflow");
  59.       Item item = first.item;        // save item to return
  60.       if (size() == 1)  
  61.       {
  62.         last = null;
  63.         first = null;
  64.       }          
  65.       else
  66.       {
  67.        // Node temp = first;
  68.        // temp.next = null;
  69.        first = first.next;            // delete first node
  70.       }
  71.       N--;
  72.       return item;                   // return the saved item
  73.     }
  74.    public Item removeLast()                 // remove and return the item from the end
  75.    {
  76.       if (isEmpty()) throw new NoSuchElementException("Deque underflow");
  77.       Item item = last.item;
  78.       if (size() == 1)  
  79.       {
  80.         last = null;
  81.         first = null;
  82.       }          
  83.       else
  84.       {
  85.       last = last.prev;
  86.       last.next = null;
  87.       }
  88.       N--;
  89.       return item;
  90.    }
  91.     public Iterator<Item> iterator()  { return new DequeIterator();  }  // return an iterator over items in order from front to end
  92.  
  93.     // an iterator, doesn't implement remove() since it's optional
  94.     private class DequeIterator implements Iterator<Item> {
  95.         private Node current = first;
  96.         private int len = N;
  97.         public boolean hasNext()  { return current != null;                     }
  98.         public void remove()      { throw new UnsupportedOperationException();  }
  99.        
  100.  
  101.         public Item next() {
  102.             if (!hasNext()) throw new NoSuchElementException();
  103.             if (N < len) remove();
  104.             Item item = current.item;
  105.             current = current.next;
  106.             return item;
  107.         }
  108.     }        
  109.    
  110.    public static void main(String[] args)   // unit testing
  111.    {
  112.       //  Deque <Integer> deque = new Deque<Integer>();
  113.       //   deque.isEmpty();
  114.       //    deque.isEmpty();
  115.       //    deque.addLast(2);
  116.       //    deque.removeFirst(); //     ==> 2
  117.       //    deque.isEmpty();
  118.       //    deque.isEmpty();
  119.       //    deque.addFirst(6);
  120.       //    deque.addFirst(3);
  121.       //    deque.addLast(34);
  122.       // //  // StdOut.println(mydeck.last.item);
  123.       // //  // mydeck.removeLast();
  124.       // //  // StdOut.println(mydeck.first.item);
  125.       // // // //  // StdOut.println(mydeck.size());
  126.       // // // //  // StdOut.println(mydeck.isEmpty());
  127.       // // // // // StdOut.println(mydeck.first.item);
  128.       //  for (Integer item : deque)
  129.       //  {
  130.       //   //mydeck.removeFirst();
  131.       //   StdOut.println(item);
  132.       //  }
  133.       //  //StdOut.println(mydeck.last.prev.item);
  134.    }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement