In: Computer Science
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");
}
}
/* * 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; } } }