Bob
Bob

Reputation: 1396

C++: Moving an element from Front of queue To Back

I'm attempting to write a function called move_to_rear that moves the element at the front of the queue to the back of the queue, then making the element second in line the front element. I was able to do this using a couple different methods, but my classmate told me he did it by using just push, front, and pop.

How did he accomplish such a thing using just those three functions?

Upvotes: 0

Views: 4693

Answers (1)

Gershon Papi
Gershon Papi

Reputation: 5106

Maybe I'm missing something but since its a queue and not a stack the answer is simple:

const Item first = q.front(); //getting the first
q.pop(); //removing him
q.push(first); //adding him back to the queue (which will be in the rear)

Just to make sure you understand:
Queue pops the first item in the queue, and pushes items into the back. FIFO = First In First Out. Stack pops the last item in the list, and pushes items into the back too. LIFO = Last In First Out. value of first must be const because front return reference and may be do not push when scopes are closed.

Upvotes: 3

Related Questions