JAVA Having trouble with remove import javautilIterator Gr
JAVA
Having trouble with remove()
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, group 2, group 3, etc. and
* the passengers line up at the end of the queue of their groups
* in a FIFO fashion; and, when removing from the GroupsQueue,
* i.e., when the passengers are boarding the plane, they get
* removed first by their group priorities and then by their FIFO
* ordering within that particular priority group.
*
* Your homework is to implement the below GroupsQueue data
* structure, so that it functions in the way described above.
*
* You may use the provided Queue class for implementing the
* GroupsQueue class.
*/
public class GroupsQueue<Item> implements Iterable<Item> {
private Node<Item> first;
private Node<Item> last;
private int n;
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
public void Queue() {
first = null;
last = null;
n = 0;
}
// TODO : implement the data structure (20 points)
/**
* Initializes an empty GroupsQueue with g groups
* @return
*/
public GroupsQueue(int g) {
// TODO : implement the constructor (20 points)
}
/**
* Is this GroupsQueue empty?
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns the number of items in the GroupsQueue.
*/
public int size() {
return n; // TODO (20 points)
}
/**
* Adds the item to this GroupsQueue with group id = gId.
*/
public void add(Item item, int gId) {
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
}
// TODO (20 points)
/**
* Removes and returns the item with the lowest group id
*/
public Item remove() {
} // TODO (20 points)
Solution
Here is the code for you:
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, group 2, group 3, etc. and
* the passengers line up at the end of the queue of their groups
* in a FIFO fashion; and, when removing from the GroupsQueue,
* i.e., when the passengers are boarding the plane, they get
* removed first by their group priorities and then by their FIFO
* ordering within that particular priority group.
*
* Your homework is to implement the below GroupsQueue data
* structure, so that it functions in the way described above.
*
* You may use the provided Queue class for implementing the
* GroupsQueue class.
*/
public class GroupsQueue<Item> implements Iterable<Item> {
private Node<Item> first;
private Node<Item> last;
private int n;
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
public void Queue() {
first = null;
last = null;
n = 0;
}
// TODO : implement the data structure (20 points)
/**
* Initializes an empty GroupsQueue with g groups
* @return
*/
public GroupsQueue(int g) {
// TODO : implement the constructor (20 points)
}
/**
* Is this GroupsQueue empty?
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns the number of items in the GroupsQueue.
*/
public int size() {
return n; // TODO (20 points)
}
/**
* Adds the item to this GroupsQueue with group id = gId.
*/
public void add(Item item, int gId) {
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
}
// TODO (20 points)
/**
* Removes and returns the item with the lowest group id
*/
public Item remove() {
if(first == last && first == null) //If there are no elements.
return null;
if(first == last) //If there is only one element.
{
Item temp = first.item;
first = last = null;
return temp;
}
Node<Item> temp = first;
Item lowestNodeVal = first.item;
while(temp != last)
{
if(temp.item < lowestNodeVal)
lowestNodeVal = temp.item;
}
if(first.item == lowestNodeVal)
first = first.next;
else
{
temp = first;
while(temp.next.item != lowestNodeVal)
temp = temp.next;
temp.next = temp.next.next;
if(last.item == lowestNodeVal)
last = temp;
}
return lowestNodeVal;
} // TODO (20 points)
}



