queue question: part 2.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

queue question: part 2.

 
0
  #1
Jan 21st, 2009
In a queue, where elements in integers are added, how do i do this:

Inside the queue:

1 2 3 5 6 8

when I want to add a number, say 4, the output should be like:

1 2 3 4 5 6 8

in which it inserts in between 2 and 5. Queues are FIFO but somehow this doesn't make sense. I think this has something to do with priority. the example above is just an ordinary queue, not to confused with a priority queue..
Can someone explain and help me?
thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: queue question: part 2.

 
0
  #2
Jan 22nd, 2009
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?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: queue question: part 2.

 
0
  #3
Jan 22nd, 2009
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.

<EDIT>
Also just my two cents, according to me a Linked List based implementation would be lot easier to implement than an Array based implementation.
Last edited by stephen84s; Jan 22nd, 2009 at 12:29 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: queue question: part 2.

 
0
  #4
Jan 22nd, 2009
Originally Posted by stephen84s View Post
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.

<EDIT>
Also just my two cents, according to me a Linked List based implementation would be lot easier to implement than an Array based implementation.
here is what i had so far:

  1. public int compareTo(int x){
  2. QueueNode temp = front;
  3. while(temp != null){
  4. if (temp.info > x)
  5. temp.info = new queueNode(x);
  6. temp = temp.link
  7. }
  8. }

is this ryt?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: queue question: part 2.

 
0
  #5
Jan 22nd, 2009
ok i made revisions on the code i just posted:

  1. public void compareTo(int item){
  2. QueueNode temp = front;
  3. while(temp != null){
  4. if (temp.info > item)
  5. temp.info = item;
  6. temp = temp.link;
  7. }
  8. }

And this is my enQueue, or my add to queue method:
  1. public void enQueue(int item) {
  2. if(empty()){
  3. rear = new QueueNode(item);
  4. front = rear;
  5. }else{
  6. compareTo(item);
  7. rear.link = new QueueNode(item);
  8. rear = rear.link;
  9.  
  10. }
  11. }

It seem that when i add, like, 4 to the qeue

1 2 5

it does this:

1 2 4 4

whereas it should be

1 2 4 5

the element 5 is gone and is know replaced by two 4's.. i believed that 5 is deleted.. can any help me with this?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: queue question: part 2.

 
0
  #6
Jan 22nd, 2009
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 :-

  1. QueueNode temp = front;

This is fine, you are declaring the temporary reference to your start of Queue.
  1. while(temp != null){
  2. 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.

  1. 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.
Last edited by stephen84s; Jan 22nd, 2009 at 8:50 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: queue question: part 2.

 
0
  #7
Jan 23rd, 2009
Here's my idea, i have 2 queues, in which i kepp track of them by:
1st queue:
front and rear;

2nd queue:
front2 and rear.

before i had read your reply stephen84s, i decided to have two queues. When an integer is added, it goes into the 1st queue. when another " int" is added to queue, what happens is the element in queue1 is tranfered to queue2. Elements in queue2 would them be, one by one, compared to the incoming number which is 'held at bay', which would traverse from the head or front of the queue. If item at queue2 is less than item at bay, the first item at queue2 would be transferred back to queue1, this would go on until when an item at queue2 is greater than item at bay, item at bay is then inserted(dequeued from queue2 and enqueued in queue1) at queue1, them the item at queue2 is next to be inserted at at queue1. Whew, i don't know if that makes any sens.

And below was the code i made:

  1. public void enQueue(int item) {
  2. if(empty()){
  3. rear = new QueueNode(item);
  4. front = rear;
  5. }else{
  6. while (front != null){
  7. int item2 = deQueue();
  8. rear2 = new QueueNode(item2);
  9. front2 = rear2;
  10. }
  11. compareTo(item);
  12. rear.link = new QueueNode(item);
  13. rear = rear.link;
  14. //rear.link = new QueueNode(item);
  15. //rear = rear.link;
  16.  
  17. }
  18. }

  1. public void compareTo(int item){
  2. QueueNode temp = front2;
  3. int x = 0;
  4. while (temp != null){
  5. if (temp.info < item){
  6. x = temp.info;
  7. rear = new QueueNode(x);
  8. front = rear;
  9. }else if(temp.info > item){
  10. int item2 = temp.info;
  11. rear = new QueueNode(item2);
  12. front = rear;
  13. rear.link = new QueueNode(item);
  14. rear = rear.link;
  15. }else{
  16. rear.link = new QueueNode(item);
  17. rear = rear.link;
  18. }
  19. }
  20. }

But i will consider what you suggested. Thanks .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC