First off, I need an explanation rather than a solution. As you see from the code, I am trying to insert a MovieNode into a MovieList. And the method is in the MovieList class. "list" is where I keep my MovieNodes.
public void insert(Movie movie, int index)
{
MovieNode node = new MovieNode(movie);
MovieNode element;
MovieNode current = list;
while(current.previous != null)
current = current.previous;
element = current; // first node in the list
for(int n = 1; n < index; n++)
element = element.next; // determines where to put new node
node.next = element;
element.previous.next = node;
}
The method above seems working. However when I change this line
element.previous.next = node;
with this one;
element = node;
There is no change in the linkedlist after insertion. It seems to me that element.previous.next is just the same thing as element itself since when we replace element with node, we also change the successor of the node that comes before the element in the list. And we point that successor to our new element which is node. I am new to the subject so I am sorry for the mistakes that I may have made.