Advertisement
Guest User

Week3_Opdracht4

a guest
Sep 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2.  
  3.  
  4. class Queue<T> {
  5.   private Stack<T> s1 = new LibraryStack<>();
  6.   private Stack<T> s2 = new LibraryStack<>();
  7.  
  8.   /**
  9.    * @return true iff there are no elements left.
  10.    */
  11.   public boolean isEmpty() {
  12.     if (s1. isEmpty() && s2.isEmpty()) { return true;}
  13.    
  14.     return false;
  15.   }
  16.  
  17.   /**
  18.    * @return the number of elements in the queue.
  19.    */
  20.   public int size() {
  21.     int size = (s1.size() + s2.size());
  22.     return size;
  23.   }
  24.  
  25.   /**
  26.    * Adds an element to the queue.
  27.    *
  28.    * @param i
  29.    *            element to enqueue.
  30.    */
  31.   public void enqueue(T i) {
  32.     s1.push(i);
  33.  
  34.   }
  35.  
  36.   /**
  37.    * Removes the first element from the queue.
  38.    *
  39.    * @return the first element from the queue.
  40.    * @throws NoSuchElementException
  41.    *             iff the queue is empty.
  42.    */
  43.   public T dequeue() throws NoSuchElementException {
  44.     if (s1.isEmpty() && s2.isEmpty()) {
  45.       //throw new NoSuchElementException();
  46.      
  47.     }
  48.    
  49.     if (s2.isEmpty()){
  50.       while( !s1.isEmpty()) { // for removing the whole stack 1 and put in stack 2
  51.         s2.push(s1.pop());
  52.       }
  53.     }
  54.     return s2.pop();
  55.   }
  56.  
  57.   /**
  58.    * Only returns (i.e. does not remove) the first element from the queue.
  59.    *
  60.    * @return the first element from the queue.
  61.    * @throws NoSuchElementException
  62.    *             iff the queue is empty.
  63.    */
  64.   public T first() throws NoSuchElementException {
  65.     if (s1.isEmpty() && s2.isEmpty()) {
  66.       throw new NoSuchElementException();
  67.      
  68.     }
  69.    
  70.     if (s2.isEmpty()){
  71.       while( !s1.isEmpty()) { // for removing the whole stack
  72.         s2.push(s1.pop());
  73.       }
  74.     }
  75.     return s2.peek();  
  76.    
  77.    
  78.   }
  79.  
  80. }
  81.  
  82.  
  83. /**
  84.  * Interface for a Stack.
  85.  *
  86.  * DO NOT MODIFY
  87.  *
  88.  * @param <T> Type of elements the Stack can hold
  89.  */
  90. interface Stack<T> {
  91.  
  92.   /**
  93.    * @return true iff it contains no elements.
  94.    */
  95.   public boolean isEmpty();
  96.  
  97.   /**
  98.    * @return the number of elements in the stack.
  99.    */
  100.   public int size();
  101.  
  102.   /**
  103.    * Add an element to the top of the stack
  104.    *
  105.    * @param e
  106.    *            element to push.
  107.    */
  108.   public void push(T e);
  109.  
  110.   /**
  111.    * Removes the top element from the stack.
  112.    *
  113.    * @return the first element.
  114.    * @throws NoSuchElementException
  115.    *             iff the stack is empty
  116.    */
  117.   public T pop() throws NoSuchElementException;
  118.  
  119.   /**
  120.    * @return the top element (does not pop it).
  121.    * @throws NoSuchElementException
  122.    *             iff the stack is empty
  123.    */
  124.   public T peek() throws NoSuchElementException;
  125.  
  126. }//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement