linked list printing empty after removing duplicates
guys, I have a remove method that looks for duplicates and remove them; however, after I call that method my list appears to be empty, and that is b/c the "first" node is pointing to the last node in the list and of course there's nothing after that, and that is why is empty. How can I initialize it that it points back to the first node when calling the "display" method?
Code :
//remove duplicate records (return true if duplicate found)
public boolean remove(String fn, String ln)
{
Node remove;
boolean found = false;
int duplicate = 0;
while(first != null)
{
if(first.value.getFname().equals(fn) && first.value.getLname().equals(ln))
{
duplicate++;
if(duplicate > 1)
{
remove = first;
found = true;
}
}
first = first.next;
}
if(found)
return found;
else
return found;
}
//display list of student
public void display()
{
if(first == null)
System.out.println("List is empty!");
else
{
while(first != null)
{
System.out.println(first.value);
first = first.next;
}
}
}
main
Code :
public class Tester {
public static void main(String[] args) {
UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics");
UnderGrad jorge = new UnderGrad("jorge", "vazquez", 3.8, "computer Science", "programming");
UnderGrad john2 = new UnderGrad("john", "doe", 3.0, "Computer Engineering", "phisics");
Advisor jim = new Advisor("jim", "smith");
Grad jane = new Grad("jane", "doe", 3.0, "Electric Engineering", jim);
LinkedList students = new LinkedList();
students.add(john);
students.add(jorge);
students.add(john2);
students.add(jane);
// students.display();
System.out.println(students.remove("john", "doe"));
students.display();
}
output
Code :
run:
true
List is empty!
BUILD SUCCESSFUL (total time: 0 seconds)
Re: linked list printing empty after removing duplicates
Time for some more debugging. Add printlns to the code to shown the values of variables as you change them to see where the links are getting disconnected to make an empty list.
Another technique for working with linked lists is to play computer with a piece of paper and pencil.
Draw the list before the method is called then play computer by making changes to the list on the paper as per the logic in the program. You should be able to see where the problem is.
If you want help debugging the code you will have to post enough of it to compile and execute and show the problem.
The Node class should have a compare method you can use instead of having to do this:
if(first.value.getFname().equals(fn) && first.value.getLname().equals(ln))
perhaps you'd call it with something like this:
first.value.compareNames(lastName, firstName)
Re: linked list printing empty after removing duplicates
where should I put the println?... right now I don't see where they are getting disconnected, by the way you didn't answer my question, is the reason that I'm getting an empty list is b/c I reached the end or b/c I remove a node in the middle...
Re: linked list printing empty after removing duplicates
Print out the values of the variables before and after every time their value changes.
What is this code for?
Code :
if(found)
return found;
else
return found;
Seems a bit silly
Re: linked list printing empty after removing duplicates
yeah, I just write "return found"...
Re: linked list printing empty after removing duplicates
another solution is to use a local variable to iterate through the list; however, I'm still not removing anything... any help?
Re: linked list printing empty after removing duplicates
Quote:
I'm still not removing anything... any help?
Not without a program that compiles, executes and shows the problem.
Re: linked list printing empty after removing duplicates
the program is pretty much the same code as shown above, but the remove method I changed.. now I'm removing the wrong "Student" instead of removing the duplicate node= "john doe"
Code :
public class LinkedList {
private Node first;
public LinkedList()
{
first = null;
}
//add students to the list
public void add(Student s)
{
Node newNode = new Node(s);
newNode.next = first;
first = newNode;
}
//remove duplicate records (return true if duplicate found)
public boolean remove(String fn, String ln)
{
Student remove;
boolean found = false;
int duplicate = 0;
for(Node iterator = first; iterator != null; iterator = iterator.next)
{
if(iterator.value.getFname().equals(fn) && iterator.value.getLname().equals(ln))
{
duplicate++;
if(duplicate > 1)
{
remove = first.value;
first = first.next;
return found = true;
}
}
}
return found;
}
//display list of student
public void display()
{
if(first == null)
System.out.println("List is empty!");
else
{
while(first != null)
{
System.out.println(first.value);
first = first.next;
}
}
}
}
main
Code :
public class Tester {
public static void main(String[] args) {
UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics");
UnderGrad jorge = new UnderGrad("jorge", "vazquez", 3.8, "computer Science", "programming");
UnderGrad john2 = new UnderGrad("john", "doe", 3.0, "Computer Engineering", "phisics");
Advisor jim = new Advisor("jim", "smith");
Grad jane = new Grad("jane", "mckee", 3.0, "Electric Engineering", jim);
LinkedList students = new LinkedList();
students.add(john);
students.add(jorge);
students.add(john2);
students.add(jane);
System.out.println(students.remove("john", "doe"));
students.display();
}
}
output
Code :
run:
true
john doe 3.0 Computer Engineering Good phisics
jorge vazquez 3.8 computer Science Good programming
john doe 2.7 computer Science Good phisics
Re: linked list printing empty after removing duplicates
Sorry, I can't help you. There are too many missing classes for testing.
I don't see any println staements for debugging. You need to add some to see what the code does when it executes.
Re: linked list printing empty after removing duplicates
yeah, I tried that... didn't help... thanks anyway. may be someone else would like to take a crack at this...
Re: linked list printing empty after removing duplicates
Hard to debug if it does not compile.
The way I'd debug it is by adding lots of println statements to see what the code is doing. If you don't know what the code is doing, it is very hard to fix it.
Re: linked list printing empty after removing duplicates
You need to play computer with the code to see what it is doing.
Take a piece of paper and draw a linked list with a first variable pointing to the list. Then manually remove one of the items in the list by drawing in the new connections. The steps that you did to remove one of the elements is what your code needs to do.
There are three cases:
remove first node
remove middle node
remove last node
You should work on the three cases one at a time. Design and write the code and then test and debug it. When it works do the same for the next case. When testing go back and retest the previous cases to make sure the new code did effect the old code.