10 Replies - 627 Views - Last Post: 11 March 2012 - 09:39 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Doubts about linked Lists!

Posted 09 March 2012 - 11:41 AM

  • Node<E> two = temp.next;
    
    (this is the second code) Does this line mean temp is pointing to the new "two" node?
  • Since, I want to remove the index node. I am thinking of fiddling with previous nodes. Something like this:

    public E remove(int index) {
                for (int i = 0; i < index - 1 - 1; i++) {
                temp = temp.next;
            }
            temp.previous.next = temp.next;
        }
    


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
}


I just corrected my code.

This post has been edited by deprosun: 09 March 2012 - 11:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Doubts about linked Lists!

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10826
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:16 PM

Quote

(this is the second code) Does this line mean temp is pointing to the new "two" node?

This means that two is pointing to the temp.next Node.
Was This Post Helpful? 0
  • +
  • -

#3 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:23 PM

View Postmacosxnerd101, on 09 March 2012 - 03:16 PM, said:

Quote

(this is the second code) Does this line mean temp is pointing to the new "two" node?

This means that two is pointing to the temp.next Node.


Great tutorial btw.
So what is your interpretation of my code?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10826
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:35 PM

Did you run it? What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#5 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:47 PM

View Postmacosxnerd101, 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;

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10826
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: Doubts about linked Lists!

Posted 09 March 2012 - 01:52 PM

Line 3: No, that is outside of the loop. Because the loop has no braces, only the first statement is in the loop.

Quote

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

Yes, this is correct.
Was This Post Helpful? 0
  • +
  • -

#7 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: Doubts about linked Lists!

Posted 09 March 2012 - 03:00 PM

Line 6!
I get everything until line 6. Wouldnt you do something like this?

temp.previous=two;


new Node<E> two = temp.next;

Is
temp.next = two.next;
same as
two = two.next;
??
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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;


so yes, equivalent
[/code]
Was This Post Helpful? 1
  • +
  • -

#9 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

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

temp = head;
for (int i = 0; i < index - 1; i++) {
            if(i==index-2){
               temp=temp.next.next;
               break;
            }
             temp = temp.next;
          }
E elem = temp.element;
temp.next.element = NULL;
temp.next = NULL;
counter--;
return E;


This post has been edited by deprosun: 10 March 2012 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#11 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 296
  • Joined: 16-November 10

Re: Doubts about linked Lists!

Posted 11 March 2012 - 09:39 AM

View Postpbl, on 10 March 2012 - 08:21 PM, said:

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


Then how else would one remove the node.
Isn't that macosxnerd101's tutorial is doing?
And yes double linked lists are less complicated:
temp.previous.next=temp.next;
temp.next.previous=temp.previous;


And I just reviewed my code (with few errors):
temp = head;
for (int i = 0; i < index - 1; i++) {
            if(i==index-2){
               temp=temp.next.next;
               break;
            }
             temp = temp.next;
          }
E elem = temp.next.element; //I forgot to put the "next"
temp.next.element = NULL;
temp.next = NULL;
counter--;
return elem; //i was returning "E" before :P/>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1