0

So we have to use the supplied methods and variables without adding any and no matter what I try I can not get it working. I am extremely new to queues so might be missing something small. Here is my code:

/**
  * Int Queue
  */
public class Queue{

/** Max num elements*/
private int numElements;

/** Array to save elements **/
private int elements[];

/** Indice to last element */
private int last;

/** Constructor to init the state object */
Queue(int numElements){
    this.numElements = numElements;
    this.elements = new int[numElements];
    this.last = -1;

}

/** Is empty the queue? */
public boolean isEmpty(){
    return (last == -1);
}

/** Is full the queue */
public boolean isFull(){
    return (numElements() == this.numElements);
}

/** Insert an element in the queue */
public void enqueue(int element) {

    last++; 

    elements[last]= element;

}

/** Extract the element in the queue.
 *  There isn't control error */
public int dequeue() {

    int elem = elements[0];

    for (int x = 0; x < last; x++){
        elements[x] = elements [x + 1];
    }

    last --;

    return elem;
}

/** Returns the number of elements in the queue */
public int numElements(){

    int number = 0;

    for (int x = 0; x < this.last; x++) {
        number++;
    }

    return number;
}

/** Print the elements in the queue*/
public void print(){

    System.out.println("\nElements: ");

   for(int b:elements){    
       System.out.println(b);    
   }    
}



  public static void main(String args[]){
     // test the class
     System.out.println("Test the queue class");
     Queue que = new Queue(4);
     que.enqueue(1);
     que.enqueue(4);
     que.enqueue(5);
     que.print();
     que.dequeue();
     que.dequeue();
     que.print();
   } // main

} // Queue

and it outputs:

Test the queue class

Elements: 
0
1
4
5

Elements: 
4
5
5
5

I am not ale to get it to print the correct options. First it should print 1 4 5 (not the zero) and then it should print 1 and nothing else. All help is apreciated thank you!

1
  • The second time it should print 5, otherwise it would be a stack Commented Apr 19, 2020 at 8:46

2 Answers 2

0

I ran your code and I got these results:

Elements: 
1
4
5
0

Elements: 
5
5
5
0

Which make sense because your internal array will always have the same number of values, as it has a static size (in your case 4).
To fix this, you could rewrite your print method as:

public void print(){

    System.out.println("\nElements: ");

   for(int i = 0; i < numElements(); i++){    
       System.out.println(elements[i]);    
   }    
}

Also, change the numElements method:

public int numElements(){
    return last + 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem just lies with how you are printing out your queue. Try this in place of your print:

public void print(){

        System.out.println("\nElements: ");

        for(int c = 0; c < last+1; c++){    
            System.out.println(elements[c]);    
        }    
    }

Explination

Because you just shift everything forward 1 space when something is dequeued, the data at the end isnt being deleted or "moved forward", its just being copied forward. With your original print, it looks at all of the elements in the entire list, and not just what we are deeming in the queue (anything less than last).

Comments

Your Answer

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