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

In the file Queue.java (provided with this homework), a Queue class is defined w

ID: 664689 • Letter: I

Question

In the file Queue.java (provided with this homework), a Queue class is defined which allows regular queue operations, and the queue is implemented based on an int array. Here we only use one pointer next, instead of two as mentioned in the lecture (in other words, in this question front is always 0). In this problem you need to (1) Complete the two methods: enqueue and dequeue. Hint: (1) when enqueuing, you need to do resizing if the current array is full, and the resize() method is already given; (2) when dequeuing, after you remove A[0] from the array, you need to move everything over, i.e., move A[i] to A[i 1], and adjust next. (2) Design your own tests (at least 10 tests) in the main method to show the correctness of your code. You can take a look at how tests are designed in the first question to get an idea. Note: two methods, printArray and toString, are also provided to help you debug your code.

Following is the code of Queue.java

public class Queue {

private int[] A = new int[10];
private int next = 0;

public void enqueue(int k) {
// your code here
// just put the new element at the end
}

public int dequeue() {
// your code here
// save A[0] in a temporary variable, then move everything over, and reset next
return 0; // your code here
}

public int size() {
return next;
}

public boolean isEmpty() {
return (next == 0);
}

public boolean isFull() {
return (next == A.length);
}

private void resize() {
int[] B = new int[A.length * 2];
for(int i = 0; i < A.length; ++i)
B[i] = A[i];
A = B;
}

public String toString() {
  
String s = "[";
if(isEmpty()) {
return s + "]";
}
else {
for(int i = next-1; i > 0; --i) {
s += (A[i] + ",");
}
s += A[0] + "]";
return s;
}
}

// debugging routine
private void printArray() {
System.out.print("[");
if(isEmpty()) {
System.out.println("]");
return;
}
else {
for(int i = 0; i < A.length-1; ++i) {
System.out.print(A[i] + ",");
}
System.out.println(A[A.length-1] + "] next = " + next);
}
}

// Unit Test
public static void main(String[] args) {


  
}
}

Explanation / Answer

Try this :

public class LinkedQueue implements Queue {
    private class Node {
        public Object data;
        public Node next;
        public Node(Object data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    private Node head = null;
    private Node tail = null;

    public void enqueue(Object item) {
        Node newNode = new Node(item, null);
        if (isEmpty()) {head = newNode;} else {tail.next = newNode;}
        tail = newNode;
    }

    public Object dequeue() {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        Object item = head.data;
        if (tail == head) {
            tail = null;
        }
        head = head.next;
        return item;
    }

    public Object peek() {
        if (head == null) {
            throw new NoSuchElementException();
        }
        return head.data;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public int size() {
        int count = 0;
        for (Node node = head; node != null; node = node.next) {
            count++;
        }
        return count;
    }
}

or also you can try this :


public class ListQueue {
public static void main(String[] args){
    Queue myQueue;
    Scanner sc = new Scanner(System.in);
    String input;
    int choice = 99;
    do{
        System.out.println("================");
        System.out.println("Queue Operations Menu");
        System.out.println("================");
        System.out.println("1,Enquene");
        System.out.println("2,Dequeue");
        System.out.println("3,Empty?");
        System.out.println("4,Count?");
        System.out.println("5,View Queue");
        System.out.println("0, Quit ");
        System.out.println("Enter Choice:");
        try{
            choice = sc.nextInt();
            switch(choice){
            case 1:
                System.out.println("Please enter name: ");
                input = sc.next();
                myQueue.enqueue(input);
                System.out.println(input + "is successful queued");
                break;
            case 2:
                if(myQueue.isEmpty()){

                }
                break;
            case 3:
                if(myQueue.isEmpty()){
                    System.out.println("Queue is empty");
                }else{
                    System.out.println("Queue is not empty");
                }
                break;
            case 4:
                System.out.println("Number of people is " + "the queue" + myQueue.size());
                break;
            case 5:
                if(!myQueue.isEmpty())
                    myQueue.viewQueue();
                else
                    System.out.println("Queue is empty");
                break;
            case 0:
                System.out.println("Good-bye");
                break;
            default:
                    System.out.println("Invalid choice");
            }
        }
        catch(InputMismatchException e){
            System.out.println("Please enter 1-5, 0 to quit");
            sc.nextLine();
        }
    }while(choice != 0);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote