Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.NoSuchElementException; public class QueueImpl<T> implements Qu

ID: 3817535 • Letter: I

Question

import java.util.NoSuchElementException;
public class QueueImpl<T> implements QueueInterface<T> {
        private Node first; // beginning of queue
        private Node last; // end of queue
        private int n; // number of elements in queue
        private final int max = 100; // maximum number of elements in queue
        // helper linked list class
       
    private static class Node {
        private Object item;
        private Node next;
    }
    public static void main(String[] args){
     
    }
    /*initializes an empty queue
     *
     */
    public QueueImpl() {
        first = null;
        last = null;
        n = 0;
    }
    /*returns true if this queue is empty
     *
     */
    public boolean isEmpty() {
        return first == null;
    }
    /*returns the number of items in this queue
     *
     */
    public int size() {
    return n;
    }
    @Override
        public void enqueue(Object newEntry) {
        if( n <= max){
        Node oldlast = last;
        last = new Node();
        last.item = newEntry;
        last.next = null;
        if(isEmpty())
        first = last;
        else
        oldlast.next = last;
       
         n++;
        }
    }
   
    @Override
    public T dequeue() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        Object item = first.item;
        first = first.next;
        n--;
        if (isEmpty()) last = null;
        return (T) item;
    }
   
    @Override
    public T getFront() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        return (T) first.item;
    }
   
    @Override
    public void clear() {
        first = null;
        last = null;
        n = 0;
    }
}

public interface Queuelnterface Adds a new entry to the back of this queue. @param newEntry An object to be added. public void enqueuem newEntry): Removes and returns the entry at the front of this queue. @return The object at the front of the queue. @throws Empty ueueException if the queue is empty before the operation. public T dequeue0; Retrieves the entry at the front of this queue. @return The object at the front of the queue. @throws E ueueException if the queue is empty. mptyQ public T getFront0: Detects whether this queue is empty. @return True if the queue is empty, or false otherwise. public boolean isEmpty(), Removes all entries from this queue. public void clearo generates a string containing all data in the queue and returns it public String toString0; end Queuelnterface

Explanation / Answer

class mains

{

public static void main(String []args)

{

Queueimpl obj=new Queueimpl;

obj.size();

obj.enqueue(obj);

obj.size();

obj.dequeue();

obj.getFront();

obj.clear();

}

this will tell the size of the queue and if want to insert some data then insert it