Question

Complete the following functions in the Queue.java/Queue.h files (ATTACHED BELOW): a. void enqueue(int val) b. int...

Complete the following functions in the Queue.java/Queue.h files (ATTACHED BELOW):
a. void enqueue(int val)
b. int dequeue()
c. int getSize()

public class Queue {
  
private int maxQueueSize, front, rear, currentSize;
private int[] queue;
  
public Queue(int maxQueueSize) {
if (maxQueueSize <= 0)
System.out.println("Queue size should be a positive integer.");
else {
this.maxQueueSize = maxQueueSize;
front = rear = 0;
currentSize = 0;
queue = new int[maxQueueSize];
}
}
  
public void enqueue(int val) { // complete this function
}
  
public int dequeue() { // complete this function
}
  
public int getSize() {// complete this function
}
  
public String toString() {
if (getSize() == 0)
return "[]";
else {
String output = "[";
if (rear > front) {
for (int i = front; i < rear - 1; i++)
output += queue[i] + ", ";
output += queue[rear - 1] + "]";
} else {
for (int i = front; i < maxQueueSize - 1; i++)
output += queue[i] + ", ";
output += queue[maxQueueSize - 1];
  
for (int i = 0; i < rear; i++)
output += ", " + queue[i];
output += "]";
}
return output;
}
}
}

Homework Answers

Answer #1

public class Queue {

   private int maxQueueSize, front, rear, currentSize;
   private int[] queue;

   public Queue(int maxQueueSize) {
       if (maxQueueSize <= 0)
           System.out.println("Queue size should be a positive integer.");
       else {
           this.maxQueueSize = maxQueueSize;
           front = rear = 0;
           currentSize = 0;
           queue = new int[maxQueueSize];
       }
   }

   public void enqueue(int val) { // complete this function
       // check queue is full or not
       if (
maxQueueSize== rear) {
           System.out.printf("\nQueue is full\n");
           return;
       }

       // insert element at the rear
       else {
           queue[rear] = val;
           rear++;
       }
   }

   public int dequeue() { // complete this function
       // check for queue underflow
       if (front == rear) {
           System.out.println("UnderFlow\nProgram Terminated");
           System.exit(1);
       }

       int res = queue[front];

       front = (front + 1) % maxQueueSize;
       currentSize--;
       return res;
   }

   public int getSize() {// complete this function
       return rear;
   }

   public String toString() {
       if (getSize() == 0)
           return "[]";
       else {
           String output = "[";
           if (rear > front) {
               for (int i = front; i < rear - 1; i++)
                   output += queue[i] + ", ";
               output += queue[rear - 1] + "]";
           } else {
               for (int i = front; i < maxQueueSize - 1; i++)
                   output += queue[i] + ", ";
               output += queue[maxQueueSize - 1];

               for (int i = 0; i < rear; i++)
                   output += ", " + queue[i];
               output += "]";
           }
           return output;
       }
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you need to write a method that takes a Queue and sorts it using the selection sort idea. The implementation of the Queue class for long data type is given in the lecture slides (the code skeleton including the implementation of the Queue class is provided below for your convenience). You should go over the Queue and use enqueue(), dequeue(),...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Use the TestTime.java/TestTime.cpp le to compare the two queue implementations. Note that enqueue function when implemented...
Use the TestTime.java/TestTime.cpp le to compare the two queue implementations. Note that enqueue function when implemented using an array has O(1) complexity, and when using two arrays has O(n) complexity, where n is the current size of the queue. So, the two stack implementation should take more time, which is substantiated by the experiment (time output). public class TestTime {    public static void main(String[] args) throws Exception {        for (int maxSize = 10000; maxSize <= 50000; maxSize...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initializes an empty queue.    */    public StringQueue() { //TODO    }    /**    * Returns true if this queue...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
can you please explain how to complete all methods in java ? thanks /* Note:   Do...
can you please explain how to complete all methods in java ? thanks /* Note:   Do not add any additional methods, attributes.         Do not modify the given part of the program.         Run your program against the provided Homework2Driver.java for requirements. */ /* Hint:   This Queue implementation will always dequeue from the first element of         the array i.e, elements[0]. Therefore, remember to shift all elements         toward front of the queue after each dequeue. */ public class QueueArray<T>...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT