Advertisement
Guest User

3.2

a guest
Dec 14th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. /**
  2.  * Implementation of a double ended queue.
  3.  *
  4.  * @param <T>
  5.  *     Type of elements the queue can hold
  6.  */
  7. class DoubleEndedQueue<T> implements Deque<T> {
  8.   /**
  9.    * Position-based list to keep the elements of the queue
  10.    */
  11.   private final PositionList<T> list;
  12.  
  13.   /**
  14.    * Constructs a new queue.
  15.    * Chooses a circular linked list as an implementation of a position-based list.
  16.    */
  17.   public DoubleEndedQueue() {
  18.     this.list = new CLList<>();
  19.   }
  20.  
  21.   /**
  22.    * @return the number of elements in the queue.
  23.    */
  24.   @Override
  25.   public int size() {
  26.     return list.size();
  27.   }
  28.  
  29.   /**
  30.    * @return true iff the queue contains no elements.
  31.    */
  32.   @Override
  33.   public boolean isEmpty() {
  34.     return list.isEmpty();
  35.   }
  36.  
  37.   /**
  38.    * @return the element at the front of the queue
  39.    * @throws EmptyDequeException
  40.    *     iff the queue is empty
  41.    */
  42.   @Override
  43.   public T getFirst() throws EmptyDequeException {
  44.     if(list.getFirst() == null) {
  45.       throw new EmptyDequeException();
  46.     } else {
  47.       return list.getFirst().getElement();
  48.     }
  49.   }
  50.  
  51.   /**
  52.    * @return the element at the end of the queue
  53.    * @throws EmptyDequeException
  54.    *     iff the queue is empty
  55.    */
  56.   @Override
  57.   public T getLast() throws EmptyDequeException {
  58.     if(list.getLast() == null) {
  59.       throw new EmptyDequeException();
  60.     } else {
  61.       return list.getLast().getElement();
  62.     }
  63.   }
  64.  
  65.   /**
  66.    * Adds an element to the front of the queue.
  67.    *
  68.    * @param element
  69.    *     to add.
  70.    */
  71.   @Override
  72.   public void addFirst(T element) {
  73.     list.addFirst(element);
  74.   }
  75.  
  76.   /**
  77.    * Adds an element to the back of the queue.
  78.    *
  79.    * @param element
  80.    *     to add.
  81.    */
  82.   @Override
  83.   public void addLast(T element) {
  84.     list.addLast(element);
  85.   }
  86.  
  87.   /**
  88.    * Removes and return the element at the front of the queue.
  89.    *
  90.    * @return the element at the front of the queue
  91.    * @throws EmptyDequeException
  92.    *     iff the queue is empty
  93.    */
  94.   @Override
  95.   public T removeFirst() throws EmptyDequeException {
  96.     if(list.isEmpty()){
  97.       throw new EmptyDequeException();
  98.     }
  99.     return list.remove(list.getFirst());
  100.   }
  101.  
  102.   /**
  103.    * Removes and return the element at the end of the queue.
  104.    *
  105.    * @return the element at the end of the queue
  106.    * @throws EmptyDequeException
  107.    *     iff the queue is empty
  108.    */
  109.   @Override
  110.   public T removeLast() throws EmptyDequeException {
  111.     if(list.isEmpty()){
  112.       throw new EmptyDequeException();
  113.     }
  114.     return list.remove(list.getLast());
  115.   }
  116. }
  117.  
  118. /**
  119.  * DO NOT MODIFY
  120.  * Interface for the double ended queue.
  121.  */
  122. interface Deque<T> {
  123.   /**
  124.    * @return the number of elements in the deque.
  125.    */
  126.   public int size();
  127.  
  128.   /**
  129.    * @return true iff the deque contains no elements.
  130.    */
  131.   public boolean isEmpty();
  132.  
  133.   /**
  134.    * @return the element at the front of the dequeue
  135.    * @throws EmptyDequeException
  136.    *     iff the queue is empty
  137.    */
  138.   public T getFirst() throws EmptyDequeException;
  139.  
  140.   /**
  141.    * @return the element at the end of the dequeue
  142.    * @throws EmptyDequeException
  143.    *     iff the queue is empty
  144.    */
  145.   public T getLast() throws EmptyDequeException;
  146.  
  147.   /**
  148.    * Adds an element to the front of the deque.
  149.    *
  150.    * @param element
  151.    *     to add.
  152.    */
  153.   public void addFirst(T element);
  154.  
  155.   /**
  156.    * Adds an element to the back of the deque.
  157.    *
  158.    * @param element
  159.    *     to add.
  160.    */
  161.   public void addLast(T element);
  162.  
  163.   /**
  164.    * Removes and return the element at the front of the dequeue.
  165.    *
  166.    * @return the element at the front of the dequeue
  167.    * @throws EmptyDequeException
  168.    *     iff the queue is empty
  169.    */
  170.   public T removeFirst() throws EmptyDequeException;
  171.  
  172.   /**
  173.    * Removes and return the element at the end of the dequeue.
  174.    *
  175.    * @return the element at the end of the dequeue
  176.    * @throws EmptyDequeException
  177.    *     iff the queue is empty
  178.    */
  179.   public T removeLast() throws EmptyDequeException;
  180. }
  181.  
  182. /**
  183.  * DO NOT MODIFY
  184.  * Interface for a node based list.
  185.  *
  186.  * @param <T>
  187.  *     Type of elements the list can hold
  188.  */
  189. interface PositionList<T> {
  190.   /**
  191.    * @return the number of nodes/elements in the list.
  192.    */
  193.   public int size();
  194.  
  195.   /**
  196.    * @return true iff the list is empty.
  197.    */
  198.   public boolean isEmpty();
  199.  
  200.   /**
  201.    * @return the first node in the list or null if no such node exists.
  202.    */
  203.   public Position<T> getFirst();
  204.  
  205.   /**
  206.    * @return the last node in the list or null if no such node exists.
  207.    */
  208.   public Position<T> getLast();
  209.  
  210.   /**
  211.    * Finds the next element in the list given a position.
  212.    *
  213.    * @param p
  214.    *     position to find the next element of.
  215.    * @return the next element of p.
  216.    * @throws InvalidPositionException
  217.    *     iff p is invalid.
  218.    */
  219.   public Position<T> getNext(Position<T> p) throws InvalidPositionException;
  220.  
  221.   /**
  222.    * Finds the previous element in the list given a position.
  223.    *
  224.    * @param p
  225.    *     position to find the previous element of.
  226.    * @return the previous element of p.
  227.    * @throws InvalidPositionException
  228.    *     iff p is invalid.
  229.    */
  230.   public Position<T> getPrev(Position<T> p) throws InvalidPositionException;
  231.  
  232.   /**
  233.    * Adds an element to the front of the list.
  234.    *
  235.    * @param o
  236.    *     element to add.
  237.    */
  238.   public void addFirst(T o);
  239.  
  240.   /**
  241.    * Adds an element to th end of the list.
  242.    *
  243.    * @param o
  244.    *     element to add.
  245.    */
  246.   public void addLast(T o);
  247.  
  248.   /**
  249.    * Adds an element after a specified position.
  250.    *
  251.    * @param p
  252.    *     position to place element after.
  253.    * @param o
  254.    *     element to insert.
  255.    * @throws InvalidPositionException
  256.    *     iff p is invalid.
  257.    */
  258.   public void addAfter(Position<T> p, T o) throws InvalidPositionException;
  259.  
  260.   /**
  261.    * Adds an element before a specified position.
  262.    *
  263.    * @param p
  264.    *     position that the element should be placed in front of.
  265.    * @param o
  266.    *     element to insert.
  267.    * @throws InvalidPositionException
  268.    *     iff p is invalid.
  269.    */
  270.   public void addBefore(Position<T> p, T o) throws InvalidPositionException;
  271.  
  272.   /**
  273.    * Removes a position from the list.
  274.    *
  275.    * @param p
  276.    *     position to remove.
  277.    * @return the element of p.
  278.    * @throws InvalidPositionException
  279.    *     if p is invalid
  280.    */
  281.   public T remove(Position<T> p) throws InvalidPositionException;
  282.  
  283.   /**
  284.    * Changes the value of the given position to the given element.
  285.    *
  286.    * @param p
  287.    *     position to change the value of.
  288.    * @param o
  289.    *     the new element for p.
  290.    * @return the old element of p.
  291.    * @throws InvalidPositionException
  292.    *     iff p is invalid.
  293.    */
  294.   public T set(Position<T> p, T o) throws InvalidPositionException;
  295. }
  296.  
  297. /**
  298.  * DO NOT MODIFY
  299.  * Interface that represents a node in the list.
  300.  *
  301.  * @param <T>
  302.  *     Type of element to hold
  303.  */
  304. interface Position<T> {
  305.   T getElement();
  306. }
  307. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement