Answered step by step
Verified Expert Solution
Question
1 Approved Answer
adt queue and stack public class ListQueue implements MyQueue { private class Node { T item; // public Node next; // null } private Node
adt queue and stack public class ListQueueimplements MyQueue { private class Node { T item; // public Node next; // null } private Node head = null; private Node tail = null; private int size = 0; /** * is empty? */ public boolean isEmpty() { // your implementation here: return head == null && tail == null; } /** * @return the number of items in the queue */ public int size() { // your implementation here: return size; } /** * enqueue: add an element at the tail of queue */ public void enqueue(T item) { Node temp = new Node(); temp.item = item; if (this.tail == null) { this.head = this.tail = temp; return; } this.tail.next = temp; this.tail = temp; size ++; } /** * dequeue: remove and return the head of the queue * @return the deleted value * @throws NoSuchElementException if queue is empty */ public T dequeue() { if (this.head == null) return null; Node temp = this.head; this.head = this.head.next; if (this.head == null) this.tail = null; size --; return temp.item; } /** * peek: view the head of queue without removing it. * @return Null if queue is empty */ public T peek() { if (head == null) { return null; } return head.item; } } public interface MyQueue { /** * is empty? */ public boolean isEmpty(); /** * @return the number of items in the queue */ public int size(); /** * enqueue: add an element at the tail of queue */ public void enqueue(T item); /** * dequeue: remove and return the head of the queue * @return the deleted value * @throws NoSuchElementException if queue is empty */ public T dequeue(); /** * peek: view the head of queue without removing it. * @return null if queue is empty */ public T peek(); } Just finish below is ok public class Driver{ public static void main(String[] args) { MyQueue q; boolean useList = false; // make it false if you want to array implementation if (useList) q = new ListQueue(); // default setting: use a list to implement the queue. You can change it. else q = new ArrayQueue(); for(int i = 0; i < 1000; i++) // add a large number of items onto the queue { q.enqueue(i); } System.out.println("Now, dequeue items!"); while(!q.isEmpty()) { System.out.print(q.dequeue() + " "); } System.out.println(" End of dequeueing"); // you should fully test/debug all methods! } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started