Thread: Linked list tail

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    Linked list tail

    hello i cannot understand why everytime we add a new entry in list we moving the tail with that way
    Code:
             l->tail->next=new_entry;
    	    	  l->tail=new_entry;
    shouldn't we use just one of them
    what's the difference that force us to use both lines
    l is the basic list and new_entry the new we must add
    Last edited by cable; 10-08-2011 at 11:43 AM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The first line adds new_entry to your linked list, as per the
    we add a new entry in list
    part of the routine.

    The second line explicitly makes new_entry the new tail of the list by assigning it to l->tail, which satisfies the
    we moving the tail
    part.

    Having a variable l->tail available for use reduces the time complexity of adding an item at the end of the list, from O(n) to O(1) (otherwise one would have to traverse the entire list to find the end).
    Consider this post signed

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, it would help for a more complete example; however from the code you introduced I can infer the following:
    • L represents your main list
    • tail is a pointer of type 'node' (your data).
    • The 'l->tail->next' line actually adds your new data(node) to the list
    • The 'l->tail' line updates the new end of your list

    So in your case both lines of code would be necessary. However it is hard to comment anymore based on the lack of information.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    wow thanks for your perfect explanation both of you
    have a nice day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List - Add to Tail
    By beatbox32 in forum C Programming
    Replies: 9
    Last Post: 08-08-2011, 03:43 PM
  2. doubly linked list tail pointer in struct
    By bazzano in forum C Programming
    Replies: 15
    Last Post: 06-11-2007, 12:31 PM
  3. loosing tail in doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 05-06-2007, 11:46 PM
  4. Another List Q - Seg fault on head-tail list
    By JimpsEd in forum C Programming
    Replies: 11
    Last Post: 05-10-2006, 12:53 AM
  5. tail pointer - linked list
    By Space_Cowboy in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2002, 03:05 AM