Help with linked node manipulation -- Easy
So I am having trouble getting my "kiss" method(which takes a node and removes it from "KissRing" and assigns it to "frontOfYard"(a different list of linked AffectionNodes). The problem I am having is when I run my test class on it and it tried to use the printSchoolYard method after calling the "kiss" method but it goes into debug mode because frontOfYard is null when I try to assign String tempName = frontOfYard.name. Can you guys help me figure out why this is happening?
Code:
import java.util.ArrayList;
public class AffectionManager {
private AffectionNode frontOfRing;
private AffectionNode frontOfYard;
// Constructor for the AffectionManager class that creates a list of linked AffectionNodes.
public AffectionManager(ArrayList<String> names){
String front = names.get(0);
frontOfRing = new AffectionNode(front);
AffectionNode current = frontOfRing;
for(int i = 1; i < names.size(); i++){
String currentName = names.get(i);
current.next = new AffectionNode(currentName);
current = current.next;
}
}
// Prints out a list of the current kiss ring with who is "so into" who.
public void printKissRing(){
String tempName = frontOfRing.name;
AffectionNode current = frontOfRing;
while(current.next != null){
String name = current.name;
String nextName = current.next.name;
System.out.println(" " + name + " is so into " + nextName);
current = current.next;
}
System.out.println(" " + current.name +" is so into " + tempName);
}
// Prints out a list of the current Schoolyard with who was kissed by who.
public void printSchoolyard(){
String tempName = frontOfYard.name;
AffectionNode current = frontOfYard;
while(current.next != null){
tempName = current.name;
String nextName = current.next.name;
System.out.println(" " + tempName + " was kissed by " + nextName);
current = current.next;
}
System.out.println(current.name +" was kissed by " + tempName);
}
// Returns true if the string passed matches one of the names within the KissRing, otherwise returns false.
public boolean kissRingContains(String name){
AffectionNode temp = frontOfRing;
String tempName;
while(temp != null){
tempName = temp.name;
if(name == tempName){
return true;
}
temp = temp.next;
}
return false;
}
// Returns true if the string passed matches one of the names within the Schoolyard, otherwise returns false.
public boolean schoolyardContains(String name){
AffectionNode temp = frontOfYard;
String tempName;
while(temp != null){
tempName = temp.name;
if(name == tempName){
return true;
}
temp = temp.next;
}
return false;
}
// Returns true if there is only 1 AffectionNode left in the KissRing, otherwise returns false.
public boolean isGameOver(){
if(frontOfRing.next == null){
return true;
}
return false;
}
// Returns the string of the winner if game is over, otherwise returns null.
public String winner(){
if(frontOfRing.next == null){
return frontOfRing.name;
}
return null;
}
// Takes the string passed in and removes any name in the KissRing that matches and adds to the front of the Schoolyard.
public void kiss(String name){
AffectionNode current = frontOfRing;
AffectionNode temp = frontOfYard;
while(current.next != null){
String currentName = current.next.name;
if(name.equals(currentName) && current.next.next != null){
frontOfYard = new AffectionNode(currentName, temp);
current.next = current.next.next;
} else {
current = current.next;
}
}
}
// Returns a string representation of an AffectionManager object.
public String toString(){
System.out.println("[");
String s = " ";
AffectionNode current = frontOfRing;
while(current != null){
s = s + current.name;
current = current.next;
}
return s;
}
}
Re: Help with linked node manipulation -- Easy
Do you call kiss() before printSchoolyard()?
There is nobody in the schoolyard before kiss() is called.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
Do you call kiss() before printSchoolyard()?
There is nobody in the schoolyard before kiss() is called.
Well, yes I see your point. But I call printSchoolyard() then call kiss() then call printSchoolyard().
Test class:
Code:
public class AffectionManagerTest {
public static void main(String[] args){
ArrayList<String> names = new ArrayList<String>();
names.add("Jim");
names.add("Sally");
names.add("Duncan");
names.add("Thad");
names.add("Patty");
AffectionManager affection = new AffectionManager(names);
System.out.println(affection.toString());
affection.printKissRing();
System.out.println();
System.out.println();
affection.kiss("Jim");
affection.printKissRing();
System.out.println();
affection.printSchoolyard();
}
}
Re: Help with linked node manipulation -- Easy
Okay figured out the problem while testing. The kiss method works fine unless it is on the first iteration on the loop. It has to do with the frontOfYard being null but I am not sure why it is happening. Here is kiss() method:
Code:
public void kiss(String name){
AffectionNode current = frontOfRing;
AffectionNode temp = frontOfYard;
while(current.next != null){
String currentName = current.next.name;
if(name.equals(currentName) && current.next.next != null){
frontOfYard = new AffectionNode(currentName, temp);
current.next = current.next.next;
} else {
current = current.next;
}
Re: Help with linked node manipulation -- Easy
[s]What exactly is the if statement in the loop supposed to do? the code inside isn't being executed.[/s]
Ok I figured that out. I would suggest looking back at where you are assigning currentName in your while loop.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
What exactly is the if statement in the loop supposed to do? the code inside isn't being executed.
It is supposed to change the reference(so that the element is being removed from frontOfYard and then add that element into frontOfYard). And it works unless it is the first iteration of the loop. If I pass the second name it executes properly.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
Ok I figured that out. I would suggest looking back at where you are assigning currentName in your while loop.
Response Ninjad.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
Response Ninjad.
So you are saying it should be current.name? because I dont think it will work set up that way. When I tried it there were multiple names missing from the schoolYard when only one should have been missing, and that name was still in the KissRing instead.
Re: Help with linked node manipulation -- Easy
Ok I get whats going on. While referencing current.next you can change that without problem and it will affect the nodes next element. You need something that changes the frontOfRing node if the front is referenced.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
Ok I get whats going on. While referencing current.next you can change that without problem and it will affect the nodes next element. You need something that changes the frontOfRing node if the front is referenced.
Alright well I edited:
Code:
public void kiss(String name){
AffectionNode current = frontOfRing;
AffectionNode temp = frontOfYard;
while(current.next != null){
String currentName = "";
if(name.equals(frontOfRing.name)){
currentName = current.name;
} else {
currentName = current.next.name;
}
if(name.equals(currentName) && current.next.next != null){
frontOfYard = new AffectionNode(currentName, temp);
current.next = current.next.next;
} else {
current = current.next;
}
}
}
However it is still giving me the wrong list when printed out.
This is how it starts:
Jim is so into Sally
Sally is so into Duncan
Duncan is so into Thad
Thad is so into Patty
Patty is so into Jim
After Kiss it should be:
Sally is so into Duncan
Duncan is so into Thad
Thad is so into Patty
Patty is so into Sally
However it prints out now:
Jim is so into Patty
Patty is so into Jim
and that is all.. Lol
Re: Help with linked node manipulation -- Easy
If the name equals the frontOfRing node, you should be changing the frontOfRing to be the next node in the list.
Re: Help with linked node manipulation -- Easy
Quote:
Originally Posted by
brynpttrsn
If the name equals the frontOfRing node, you should be changing the frontOfRing to be the next node in the list.
Ahhhh got it completely functional with a few tweaks. Thanks bryn for helpin me out its appreciated :D