public E remove(int index){
for(int i = 0; i < index-1; i++) temp = temp.next;
Node<E> two = temp.next;
//set temp.next to point to the Node next to the Node to be removed
temp.next = two.next;
E elem = two.elem; //store the element to return
two = null; //remove the node
counter--; //decrement size
return elem; //return the element at that position
}

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:47 PM

macosxnerd101, on 09 March 2012 - 03:35 PM, said:

Did you run it? What specific problems or errors are you encountering?

No, Linked List is new to me. I am understanding your tutorial.
My ideas (the way i see it)to remove a node was just to have the previous node point to the node next to the node that is wanted to be removed. And that is what, I believe you are trying to do as well. I'm still trying to understand the code of yours (only that part).
Is line 3 in the loop?

//iterate to the position before the index
for(int i = 0; i < index-1; i++) temp = temp.next;
Node<E> two = temp.next; //is this in the loop??
//set temp.next to point to the Node next to the Node to be removed
temp.next = two.next;
E elem = two.elem;
two = null;
counter--;
return elem;

Re: Doubts about linked Lists!

Posted 09 March 2012 - 03:32 PM

new Node<E> two = temp.next;
temp.next = two.next;
two = two.next;

assuming that your new is a typo:

Node<E> two = temp.next; // two is temp.next
temp.next = two.next; // two being temp.next the statement is equivalent to:
temp.next = temp.next.next;
two = two.next // two being temp.next the statement is equivalent to
temp.next = temp.next.next;

Re: Doubts about linked Lists!

Posted 10 March 2012 - 08:35 AM

public E remove(int index){
for(int i = 0; i < index-1; i++) temp = temp.next;
Node<E> two = temp.next;
//set temp.next to point to the Node next to the Node to be removed
temp.next = two.next;
E elem = two.elem; //store the element to return
two = null; //remove the node
counter--; //decrement size
return elem; //return the element at that position
}

____________________________________________________________
Will the unwanted node be temp after the loop? If so then,
I am forcing the loop to break as soon as the loop reaches the node previous to unwanted node. That previous node will be in the temp. I, then, point that previous node(temp) to temp.next.next node. Then, I store the element of the unwanted node (temp.next) into E. Then, I dispose temp.next = null and temp.next.element = null. And i finally decrement size(counter) and return E. Am i thinking right? Below i have the code:
_____________________________________________________________My interpretation of your code:
Your code above, you are expecting the user to enter an actual index number(including 0).
Then you also doing the same thing as I am but you are creating a new node. I am expecting the user to give me node number(0 will be invalid).
Am I right?
_____________________________________________________________

Re: Doubts about linked Lists!

Posted 10 March 2012 - 06:21 PM

It is not that easy
Setting a Node to null does not remove it from the list

If it is a doubly linked list you'll have to:
- set the previous node link of the forward node pointer to the previous node of the node you remove
- set the next node forward pointer of the previous to the next forward pointer of the node you remove