tomaszsvd
tomaszsvd

Reputation: 148

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]);
        }
    }
}

Upvotes: 0

Views: 414

Answers (3)

ankit249
ankit249

Reputation: 452



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]


Upvotes: 1

Chris Gong
Chris Gong

Reputation: 8229

The problem in your addQueue method is that you are incremementing count before checking if it equals zero. Therefore, the condition count == 7 will evaluate to true after adding the 7th item, activating the takeQueue method. So "Item removed: a" will be printed out. The simple fix is to increment count after checking if it equals 7.

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

Upvotes: 0

ChrisCantrell
ChrisCantrell

Reputation: 3853

I ran your code by adding the following main. It DOES print "Item removed:a". Paste in your "main" so we can see what you are doing.

public static void main(String [] args) {             
         MyQueue q = new MyQueue();
         q.addqueue("a");
         q.addqueue("b");
         q.addqueue("c");
         q.addqueue("d");
         q.addqueue("e");
         q.addqueue("f");
         q.addqueue("g");
         q.addqueue("h");
     }

enter image description here

Upvotes: 0

Related Questions