Adding ListNodea

i want to add nodes

WHat this method does is that it adds a value to the end of the list.

The problem i am facing is that, when the is null, the objects are not added. Other wise it's working just fine

example:

curent List : list is empty

what number do you want to add ?

3

new list: List is empty

here is what i have done:

publicstaticvoid add(ListNode head, Object value)

{

if (head ==null)// this is not working

head =new ListNode (value, head);

ListNode runner = head;

while (runner.getNext() !=null)

runner = runne.getNext();

runner.setNext(new ListNode(value,null));

}

[1109 byte] By [JollyJoea] at [2007-10-2 5:05:13]
# 1
> The problem i am facing is that, when the is null,should be when the list is null
JollyJoea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...
# 2

these modifications would seem to make sense to me BUT,

I can't see all of your code AND

if you create a new head, then you should put your head somewhere,

otherwise, how will you find that head the next time you need to use it?

public static void add(ListNode head, Object value) {

if (head == null)

head = new ListNode(value, null);

else{

ListNode runner = head;

while (runner.getNext() != null)

runner = runner.getNext();

runner.setNext(new ListNode(value, null));

}

}

Arbiea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...
# 3

> I can't see all of your code AND

i c.. so should i send my whole code? i

> if you create a new head, then you should put your

> head somewhere,

> otherwise, how will you find that head the next time

> you need to use it?

LOL, sorry, but I don't understand what you just said.

> > public static void add(ListNode head, Object

> ject value) {

> if (head == null)

> head = new ListNode(value, null);

> else{

> ListNode runner = head;

> while (runner.getNext() != null)

>runner = runner.getNext();

> runner.setNext(new ListNode(value,

> tNode(value, null));

> }

>}

>

in this code, why put null? wouldn't head be null? and also it doesnt work it says nullpointerException.

JollyJoea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...
# 4
:(Arrgh this is frustrating lolAny suggestions ?
JollyJoea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...
# 5

> > if you create a new head, then you should put your

> > head somewhere,

> > otherwise, how will you find that head the next

> time

> > you need to use it?

>

> LOL, sorry, but I don't understand what you just

> said.

>

I was assuming this was an implementation of a Linked List.

If that's the case, then you always need to hold a reference to the head.

if you create a head and the only reference you have is a local variable, then it will go out of scope and you won't have it any more.

> > > > public static void add(ListNode head, Object

> > ject value) {

> > if (head == null)

> > head = new ListNode(value, null);

> > else{

> > ListNode runner = head;

> > while (runner.getNext() != null)

> >runner = runner.getNext();

> > runner.setNext(new ListNode(value,

> > tNode(value, null));

> > }

> >}

> >

>

> in this code, why put null? wouldn't head be null?

> and also it doesnt work it says nullpointerException.

I looked at one of your other posts and saw some more of your code.

It's not an implementation that i would use but i thought the second parameter corresponded to the "next" element in the list which i thought would be null.

I use a much different approach. I always have a node element for a head that is non null, even if there are no elements in the list. when i want to add an element, i don't have to check to see if the head is null.

i just use

Node temp = head;

while(temp.next()!=null)

temp = temp.next();

temp.setNext(new Node(value));

and when i want to get something

Node temp = head.next();//head.next() doesn't contain a value

while(temp!=null){

//get value from temp

//do something

//

temp = temp.next();

}

anyway, that's sort of the idea i use when writing C code.

never done a list in java( i use java.util collections framework)

I may have made a mistake cause that came from the top of my head

but you can get the idea.

Arbiea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...
# 6
Something like that thanks,Actually my teacher made a mistake. He said to change from a "void" method to a method that returns a ListNode. But I see where you are getting at. Thanks a lot.
JollyJoea at 2007-7-16 1:08:38 > top of Java-index,Java Essentials,New To Java...