Thread: i can't seem to understand the logic behind linked lists :|

  1. #16
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    and here i've shortended to code even more, i guess it's easier to understand:

    Code:
      	for (prev=NULL; loc != NULL; loc = loc->next)
          	{
    			prev = loc;
          	}
          	
        	prev->next = temp;

  2. #17
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Axel
    and here i've shortended to code even more, i guess it's easier to understand:

    Code:
      	for (prev=NULL; loc != NULL; loc = loc->next)
          	{
    			prev = loc;
          	}
          	
        	prev->next = temp;
    check for loc->next is not equal to null

    ssharish2005

  3. #18
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    wouldn'tt that already be handled when i malloc the new node?

  4. #19
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    noo. the reason why i said, when u interate through each node u will check for the last node.The way u can find is that the last node next should be null. So when u check for loc->next is equal to null u will come to know that u reached end of the node. and then u can follow with appending a new node.

    ssharish2005
    Last edited by ssharish2005; 10-14-2006 at 04:26 AM.

  5. #20
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Axel
    and here i've shortended to code even more, i guess it's easier to understand:

    Code:
      	for (prev=NULL; loc != NULL; loc = loc->next)
          	{
    			prev = loc;
          	}
          	
        	prev->next = temp;
    this is not quite rite ...

    say u have
    Code:
    "Panda" -> NULL
    on the first iteration, loc = *start yes?
    and then you assign prev to loc...
    Code:
    prev = loc;
    and after that, the for loop checks the nullity of loc
    Code:
    for (prev=NULL; loc != NULL; loc = loc->next)
    .. in this case, no... and then loc goes to the next element which is null...
    Code:
    for(..., loc = loc->next)
    which is a problem because after that
    Code:
    prev = loc; // prev becomes null (but we don't want it to be)
    remember in linked list, the last element isn't denoted by NULL... it's denoted by the last item BEFORE NULL
    NULL is just like '\0' character in string.

    got me? this is exactly what ss was saying
    Last edited by yxunomei; 10-14-2006 at 04:41 AM.

  6. #21
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok i think i've understood my original code now please correct me if i'm wrong.

    We have the first element, Cat already added so:

    Code:
    if (*start == NULL)
    evaluates to false so we go to the else bit.

    Code:
    else
       {
          loc = *start; // MAKE loc point to the start of the list.
          prev = NULL;
    
           for (i=0; loc != NULL; i++) //here we just loop till we find an empty position to append to
           {
      
               	prev = loc; // keep track of the previous node by copying what the current node equals to
                    loc = loc->next; // set loc to the next node
            }
    Code:
    once the loop ends it means we have found an empty position i.e.:
    
    [   CAT  ]    [ NULL ]  
    
    prev              loc
    
     so:
    
     prev->next = temp; 
    
    we just insert the new node AFTER prev->next i.e.:
    
    
    [   CAT  ]    [ DOG (new node inserted) ] 
    
    prev      - >  next

  7. #22
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    i don't see why you need the i in
    Code:
    for (i=0; loc != NULL; i++)
    just do something like
    Code:
    for(loc = *start; loc != NULL; loc = loc -> next)
    this, not only you make it easier to understand, but also make your code shorter because then you can take out the
    Code:
     loc = *start
    at the start of the else bit.

    better yet
    Code:
    for(loc = *start; loc->next != NULL; loc = loc->next) { 
       // do nothing, it's all done in the loop
    }
    
    loc->next = temp;
    Last edited by yxunomei; 10-14-2006 at 04:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  4. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM