Linked List : Adding Recursively

I have a linked list, which can extract data from a random access file. But i need it to be edited so that the actual list will change. But when i try to add something in the middle of the list, everything past the list dissapears. I need to replace one deafult entry with the users entry.

The code:

Linked List Class

class LinkedListNode{

private String day;

privateint abs;

private LinkedListNode next;

public LinkedListNode(){

this(" ", 0);

}

public LinkedListNode(String s1,int s2){

day = s1;

abs = s2;

next =null;

}// LinkedListNode()

publicvoid setData(String s1,int s2){

day = s1;

abs = s2;

}// setData()

public String getDay(){

return day;

}// getday()

public String getData(){

return day +"\t" + abs;

}// getData()

public String toString(){

return day +"\t" + abs;

}// toString()

publicvoid setNext(LinkedListNode nextPtr){

next = nextPtr;

}// setNext()

public LinkedListNode getNext(){

return next;

}// getNext()

}// LinkedListNode class

User entry:

int point = inputInt("Select insertion point: ");

node = head;

addRecursive(point,"just a test");

Method to insert data:

publicvoid addRecursive(int i, String o){

int count = 0;

if(i == 1){

node.setNext(new LinkedListNode(o.trim(),5) );

node.getNext();

}

elseif (node.getNext() !=null){

node = node.getNext();

addRecursive(i-1, o);

}

else output("End of list reached");

}

Thanks in advance

[3860 byte] By [desijockeya] at [2007-11-26 15:10:32]
# 1

If you're losing all the nodes after the newly inserted one, it sounds like you're not attaching the end of the list to the new node properly.

Say you have a list with 2 nodes, A and C. A's next is pointing to C currently. You insert new node B in between A and C. You set A's next to point to B, then set B's next to point to C. Make sure you're doing the part where you point b's next to C.

hunter9000a at 2007-7-8 9:01:23 > top of Java-index,Java Essentials,Java Programming...
# 2

public void addRecursive(int i, String o) {

int count = 0; // used for what?

if(i == 1) {

node.setNext(new LinkedListNode(o.trim(),5) ); // no checking whether this node was already present

node.getNext(); // used for what?

}

else if (node.getNext() != null) {

node = node.getNext(); // where's "node" declared?

addRecursive(i-1, o);

}

else output("End of list reached");

}

quittea at 2007-7-8 9:01:23 > top of Java-index,Java Essentials,Java Programming...
# 3

public void addRecursive(int i, String o) {

int count = 0; // not used for anything, previously used for flagging, redundant now

if(i == 1) {

node.setNext(new LinkedListNode(o.trim(),5) ); // no checking whether this node was already present

// there will always be a node there, it is based on the days of the year, so there will always be a day present, the integer assigned is what i want to change

node.getNext(); // used for what? - not quite sure...

}

else if (node.getNext() != null) {

node = node.getNext(); // where's "node" declared? - outside the method, as a static

addRecursive(i-1, o);

}

else output("End of list reached");

}

desijockeya at 2007-7-8 9:01:23 > top of Java-index,Java Essentials,Java Programming...
# 4

public void addRecursive(int i, String o) {

int count = 0; // remove it

if(i == 1) {

node.setNext(new LinkedListNode(o.trim(),5) );

// there will always be a node there, it is based on the days of the year, so there will always be a day present, the integer assigned is what i want to change

// Well, you replace the node that existed as a "next node" before without

// passing its "tail" of other nodes to the newly created one. I'm not sure

// whether you want to replace or insert a node

node.getNext(); // remove it

}

else if (node.getNext() != null) {

node = node.getNext();

// where's "node" declared? - outside the method, as a static

// Static? Then it'll work only for one single linked list in your program; make it non-static

addRecursive(i-1, o); // I guess this should actually be getNext().addRecursive(...), then there's no need for a "node" field at all

}

else output("End of list reached");

}

quittea at 2007-7-8 9:01:23 > top of Java-index,Java Essentials,Java Programming...