This sounds like sorting an array rather than a queue. As you said, queues work on the FIFO format, so you can't sort a queue since you will lose track of which one should be removed next. Are you sure this is what your task requires?
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
Well it depends she could be using a Priority Queue, in which members are organized in the ascending or descending order.
Also priority queue implementations may vary, some may do the selection of element to be removed at deletion time while others may do the sorting at insertion time so that elements in queue are always in a sorted order (based on priority which is what the O.P. wants here) for deletion.
Now to do the above, you will have to traverse your queue until you find an element that is greater than the element you wish to insert and then fit in your element there. Also let us see what you have tried.
Also just my two cents, according to me a Linked List based implementation would be lot easier to implement than an Array based implementation.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
You will need to maintain a reference to the previous element also in the above code. Because as per your requirement you want to insert the new element before the element "temp" is holding a reference of when you are traversing the queue.
Now let us take analyse your code one line at a time :-
QueueNode temp = front;
This is fine, you are declaring the temporary reference to your start of Queue.
while(temp != null){
if (temp.info > x)
Now your while condition states that you will should go through to the end of the queue. However there is a problem if your "if" condition, Consider you list contains the following elements :-
2 3 4 9 10
And you wish to insert the value "7",
Because of your "if" condition temp will enter the "if" block only when it is pointing to "9". And since this is a singly linked list you cannot traverse backwards. So you cannot insert "7" in the correct position in the list. For that you will need to also maintain a reference to node in your queue preceding the node to which "temp" is currently referring to.
temp.info = new queueNode(x);
Now over here this statement at least appears to be incorrect. Although I do not know your structure for class "queueNode", I am guessing "info" should be of type "int". Also you are overwriting directly the value present at the current location, You should instead create a new node, and assign it to another temporary variable say "temp2", and then in order to maintain the "link" in your linked list, the "link" field of this new node should be assigned to current value in "temp". And the "link" field of the node preceding "temp" should be made to point to the new node. So your new element would be linked into your queue before "temp" which contains an element larger than it.
Also you will need to put a "break" in your "if" block else the new value will be inserted after every element greater than it. For example your list queue would become:-
2 3 4 7 9 7 10 ...
(Where 7 is the element being inserted in queue 2 3 4 9 ...)
EDIT:
Currently citing your previous code as I had reply already typed in earlier but just forgot to submit it.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154