1

I'm trying to add a new Node to the end of my linked list but it doesn't seem to be doing anything. It adds the first element because it's a special case but then ignores all of the other assignments when I step through the debugging.

Here's the test I'm running:

@Test
public void testInsertElement()
{
PriorityList<String> list = new LinkedPriorityList<String>();
list.insertElementAt(0, "first");
list.insertElementAt(1, "second");
list.insertElementAt(2, "third");
assertEquals("first" , list.getElementAt(0));
assertEquals("second", list.getElementAt(1));
assertEquals("third" , list.getElementAt(2));
}

It fails on the second assertion because nothing is added after the first.

Here's the constructor for the Node Objects:

public class LinkedPriorityList<E> implements PriorityList<E> {

  private class Node
  {

    private E data;
    private Node next;

    public Node(E element)
    {
      data = element;
      next = null;
    }
  }

And finally the code that is failing on me:

public void insertElementAt(int index, E element) throws IllegalArgumentException
  {
      if(index>size() || index<0) //can only be between 0 and size()
            throw new IllegalArgumentException();

      if(size()==0)
          first = new Node(element); //adding the first element. This works
      else
      {
          if(index == size()) //if an element is being added to the end
          {
              Node ref = first;                //assigning ref to the first element of the list
              for(;ref!=null; ref = ref.next); //stepping through the list until ref is null
              ref = new Node(element);         //assigning the null reference a new Node. Doesn't assign
          }
          else //if an element is being inserted in the list. untested...
          {
              Node ref = first;
              Node temp = new Node(element);
              for(int i=1; i<index; i++)
                  ref = ref.next;
              temp = ref.next;
              ref = temp;
          }
      }
      size++; //keeping track of how many elements in list
  }

I think this works but if you want the get method too, here it is:

public E getElementAt(int index) throws IllegalArgumentException
  {
    if(index>=size() || index<0)
        throw new IllegalArgumentException();

    Node ref = first;
    for(int i=0; i<index; i++)
        ref = ref.next;
    return ref.data;
  }
2
  • 2
    If the size isn't 0, then your insertElementAt method does nothing useful. It ends up assigning a value to a local variable, but that's all. It never adds a new element into the list. Which statement did you expect to do that? Commented Oct 11, 2013 at 21:57
  • Sorry I didn't include it. First is actually a global instance variable Commented Oct 11, 2013 at 22:03

3 Answers 3

3

When index == size, you want to create a new node, find the last node in the list, and assign the new node to its next pointer.

The last node is the one whose next pointer is null.

This should be enough to let you implement the algorithm by yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

This is probably what you meant to do:

for(; ref.next != null; ref = ref.next) {
  /* intentionally empty */
}
ref.next = new Node(element); 

Note that I'm both testing and assigning ref.next, not ref itself.

3 Comments

Am I the only one to find that a while loop would be much more readable here, and wouldn't need any comment?
@JBNizet That and extracting it into a method, i.e. getLastNode().
I agree, but I tried to keep the changes to his code minimal, so he finds easily where this part is supposed to go to.
1

You need a temp node when adding at the end too (to keep track of the last element)

if (index == size())
{
    Node ref = first, temp = first;
    for (; ref != null; temp = ref, ref = ref.next);
    temp.next = new Node(element);    
}

By just assigning the new Node to ref; it doesn't link it to the current last node's next.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.