0

im very confused as to why my queue is not working, i believe that there is a problem in the enqueue and dequeue methods. but im not sure, i am supposed to Implement the class with the initial array size set to 8. The array size will be doubled once the number of the elements exceeds the size. After an element is removed from the beginning of the array, you need to shift all elements in the array one position the left. Write a test program that adds 20 numbers from 1 to 20 into the queue and removes these numbers and displays them. here is my code

    public class Queue {
    private int[] elements;
    private int size;
    private int first;
    private int last;
    public static final int DEFAULT_CAPACITY = 8;

    public Queue(){
        this (DEFAULT_CAPACITY);
    }
    public Queue (int capacity){
        elements = new int[capacity];
        first = 0;
        last = 0;
        size = 8;
    }
    public void Enqueue(int v){ //fills queue and lengthens if necessary
        if (last>=size){
            int[] temp = new int[elements.length*2];
        System.arraycopy(elements, 0, temp, 0, elements.length);
            elements = temp;
        }
        elements[last]=v;
        last++;

    }
    public int Dequeue(){    
         int output = elements[first];
         System.out.print(output + " ");
        while(last != 0){
            for(int i = 0; i<last;i++){
                elements[i]= elements[i-1];
            }
            last--;
        }
        return output ;
    }
    public boolean empty(){ // tests for empty queue


        return last==first;

    }
    public int getSize(){
        size=last;
        return size;
    }
    }

and here is the tester class.

    public class QueueTester {
    public static void main(String[] args){
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        q.Enqueue(5);
        q.Enqueue(6);
        q.Enqueue(7);
        q.Enqueue(8);
        q.Enqueue(9);
        q.Enqueue(10);
        q.Enqueue(11);
        q.Enqueue(12);
        q.Enqueue(13);
        q.Enqueue(14);
        q.Enqueue(15);
        q.Enqueue(16);
        q.Enqueue(17);
        q.Enqueue(18);
        q.Enqueue(19);
        q.Enqueue(20);
        while (q.empty()){
            q.Dequeue();
1
  • Is it a requirement that you insert elements at the start rather than the end? It'd be a lot more efficient to queue and dequeue at the end of the array. Commented Sep 13, 2016 at 2:09

2 Answers 2

1
while(last != 0){
    for(int i = 0; i<last;i++){
        elements[i]= elements[i-1];
    }
    last--;
}

Remove the while loop. If you're trying to make sure it's not dequeueing an empty queue have an if condition check to ensure that the size is > 0.

 public int Dequeue(){
         if (getSize() == 0) {
             // throw an error or something
         }    
         int output = elements[first];
         System.out.print(output + " ");
         for(int i = 0; i<last;i++){
             elements[i]= elements[i-1];
         }
         last--;
         return output ;
    }

Additionally you need to print the output in your tester class, and I assume you want to dequeue while the queue is NOT empty:

 while (!q.empty()){
            System.out.println(q.Dequeue());
Sign up to request clarification or add additional context in comments.

1 Comment

this still does not fix the issue, when calling q.dequeue() in the tester it does not print output
0

Mmm, think the algorithm you are using is not correct try referring this http://projectyogisha.com/implementing-queues/ , its in C though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.