Question

You must use an array to hold the items in the queue. In other words, one...

You must use an array to hold the items in the queue. In other words, one of your fields should be an array for holding the items, but you may not have any other container fields (no other arrays, lists, stacks, queues, etc.) You will need at least one additional field (an int).

package hw5;

public class GeneralizedQueue {
  
/**
* Creates an empty queue.
*/
public GeneralizedQueue() {
// TODO
}
  
/**
* Checks if the queue is empty
* @return true if the queue is empty
* and false otherwise.
*/
public boolean isEmpty() {
// TODO
}
  
/**
* Add an item to the back of the queue.
* @param x the item to be addded to the queue
*/
public void insert(Item x) {
// TODO
}
  
/**
* delete and return the kth least recently inserted item
* (the kth item "in line".)
* @param k indicates which position from the queue to be removed.
* The first item is in position 1.
* @return the kth oldest item in the queue
*/
public Item delete(int k) {
// TODO
}
}

// CHECK AGAINST THE FOLLOWING TEST FILE

package hw5;

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class HW5Test {

   @Rule
public Timeout globalTimeout = Timeout.seconds(1);
  
  
   @Test
   public void test() {
       Integer[] data = new Integer[10];
       for(int i = 0; i < data.length; i++)
           data[i] = new Integer(i);
       GeneralizedQueue<Integer> gq = new GeneralizedQueue<Integer>();
       for(int i = 0; i < data.length; i++) {
           gq.insert(data[i]);
       }
       // gq = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
       assertEquals(data[1], gq.delete(2)); // deletes 2nd item
       // gq = 0, 2, 3, 4, 5, 6, 7, 8, 9
       assertEquals(data[3], gq.delete(3)); // deletes 3rd item
       // gq = 0, 2, 4, 5, 6, 7, 8, 9
       assertEquals(data[5], gq.delete(4)); // deletes 4th item
       // gq = 0, 2, 4, 6, 7, 8, 9
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package hw5;

/* util package is use import Scanner class */
import java.util.*;
public class GeneralizedQueue {
int size; // total capacity of the queue
Integer queue[]; // array to hold the elements of the queue
int pointer; // specifies the index position of the last inserted item in the queue
/**
* Creates an empty queue.
*/
public GeneralizedQueue() {
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of the queue :: ");
size = sc.nextInt();
queue = new Integer[size];
pointer = -1;
// indicates that the queue is empty intially
for(int i = 0; i < queue.length; i++)
insert(new Integer(sc.nextInt()));
//adds an element into the queue
}
  

/**
* Checks if the queue is empty
* @return true if the queue is empty
* and false otherwise.
*/
public boolean isEmpty() {
if(pointer == -1)
return true;
else
return false;
}
  

/**
* Add an item to the back of the queue.
* @param x the item to be addded to the queue
*/
public void insert(Integer x) {
if(pointer<size-1){
/* if the current pointer position is less than the capacity of queue */
pointer = pointer + 1; /* move the pointer to an empty location in the queue */
queue[pointer] = x; /* place the item in that location */
}else{
System.out.println("The queue is already full");
}


}
  

/**
* delete and return the kth least recently inserted item
* (the kth item "in line".)
* @param k indicates which position from the queue to be removed.
* The first item is in position 1.
* @return the kth oldest item in the queue
*/
public Integer delete(int k) {

System.out.println("Position to delete : " + k);
Integer item = new Integer(queue[k-1]);
/* take the element at the k-1 index position */

System.out.println("Item deleted : " + item);
for(int i=k-1;i<pointer;i++)
/* move the other elements one position before, overwritting the deleted item */
queue[i] = queue[i+1];
pointer = pointer - 1;
/* reduce the pointer one location before */
return item;
}

public void displayQueue(){

System.out.println("Elements of queue : " );
for(int i=0;i<=pointer;i++)
System.out.println(queue[i]);
}

}

Sample Output

Add a comment
Know the answer?
Add Answer to:
You must use an array to hold the items in the queue. In other words, one...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Restrictions: You may not change any of the fields, nor the constructor, nor the insertAtFront method....

    Restrictions: You may not change any of the fields, nor the constructor, nor the insertAtFront method. As usual, you may not modify the method headers given to you in any way nor may you change the name of the class or the package. You must use recursion to solve the problems. Your code may not contain any loops. Functions that have loops will receive 0 points package hw8; import java.util.NoSuchElementException; public class MyList<Item> { private class MyListNode { public Item...

  • Hello, I've been working on this for a while and I can't figure the rest out....

    Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise {    /*    * Construct BST from preorder traversal    */    public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)    {                       if(start > end) return null;               Node<Integer> root = new Node<Integer>(arr[start],...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestIterator { private List<Integer> list; // See the Java List Interface documentation to understand what all the List methods do ... @Before public void setUp() throws Exception { list = new ArrayList<Integer>(); // TODO also try with a...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from...

    package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from Question 2.4.24. * It represents a priority queue of generic keys. *   * It supports the usual <em>insert</em> and <em>delete-the-maximum</em> * operations, along with methods for peeking at the maximum key, * testing if the priority queue is empty, and iterating through * the keys. * For additional documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 2.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne....

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

    JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT