Question:
Java Program - Lists help?
anonymous
2010-02-24 18:00:44 UTC
Can someone tell me whats wrong with this program?

public class List
{
private class Node
{
public int data;
public Node next;
public Node ( int d, Node n )
{
data = d;
next = n;
}
}

private Node head;

public List()
{
head = null;
}

private class TraverseReturn
{
public Node prev;
public Node curr;
public TraverseReturn ( Node _prev, Node _curr )
{
prev = _prev;
curr = _curr;
}
}

public TraverseReturn traverse ( int value )
{
Node prev = null;
Node curr = head;
while ( curr != null && curr.data < value )
{
prev = curr;
curr = curr.next;
}

return new TraverseReturn ( prev, curr );
}

public void insert ( int value )
{
TraverseReturn tr = traverse ( value );
Node addNode = new Node();

if ( tr.prev != null )
tr.prev.next = addNode;
else // this belongs at the beginning or head
head = addNode;
}

public String toString()
{
String rval = "";
Node curr = head;
while ( curr != null )
{
rval = rval + curr.data + "";
curr = curr.next;
}

return rval;
}

public boolean find ( int value )
{
TraverseReturn tr = traverse ( value );
return ( tr.curr != null && tr.curr.data == value );
}

public void remove ( int value )
{
TraverseReturn tr = traverse ( value );
if ( tr.curr != null && tr.curr.data == value )
{
if ( tr.prev != null )
{
tr.prev.next = tr.curr.next;
}
else
head = tr.curr.next;
}
}

public static void main ( String args[] )
{
int[] arr = { 1, 4, 8, 6 };
List l = new List();
for ( int x : arr )
{
System.out.println( "inserting: " + x );
l.insert( x );
}
System.out.printf( "List contains: \n", l );
}
}
Three answers:
vincentgl
2010-02-25 11:22:00 UTC
Next time, try asking an explicit question (like, "why do I get this compiler error - ?").



Your insert method was messed up. You referenced a Node default constructor that doesn't exist. Here, I've commented out your original lines and inserted corrected logic.



public void insert(int value) {

TraverseReturn tr = traverse(value);

// Node addNode = new Node();



if (tr.prev != null)

// tr.prev.next = addNode;

tr.prev.next = new Node(value, tr.curr);

else

// this belongs at the beginning or head

// head = addNode;

head = new Node(value, tr.curr);

}



I also fixed your main():

public static void main(String args[]) {

int[] arr = { 1, 4, 8, 6 };

List l = new List();

for (int x : arr) {

System.out.println("inserting: " + x);

l.insert(x);

}

// System.out.printf("List contains: \n", l);

System.out.println("List contains: ");

System.out.println(l);

}



Output is this:

inserting: 1

inserting: 4

inserting: 8

inserting: 6

List contains:

1468
?
2016-11-09 06:16:04 UTC
Like some suggested, you are able to grow to be a good application developer in any language. even however, i could recommend going in the direction of the languages like C++, C#, and Java that employers seek for.
anonymous
2010-02-24 18:04:36 UTC
It would help us out a lot if you gave us the errors you're getting.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...