Priority Queue

Having some issues

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1504 Views - Last Post: 22 July 2009 - 09:33 PM Rate Topic: -----

#1 humer015  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 09

Priority Queue

Posted 22 July 2009 - 02:24 PM

I am using priority queues, and I can put the objects in fine, however I can't pull them back out to see what the objects are. I am getting a NullPointerException each time I try.

//This should print the entire List
//N is my node class
		public String toString() {
		N current=head;
		String result="";
		
		while (current.getNext()!=null) {
			System.out.println("in while"); // To see where I am
			result="Data: " + current.getData() + " --- Priority: " + current.getPriority()+"\n";
			current=current.getNext();
		}
		return result;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Priority Queue

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1200
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: Priority Queue

Posted 22 July 2009 - 02:46 PM

I don't think the error is being caused by this code. It looks like you are having a problem getting the next in the queue.

Could you post the rest of the code? It may help to find the error location.
Was This Post Helpful? 0
  • +
  • -

#3 olatno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-July 09

Re: Priority Queue

Posted 22 July 2009 - 02:58 PM

I think you have not initialised your N class properly. When you used your current instance to call getNext() method, current is pointing null object, that is why your getData or getPriority contain no value.

Try to use N current = new N() there is still no guranttee it will work because i have not see your N class
Was This Post Helpful? 0
  • +
  • -

#4 humer015  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 09

Re: Priority Queue

Posted 22 July 2009 - 03:12 PM

This is how I push it into the list
public void push(Object obj, double priority) {
	N front = head;
	
		if (size == 0) {
			front = new N(obj, priority, null);
			rear = front;
		  }
		  else {
			rear.setNext(new N(obj, priority, null));
			rear = rear.getNext();
		 
		  }
		  size++;

	}






This is my N class
 package csci1902.lab4;

public class N {
	// constructors
	
	public N() {}

	public N(Object o, double priority, N link) {
		data = o;
		priorities = priority;
		next = link;
	}

	// selectors

	public Object getData() {
		return data;
	}

	public void setData(Object o) {
		data = o;
		
	}
	
	public void setPriority(double priority) {
		priorities=priority;
	}

	public N getNext() {
		return next;
	}

	public void setNext(N link) {
		next = link;
	}
	
	public double getPriority() {
		return priorities;
	}

	// instance variables

	private Object data;
	private N next;
	private double priorities;

}


This post has been edited by humer015: 22 July 2009 - 03:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Priority Queue

Posted 22 July 2009 - 04:36 PM

		while (current.getNext()!=null) {
			System.out.println("in while"); // To see where I am
			result="Data: " + current.getData() + " --- Priority: " + current.getPriority()+"\n";
			current=current.getNext();   <---------- this line shouldn't be there
		}
		return result;


when you'll hit the last one current will be null
then in your while you fetch getNext() on null :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Priority Queue

Posted 22 July 2009 - 04:41 PM

I know the Scanner class has one, haven't exactly attempted Nodes yet, never had a reason to yet; but couldn't you just use a while(current.hasNext()) ?? You could do that if you use a scanner but not sure about Nodes?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Priority Queue

Posted 22 July 2009 - 04:45 PM

View PostFuzzyness, on 22 Jul, 2009 - 03:41 PM, said:

I know the Scanner class has one, haven't exactly attempted Nodes yet, never had a reason to yet; but couldn't you just use a while(current.hasNext()) ?? You could do that if you use a scanner but not sure about Nodes?

Fuzzy have you read the post I wrote just before yours ?
PBL: 3
Fuzzy: 0
:D
Was This Post Helpful? 0
  • +
  • -

#8 olatno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-July 09

Re: Priority Queue

Posted 22 July 2009 - 04:51 PM

what i think you need to do here is

1. you call your set methods first and pass values to them for example setData(new String("one")) and setPriority(1)

2. call push method and you have something like this push(data, priorities)

you could repeat those steps many times i.e new String("two") with setPriority(2) and so on.

3. try pop your result and see what happen then
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Priority Queue

Posted 22 July 2009 - 05:14 PM

That should do the trick

	   while (current.getNext()!=null) {
			System.out.println("in while"); // To see where I am
			result="Data: " + current.getData() + " --- Priority: " + current.getPriority()+"\n";
 /////		   current=current.getNext();
		}
		return result;



but this not the best approach
you you have an array of "current" based on your number of priorities and scan them from the highest to lowest priority
the first one that returns a thing to do threat that one

unless this is what you are doing and current is pointing to the queue your are processing

This post has been edited by pbl: 22 July 2009 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 devraj0109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-July 09

Re: Priority Queue

Posted 22 July 2009 - 08:59 PM

you are assigning head to current and then you are entering while loop with condition that current.hasNext() != null so it appears that your list is not being populated properly because N current = head leads to head.hasNext() and if head is null that means NullPointerException

better do this:
N current = head
while ( current != null )
{
//do work here
current = current.getNext();
}
Was This Post Helpful? 0
  • +
  • -

#11 devraj0109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-July 09

Re: Priority Queue

Posted 22 July 2009 - 09:07 PM

@pbl - I apologize if I am wrong but the last piece of code you posted is an infinite loop because you are not updating current. also in the original code the problem would not be with while ( current.getNext() != null ) because this test will let the second last element of the list to pass through but the last non-null element will not pass because last element != null and (last element).geNext() == null
So only problem at the end is that the last element will never be printed
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Priority Queue

Posted 22 July 2009 - 09:11 PM

View Postdevraj0109, on 22 Jul, 2009 - 08:07 PM, said:

@pbl - I apologize if I am wrong but the last piece of code you posted is an infinite loop because you are not updating current. also in the original code the problem would not be with while ( current.getNext() != null ) because this test will let the second last element of the list to pass through but the last non-null element will not pass because last element != null and (last element).geNext() == null
So only problem at the end is that the last element will never be printed

Why do you mean I do not update current
This does the trick
while (current.getNext()!=null) {
Was This Post Helpful? 0
  • +
  • -

#13 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Priority Queue

Posted 22 July 2009 - 09:12 PM

From what I can tell .getNext() is going to set current equal to the next item. Since the getNext is in the test of the loop it will be called once every loop (read: not an infinite loop).

Before it was being call twice every loop. The null pointer exception was because the last element was test and came up as not null but the the getNext was called again. Since there was no next it came up null.

Edit: I need to type faster...

This post has been edited by syfran: 22 July 2009 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#14 devraj0109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-July 09

Re: Priority Queue

Posted 22 July 2009 - 09:18 PM

Like I said, I apologize. I read the method declared in the class N as
public N getNext() {
return next;
}
therefore I was thinking according to that, and that is why my piece of code say current = current.getNext(). But if getNext() method modifies the object it is called upon then yes it would not be an infinite loop.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Priority Queue

Posted 22 July 2009 - 09:21 PM

My apologies you are right
you need to find out a way to have a "current" that is actually your current

I NEVER update a loop conditional variable inside a while statement
REALLY BAD CODING practive

This post has been edited by pbl: 22 July 2009 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2