Linked List problem, please help.
I am trying to make a method which receives the first list node of two different linked list's of ints and the method is supposed to insert the second list right before the smallest int of the first list. Here is what i have but it is very confusing and I do not know how to fix it. The other methods of the program are working fine.
Code Java:
public static ListNode insertBeforeSmallest (ListNode headMain, ListNode headInsert)
{
ListNode start = headMain;
ListNode prevNodeMain = headMain;
ListNode prevNodeMain2 = headMain;
ListNode nodePtrMain = headMain.getNext ();
ListNode nodePtrMain2 = headMain.getNext ();
ListNode nodePtrInsert = headInsert;
int length = Length (prevNodeMain);
String lowestObj = "";
for (int j = 0 ; j < length ; j++)
{
if (j == 0)
{
lowestObj = ((String)prevNodeMain.getValue ());
prevNodeMain = nodePtrMain;
nodePtrMain = nodePtrMain.getNext ();
}
else if (((String)prevNodeMain.getValue ()).compareTo (nodePtrMain.getValue ()) < 0)
{
lowestObj = (String)nodePtrMain.getValue ();
prevNodeMain = nodePtrMain;
nodePtrMain = nodePtrMain.getNext ();
}
else
{
prevNodeMain = nodePtrMain;
nodePtrMain = nodePtrMain.getNext ();
}
}
while (nodePtrMain2 != null)
{
if(prevNodeMain2.getNext ().getValue ().equals (lowestObj))
{
prevNodeMain2.getNext ().setNext (nodePtrInsert);
for (int i = 0 ; i < (Length (nodePtrInsert) - 1) ; i++)
{
nodePtrInsert.setNext (nodePtrInsert.getNext ());
}
nodePtrInsert.setNext (nodePtrMain2);
return start;
}
else
{
prevNodeMain2 = nodePtrMain2;
nodePtrMain2 = nodePtrMain2.getNext ();
}
}
return null;
}
Re: Linked List problem, please help.
Hey Axeander,
Just a small thing, please use
while posting your java code.
That would make the code more readable for us.
We appreciate your co-operation. :)
Goldest
Re: Linked List problem, please help.
Ok thanks, but I'm cant find how to use that. Is it build into the post box, or do I need to get it from somewhere else?
Re: Linked List problem, please help.
Quote:
Originally Posted by
Axeander
Ok thanks, but I'm cant find how to use that. Is it build into the post box, or do I need to get it from somewhere else?
Hi Axeander, you can get your code into the box by editing your post above and flanking the source code with either the [highlight=java][/highlight] or [code][/code], where the code goes between the bracketed tags.
Re: Linked List problem, please help.
Ok thanks, but do you have any suggestions about the code its self? I really need some help on it.
Re: Linked List problem, please help.
ListNode<Integer> start as your ListNode stores Integers.
Re: Linked List problem, please help.
int length = Length (prevNodeMain);
I don't see what this is for.
Usually you look at the size of the entire list, not a just a single Node.
Unless Length is another method in the class that you're not showing because you've already got it figured out.
Re: Linked List problem, please help.
(String)prevNodeMain.getValue ()
If you're using Integers,
prevNodeMain.getValue().toString() would be better.
I don't think the way you have it now will work.
Re: Linked List problem, please help.
Also, the ListNodes you're passing as parameters should have their data types. I think that you said they were both Integer.
Code java:
public static ListNode<Integer> insertBeforeSmallest (ListNode<Integer> headMain, ListNode<Integer> headInsert)
Re: Linked List problem, please help.
Yes I already have the method Length on the rest of the program which is working but for some reason when i run the program using this code,
Code java:
public static void main (String[] args)
{
ListNode startList, startInsert;
System.out.println ("Entering the original list...");
startList = getLinkedList ();
System.out.println ("Entering the list to be inserted...");
startInsert = getLinkedList ();
startList = insertBeforeSmallest (startList, startInsert);
// prints elements of a linked list
ListNode tempNode = startList;
while (tempNode != null)
{
System.out.println (tempNode.getValue ().toString ());
tempNode = tempNode.getNext ();
}
} // main method
public static ListNode getLinkedList ()
{ //gets linked list
ListNode startNode, node, lastNode;
System.out.println ("Type in a name,'none' to quit..");
String name = Stdin.readLine ();
if (name.equals ("none"))
startNode = null;
else
{
startNode = new ListNode (name, null);
lastNode = startNode;
do
{
System.out.println ("Type in a name, 'none' to quit.");
name = Stdin.readLine ();
if (!name.equals ("none"))
{
node = new ListNode (name, null);
lastNode.setNext (node);
lastNode = node;
}
}
while (!name.equals ("none"));
} //else
return startNode;
}
I get stuck in the while loop "while(tempNode != null)" and keeps printing the same 2 number over and over.
Re: Linked List problem, please help.
Ok thanks everyone, I just finished it up. Thanks for the help.