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

please provide complete Java class code with main() function, and write some com

ID: 3862679 • Letter: P

Question

please provide complete Java class code with main() function, and write some comment

1. Extend the queue ADT to include the following operations:

• last that will return the rear element of the queue (which shall not be deleted from the queue)

• length that will return the number of elements on the queue.

• enqueueN that will enqueue an array of N elements on the queue

• dequeueN that will dequeue and return an array of N elements from the queue Write an implementation for each operation and compute their time complexity in Big-O notation

Explanation / Answer


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class QueueMain<T> implements QueueADT<T> {

    private void enqueue(T t) {
        Node newNode = new Node();
        newNode.data = t;
        newNode.next = null;
        if (first == null)
            first = last = newNode;
        else
            last = last.next = newNode;
        length++;
    }

    private T dqueue() {
        Node temp = first;
        first = first.next;
        if (first == null)
            last = null;
        length --;
        return temp.data;
                  
    }
  
    class Node{
        T data;
        Node next;
    }
    Node first;
    Node last;
    int length;
    @Override
    public T last() {
        if(last == null)
            return null;
        return last.data;
    }

    @Override
    public int length() {
        return length;
    }

    @Override
    public void enqueueN(T[] array) {
        for (T t: array)
            enqueue(t);
    }

    @Override
    public void dequeueN(T[] array) {
        int n = array.length;
        for (int i = 0; i<n; i++)
            array[i] = dqueue();
    }
    public static void main(String[] args) {
        QueueMain<String> obj = new QueueMain();
        String[] a = {"A","B","C","D"};
        obj.enqueueN(a);
        System.out.println("length = " + obj.length());
        String[] b = new String[2];
        obj.dequeueN(b);
        for (String s:b)
            System.out.print(s+", ");
        System.out.println();
    }
  
}

interface QueueADT<T>{
    T last();
    int length();
    void enqueueN(T[] array);
    void dequeueN(T[] array);
}