Thread: head & last ptrs in linked lists

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40

    head & last ptrs in linked lists

    Hi,
    To quote Prelude's FAQ on linked lists

    For code that maintains both a head and tail pointer, the first and last functions will never even be used! The only reason I put them there is because I'm usually too lazy to maintain head and tail pointers. But just so that you know the issues, regular use of first and last can be a big performance hit for large lists.
    My question is that, how is maintaining a head and tail pointer more expensive than, say traversing the entire list each time you want to add a node at the end?

    Thanks,
    Sahil

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    it's not

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how is maintaining a head and tail pointer more expensive than,
    >say traversing the entire list each time you want to add a node at the end?
    In general, an O(N) operation like walking down the list to insert a new node at the end is way more expensive than caching the current end of the list and making the same insertion O(1) because you usually spend a lot more time finding a place to insert than you do for the actual insertion. So it's not more expensive performance-wise, but it could be space-wise if you have certain space constraints.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04: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. Linked Lists in C++
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2002, 07:28 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM