Advertisement
Guest User

Untitled

a guest
Nov 27th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. /**
  2.  * Write a description of class Deque here.
  3.  *
  4.  * @author (your name)
  5.  * @version (a version number or a date)
  6.  */
  7.  
  8. import java.util.NoSuchElementException;
  9.  
  10. public class Deque<E> implements Queue<E>
  11. {
  12.     private static class LLNode<E>
  13.     {
  14.         private E data;        
  15.         private LLNode<E> next;
  16.         private LLNode<E> previous;
  17.        
  18.         public LLNode(E data, LLNode<E> next, LLNode<E> previous)
  19.         {
  20.             this.data = data;
  21.             this.next = next;
  22.             this.previous = previous;
  23.         }
  24.     }
  25.    
  26.    
  27.     private LLNode<E> head, tail;
  28.     private int size;  
  29.    
  30.     public boolean isEmpty()
  31.     {
  32.         return head == null;
  33.     }
  34.  
  35.    
  36.     public void enqueueFront(E newData)
  37.     {
  38.         if (isEmpty()) {
  39.             head = tail = new LLNode<E>(newData, null, null);
  40.  
  41.        
  42.         } else {
  43.             tail.next = new LLNode<E>(newData, null, null);
  44.             tail = tail.next;
  45.         }
  46.  
  47.         size++;
  48.     }
  49.    
  50.     public void enqueueBack(E newData)
  51.     {        
  52.         if (isEmpty()) {
  53.             head = tail = new LLNode<E>(newData, null, null);
  54.         } else {
  55.             tail.previous = new LLNode<E>(newData, null, null);
  56.             tail = rail.previous;
  57.         }
  58.  
  59.         size++;
  60.     }
  61.    
  62.     public E dequeueFront()
  63.     {
  64.         if (!isEmpty()) {
  65.             E temp = head.data;
  66.             head = head.next;
  67.             size--;
  68.  
  69.             if (size == 0)
  70.                 tail = null;
  71.  
  72.             return temp;
  73.         } else {
  74.             throw new NoSuchElementException();
  75.         }
  76.     }
  77.    
  78.     public E dequeueBack()
  79.     {
  80.         if (!isEmpty()) {
  81.             E temp = head.data;
  82.             head = head.previous;
  83.             size--;
  84.            
  85.             if (size == 0)
  86.                 tail = null;
  87.  
  88.             return temp;
  89.         } else {
  90.             throw new NoSuchElementException();
  91.         }
  92.     }
  93.    
  94.     public E peekFront()
  95.     {
  96.         if (!isEmpty())
  97.             return head.data;
  98.         else
  99.             throw new NoSuchElementException();
  100.     }
  101.    
  102.     public E peekBack()
  103.     {
  104.         if (!isEmpty())
  105.             return tail.data;
  106.         else
  107.             throw new NoSuchElementException();
  108.     }
  109.    
  110.     public String toString()
  111.     {
  112.         String s = "LLQueue (size = " + size + "), containing (front to back): ";
  113.         LLNode<E> temp = head;
  114.         while (temp != null) {
  115.             s += temp.data + " ";
  116.             temp = temp.next;
  117.         }
  118.         return s;
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement