tomaszsvd tomaszsvd - 1 year ago 38
Java Question

Using an array like a queue

I have a problem with the my Queue class. The queue is based on array. When the queue items reach the end of the array they should be added to the start of the array. If the queue becomes full the oldest queue item should be removed and displayed. I have problem with displaying the removed oldest item when the 8th element is inserted.

When the user selects to display the names of the first 3 customers code should remove them from the queue one by one (first in first out) and display them as they are removed.

Example input and output:


Queue Input is:
a
b
c
d
e
f
g
h



Queue Output is:
h
b
c
d
e
f
g


item removed was: h



Expected output should be:


h
b
c
d
e
f
g
item removed was: a



so it moves the last inserted item in to the front of array however how do i display "item removed was: a"
the code i have is:

public class MyQueue {

private final static int CAPACITY = 7;
static String qitems[] = new String[CAPACITY];
private static int front = 0, end = 0, count=0;

public void addqueue(String name) {
qitems[end] = name;
count++;
if(count==7) {
takequeue();
System.out.println("Queue is full");
}
end = (end + 1) % 7;
}

public void takequeue() {
System.out.println("Item removed:"+qitems[front]);
front = (front +1) % 7;
}

public void displayNames() {
System.out.println(" 3 names entered are: ");
for (int x = 0; x < 3; x++) {
System.out.println(qitems[x]);
}
}
}

Answer Source


public class QWithArray {

    String[] qItems;
    int front;
    int end;
    int current;

    QWithArray(int CAPACITY) {
        qItems = new String[CAPACITY];
        current = 0;
        front = -1;
        end = -1;
    }

    public void addqueue(String element) {
        if (current == qItems.length) {
            System.out.println("Item removed was: " + qItems[(front + 1) % qItems.length]);
        }

        front = (front + 1) % qItems.length;
        qItems[front] = element;
        current++;

        if (end == -1) {
            end = front;
        }
    }

    public String takequeue() {
        if (current == 0) {
            System.out.println("Queue is empty; can't remove.");
            return null;
        }

        String result = qItems[end];
        qItems[end] = null;

        end = (end + 1) % qItems.length;
        current--;

        if (current == 0) {
            front = -1;
            end = -1;
        }
        return result;
    }

    public static void main(String[] args) {
        QWithArray q = new QWithArray(7);
        q.addqueue("a");
        q.addqueue("b");
        q.addqueue("c");
        q.addqueue("d");
        q.addqueue("e");
        q.addqueue("f");
        q.addqueue("g");
        System.out.println(Arrays.toString(q.qItems));

        q.addqueue("h");
        System.out.println(Arrays.toString(q.qItems));
    }
}

Output would be:


[a, b, c, d, e, f, g]

Item removed was: a

[h, b, c, d, e, f, g]


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download