LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2006, 09:32 PM   #1
manas_sem
LQ Newbie
 
Registered: Dec 2006
Posts: 29

Rep: Reputation: 15
Linked list


How fast can we search a node in a linked list, instead of traversing the whole list
 
Old 12-20-2006, 10:35 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Linked lists require a sequential search. If you want something faster, use a hash table, or at least a binary tree.
 
Old 12-21-2006, 01:42 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Well, if you keep your list sorted, then you might not need to fully traverse it in every case, since you can stop the search as soon as you find the node you want.
I think that the list is O(n), and you always end up traversing it all the way eventually. Unless you compliment it with another type of auxiliar index (like Dan04 said, a Tree [any kind] or a hash table).


But at the bottom line, nothing beats a good hash table in terms of direct access

Cheers!
 
Old 12-21-2006, 01:53 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
As the others have said there are alternative structures out there which are better at searching, but it really depends on what you want to do, if the list is small the time doesn't really matter much, if the list is large but searching for a specific node is rare then again the time to access it might not be too important.

You can build an extra index structure that at the cost of memory will significantly speed up access (I guess from O(n) to O(logn)), but that will increase the overhead, an insert update and a delete will also require the index to be modified.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C linked list exvor Programming 4 04-28-2006 05:25 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 10:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration