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 8 of 8

Thread: why am I getting an endless loop in this implementation of addlast to linkedlist

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

    Default why am I getting an endless loop in this implementation of addlast to linkedlist

    guys, I'm trying to implement an "addLast" method of a LinkedList; however, I'm getting an endless loop.... could anyone help

    public void addLast(int n)
        {
            Node p = new Node(n);
     
            if(first == null)
            {
                p.next = first;
                first = p;
            }
            else
            {
                Node current = first;
                while(current != null)
                {
                    System.out.println("here!");
                    if(current.next == null)
                    {
                        current.next = p;
                    }
                    current = current.next;
                }
            }
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    Think about how you are looping...write the process out on paper if you have to....when the current node's next element is null, you set it the the 'p' node. You loop once more, seeing that the current (which is now p) next node is null, so you set it's next element to p (or itself) - hence there will not be a non-null next element again (circular reference)

  3. The Following User Says Thank You to copeg For This Useful Post:

    mia_tech (June 18th, 2012)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    while(current != null)
    {
    System.out.println("here!");
    if(current.next == null)
    {
    current.next = p;
    }
    current = current.next;

    Ok, so if the next Node is null, then current.next, a.k.a. the next node, is made p. Then after adding p, which it does right away, current.next won't be null, hence an infinite loop.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    mia_tech (June 18th, 2012)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    else
    {
    Node current = first;
    while(current.next != null)
    {
    System.out.println("here!");

    current = current.next;
    }

    current.next = p;
    p.next = null;

    }

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

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    Thank you guys, it was so simple as to adding a "break" line

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

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    to: javapenguin

    oh this one is very simple too... I just added a "break" line to mine

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

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    Quote Originally Posted by javapenguin View Post
    else
    {
    Node current = first;
    while(current.next != null)
    {
    System.out.println("here!");

    current = current.next;
    }

    current.next = p;
    p.next = null;

    }
    is it necessary to assign "p.next = null"?.... it is already null right?

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: why am I getting an endless loop in this implementation of addlast to linkedlist

    it is already null right?
    Add a println to the code to print out its value before you set it to null to see what its value is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 01:52 PM
  2. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 09:51 PM
  3. Implementation of remove method in linkedlist Iterator
    By jay2you in forum Collections and Generics
    Replies: 3
    Last Post: October 12th, 2011, 10:16 AM
  4. drawImage() causes endless calls to paintComponent()
    By repaint_forever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2011, 01:15 PM
  5. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 04:14 PM