JAVA need help with public Iterator iterator import javautil
JAVA
need help with public Iterator<Item> iterator()
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) {
first = new Node<>();
last = first;
for(int i=1; i<=19; i++){
last.next = new Node<>();
last = last.next;
}
}
// 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() {
Node temp = first;
Node prev = first;
Item minGid;
if (! isEmpty())
{
minGid = first.item;
}
else
{
return null;
}
while(temp!= null)
{
prev = temp;
temp = temp.next;
if (temp.item == minGid)
{
minGid = (Item) temp.item;
}
}
if (minGid == first.item)
{
first = first.next;
}
else
{
prev.next = prev.next.next;
}
n--;
return minGid;
}// TODO (20 points)
/**
* Returns an iterator that iterates over the items in this GroupsQueue
* in group id order (lowest id first) and in FIFO order in each group.
*/
public Iterator<Item> iterator() {
// TODO
// BONUS (20 points)
return null;
}
}
Solution
Implementation given here seems to have many logical errors.
Here is the code given for GroupQueue:
package groupqueue;
public class GroupQueue{
/**
* n is number of queues maintained. and queues is array of all queues maintained by GroupQueue.
* here there are n groups . so maintain n queues and each queue represent one group.
*/
public int n;
public Queue[] queues;
public GroupQueue(int n){
this.n =n;
this.queues = new Queue[n]; //initialize array of queues with n number of groups
}
//is this GroupQueue empty ?
public boolean isEmpty() {
for(int i=0; i<this.n; i++){
if(this.queues[i].first != this.queues[i].last ){ // when first!= last there is at least one element in queue
return false; //return false when there is a queue which has at least one element.
}
}
return true;
}
public int size() { // returns number of items in group queue.
int itemCount =0;
if(this.isEmpty()){
return 0;
}
else{
Node<Item> tempNode ;
for(int i=0; i<this.n; i++){
tempNode= this.queues[i].first;
itemCount++;
while(tempNode != queues[i].last){
itemCount++;
tempNode= tempNode.next;
}
}
}
return itemCount;
}
/**
* Adds the item to this GroupsQueue with group id = gId.
*/
public void add(Item item, int gId) {
Node<Item> newNode= new Node(item);
if(queues[gId].first== null){ //when queue is empty first and last point to same new node.
queues[gId].first= newNode;
queues[gId].last= newNode;
}
else{
queues[gId].last.next= newNode;
queues[gId].last= newNode;
}
}
public Item remove(){
if(isEmpty()) return null;
int i=0;
// find for a queue which has at leats one element.
while(queues[i].first== null){
i++;
}
// now ith queue will have at least one element
Node<Item> temp =queues[i].first; // return the first element from ith queue
queues[i].first= queues[i].first.next; //First variable will point to second element of queue now.
return temp.item;
}
// method to iterate through all elements of GroupQueue and prints them in ascending order of group id.
// here for a one group , elements will be printed in FIFO manner of queue of that group .
// here no need to retun anything , as we are printing all elements in function itself.
public void iterator() {
Node<Item> temp;
for(int i=0 ; i<n; i++){
if(queues[i].first == null){
continue;
}
temp= queues[i].first;
System.out.println(temp.item);
while(temp != queues[i].last){
temp= temp.next;
System.out.println(temp.item);
}
}
return;
}
public static class Node<Item>{
public Item item;
public Node<Item> next;
public Node(Item item){
this.item= item ;
this.next= null;
}
}
public static class Item{
public int Itemdata;
public Item(int d){
this.Itemdata=d;
}
}
public static class Queue{
public Node<Item> first;
public Node<Item> last;
public Queue(){
first = last= null; //initially first and last pointer are null for each queue.
}
}
}




