Walk the round line without using the temporary line

Basically, I was given an implementation of CircularQueue, I need to implement a method called public boolean contains (E other) 'that should return true if the "other" parameter exists in my queue.

I was fine with it because it was an array, but then I saw this other condition eavesdropping on me.

Remember that you cannot move freely through all the elements of the queue. Only the front element is available at any time using the peek method. Your implementation contains and traverses with methods MUST NOT use any additional queue to temporarily store some of the elements of this queue.

Could Iterator be applicable to solve this problem?

Any help is appreciated.

Mjall

Decision:

Answer I came up with the rotate description method: The rotate (int n) method removes n items from the front of the queue and adds them to the back of the queue. Items are added to the back of the queue in the same order in which they are removed from the front of the queue. For example, if a queue is given q containing items \ A, B, C, D, E ", where item A is at the head of the queue, after calling the q.rotate (2) method, the contents of the queue will be \ C, D, E, A , B ";

   public boolean contains(E elem) { 

    while( this.isEmpty() != true){

      if(this.peek() == elem){return true;}
      else{rotate(1);}



   } 
   return false;

 }

      

+3


source to share


1 answer


An iterator would not be practical in this situation.



Since this is a circular queue, you can remember the first one you saw (not necessarily removing it entirely from the circular queue) and dequeue / enqueue elements until you find what you are looking for, or come to the first dequeued node.

+1


source







All Articles