Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: linked list printing empty after removing duplicates

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

    //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

    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

    run:
    true
    List is empty!
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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)
    Last edited by Norm; June 9th, 2012 at 06:19 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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...
    Last edited by mia_tech; June 9th, 2012 at 07:37 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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?
            if(found)
                return found;
            else
                return found;

    Seems a bit silly
    Last edited by Norm; June 10th, 2012 at 10:11 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: linked list printing empty after removing duplicates

    yeah, I just write "return found"...

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: linked list printing empty after removing duplicates

    I'm still not removing anything... any help?
    Not without a program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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"

    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


    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

    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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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.
    Last edited by Norm; June 10th, 2012 at 01:59 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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...

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM
  2. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 04:20 PM
  3. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  4. removing stopwords from a word list
    By jessie in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 15th, 2010, 12:02 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM