468,484 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Size of linked lists without traversing list

Hi every body,

I have a linear single list , so, how to find out the length of the linked list w/o traversing the linked list.....


Thanks in Advance
Feb 4 '08 #1
3 3669
sicarie
4,677 Expert Mod 4TB
Keep an int i_linkedListSize variable that has the size, and modify it as you add/remove nodes.

This is rather poor form, though. If you forget to modify this anywhere you modify your list in your code (or if someone else comes and tries to use your code later), and they forget, you run into problems.
Feb 4 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
You can minimize the danger by using a struct for the list:
Expand|Select|Wrap|Line Numbers
  1. struck LinkedList
  2. {
  3.     int numNodes;
  4.     Node* start;          //start of the list
  5.     Node* end;           //end of the list;
  6.     Node* current;      //our current position in the list.
  7. };
  8.  
and a separate struct for the Node.

Then carefully ensure your functions update the LinkedList consistently.

This type of design a called a cursor-based list.
Feb 4 '08 #3
Is this the ONLY way to know the size of the list without traversing it?
Mar 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

32 posts views Thread by source | last post: by
33 posts views Thread by junky_fellow | last post: by
hi
11 posts views Thread by Ganga | last post: by
6 posts views Thread by Baltazar007 | last post: by
45 posts views Thread by charles.lobo | last post: by
46 posts views Thread by junky_fellow | last post: by
23 posts views Thread by Himanshu Chauhan | last post: by
12 posts views Thread by kalyan | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.