// This method should insert a new node containing the string newString immediately after the first
// occurrence of the string aString in the list. If aString is not in the list, the method should add a node
// containing newString to the tail of the list.
public void insertAfter(String newString, String aString)
{
StringLLNode newNode = new StringLLNode();
newNode.setData(newString);
//Check if HeadNode == aString
if (headNode.getData().equalsIgnoreCase(aString))
headNode = newNode;
//rest of the nodes
StringLLNode currNode = headNode;
while(currNode != null)
{
if (currNode.getData().equalsIgnoreCase(aString))
{
newNode.setNext(currNode);
//System.out.println("It went THROUGH");
}
currNode = currNode.getNext();
}
//Last Node
if (currNode != null)
{
newNode.setNext(headNode);
}
}
if (headNode.getData().equalsIgnoreCase(aString))
headNode = newNode;
The first thing I can see wrong is that you are supposed to insert the newString after aString. But all that code does is make newString the head and tosses away the rest of the list. if (headNode.getData().equalsIgnoreCase(aString))
headNode.setNext(newNode);
I tested but the code still does not fix the problem public void insertAfter(String newString, String aString)
{
StringLLNode newNode = new StringLLNode();
newNode.setData(newString);
//Check if HeadNode == aString
if (headNode.getData().equalsIgnoreCase(aString))
headNode = newNode;
//rest of the nodes
StringLLNode currNode = headNode;
while(currNode != null)
{
if (currNode.getData().equalsIgnoreCase(aString))
{
newNode.setNext(currNode);
//System.out.println("It went THROUGH");
}
currNode = currNode.getNext();
}
//Last Node
if (currNode != null)
{
newNode.setNext(headNode);
}
}
Given a linked list [A-E] we have: A => B => C => D => E. Each Node is referencing the node to it's right, so A references B, D references E etc.StringLLNode newNode = new StringLLNode();
newNode.setData(newString);
//Check if HeadNode == aString
if (headNode.getData().equalsIgnoreCase(aString))
headNode.setNext(newNode);
Then I will check for the rest. After i drew the boxes i figured out that if the next (or subsequent) nodes equals what i want, then I will need to:while(currNode != null)
{
if (currNode.getData().equalsIgnoreCase(aString))
{
newNode.setNext(currNode.getNext());//links the new node to the next node after what was equal
currNode.setNext(newNode);
}
currNode = currNode.getNext();
}
But it seems that this turns out to be an infinite loop!deyiengz wrote:Why are you treating the headNode differently to any other node? All you are doing is setting the heads next node to equal the new node.
1. I will check if the head node equals the the string i need to insert another string after: and I think this code should do itStringLLNode newNode = new StringLLNode(); newNode.setData(newString); //Check if HeadNode == aString if (headNode.getData().equalsIgnoreCase(aString)) headNode.setNext(newNode);
newNode.setNext(currNode.getNext());//links the new node to the next node after what was equal
currNode.setNext(newNode);
//YOU INSERTED THE NEW NODE!!! STOP ITERATING OVER THE LOOP
2. [newNode equals null] - no match found, do nothingStringLLNode newNode = new StringLLNode(); newNode.setData(newString);
deyiengz wrote:The simple code? Explain to me, what is the simple code? This is exactly my point why are you treating the head differently if you always insert after a match.
Mel I thought I go through the head node first and do the simple code if it equals what i'm looking for.
I don't see why you say the new node is null after this codeI am not saying that, I am saying your "simple code" sets heads next node to equal the new node and disregards the rest of the list. What you want to do is if a match is found with the currentNode, set the newNodes next() to equal the currentNodes next(), then set the currentNodes next() to equal the new node. Exactly what you do here:StringLLNode newNode = new StringLLNode(); newNode.setData(newString);
while(currNode != null){
if (currNode.getData().equalsIgnoreCase(aString)){
newNode.setNext(currNode.getNext());//links the new node to the next node after what was equal
currNode.setNext(newNode);
}
currNode = currNode.getNext();
}
Again don't treat the head differently, initially set the currNode to equal the head, then use your loop above. You will need to break or return from the while loop when a match is found unless the linkedlist contains non unique values. In which case if the linkedlist contains non unique values you will need to create a newNode with the newString each time you insert, otherwise you are referencing the same object.