Question

In: Computer Science

Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...

Java queue linked list

/*
* Complete the enqueue(E val) method
* Complete the dequeue() method
* Complete the peek() method
* No other methods/variables should be added/modified
*/
public class A3Queue {
   /*
   * Grading:
   * Correctly adds an item to the queue - 1pt
   */
   public void enqueue(E val) {
       /*
       * Add a node to the list
       */
   }
   /*
   * Grading:
   * Correctly removes an item from the queue - 1pt
   * Handles special cases - 0.5pt
   */
   public E dequeue() {
       /*
       * Remove a node from the list and return it
       */
       return null;
   }
   /*
   * Grading:
   * Correctly shows an item from the queue - 1pt
   * Handles special cases - 0.5pt
   */
   public E peek() {
       /*
       * Show a node from the list
       */
       return null;
   }
  
   private Node front, end;
   private int length;
   public A3Queue() {
       front = end = null;
       length = 0;
   }
   private class Node {
       E value;
       Node next, prev;
       public Node(E v) {
           value = v;
           next = prev = null;
       }
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


public class A3Driver {
  
   public static void main(String[] args) {
      
  
       A3Queue queue = new A3Queue<>();
       queue.enqueue(5);
       queue.enqueue(20);
       queue.enqueue(15);
       System.out.println(queue.peek()+":5");
       System.out.println(queue.dequeue()+":5");
       queue.enqueue(25);
       System.out.println(queue.dequeue()+":20");
       System.out.println(queue.dequeue()+":15");

   }
}

Solutions

Expert Solution

/*
 * Complete the enqueue(E val) method
 * Complete the dequeue() method
 * Complete the peek() method
 * No other methods/variables should be added/modified
 */
public class A3Queue<E> {
    /*
     * Grading:
     * Correctly adds an item to the queue - 1pt
     */
    public void enqueue(E val) {
        /*
         * Add a node to the list
         */
        Node n = new Node(val);
        if (front == null) {
            front = end = n;
        } else {
            end.next = n;
            end = n;
        }
    }

    /*
     * Grading:
     * Correctly removes an item from the queue - 1pt
     * Handles special cases - 0.5pt
     */
    public E dequeue() {
        /*
         * Remove a node from the list and return it
         */
        E result = front.value;
        front = front.next;
        return result;
    }

    /*
     * Grading:
     * Correctly shows an item from the queue - 1pt
     * Handles special cases - 0.5pt
     */
    public E peek() {
        /*
         * Show a node from the list
         */
        return front.value;
    }

    private Node front, end;
    private int length;

    public A3Queue() {
        front = end = null;
        length = 0;
    }

    private class Node {
        E value;
        Node next, prev;

        public Node(E v) {
            value = v;
            next = prev = null;
        }
    }
}




Related Solutions

Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull functions. Test your code in main function with 10 elements Note: for each task, you are supposed to create three files as specified in task1, i.e., implementation file, interface file and file containing point of entry. in C++
java method for dequeue write the “dequeue” method for a queue of type double. If the...
java method for dequeue write the “dequeue” method for a queue of type double. If the queue is empty return 0.0. Make sure to change the links properly ensure that the data structure remains a queue. use the code provided below public class Q04 { public class ListNode//Public for testing purposes { public double data; public ListNode link; public ListNode(double aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//Public for testing purposes public ListNode...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. You may not add any more fields to the class. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initialize an empty queue.    */    public StringQueue() { //TODO   ...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an elment, it inserts the element at the beginnings of queue
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT