rear indicates the last element. You want to place temp after the current rear , and then move rear to point to the recently placed last element. So, if we wanted to queue 4 in the queue (1, 2, 3) , we want:
1 -> 2 -> 3 -> 4 | | front rear
Your solution allows temp cut in front of the current rear , and then move the rear to the cutout position. It doesn't even cut properly, because the element in front of the rear still points to the original rear . rear no longer points to the last element, and your queue will be in an inconsistent state.
1 -> 2 -> 3 | 4 -^ | | front rear
source share