Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 9th, 2007, 3:03 AM   #1
comp_wizard07
Newbie
 
Join Date: Oct 2007
Posts: 2
Rep Power: 0 comp_wizard07 is on a distinguished road
Search method for singly linked list

Hi,

Is there any effective search method for a singly linked list?

linear search is too costly.
comp_wizard07 is offline   Reply With Quote
Old Oct 9th, 2007, 3:36 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Linear search is all you've got, boy. If you want a more efficient search, use a different data structure.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Oct 9th, 2007, 6:35 PM   #3
mark
Newbie
 
Join Date: Mar 2006
Posts: 14
Rep Power: 0 mark is on a distinguished road
couldn't you sort the linked list, then use a binary search
mark is offline   Reply With Quote
Old Oct 9th, 2007, 6:44 PM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Thats what a binary tree is for, is it not?
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Oct 9th, 2007, 7:58 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by mark
couldn't you sort the linked list, then use a binary search
I suppose you could sort it, though it would be painful. However, the reason a binary search won't work (at least not with the expected efficiency) is not because the list is unsorted. It's because the list is not random access. In order to access a given element, you need to iterate through all the preceding elements. A linked list is thus O(N) for accessing elements, and therefore, your search won't be more efficient.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Oct 10th, 2007, 4:47 AM   #6
comp_wizard07
Newbie
 
Join Date: Oct 2007
Posts: 2
Rep Power: 0 comp_wizard07 is on a distinguished road
Thanks for the update.

I have tried binary search, but is not efficient for a singly linked list.
comp_wizard07 is offline   Reply With Quote
Old Oct 10th, 2007, 7:59 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You got a primo answer in response #1. Resonse #2 should never have been posted. You should be Googling for data structures and searching/sorting thereof by now....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 11th, 2007, 11:39 AM   #8
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>I have tried binary search, but is not efficient for a singly linked list.
Duh. Binary search relies on random access (or a suitable data structure) for its speed. Linked lists are notoriously bad at random access unless you want to go for broke and write something ridiculously complicated.

If a sorted linear search is too slow, you're not using the right data structure. Do some research on skip lists, and consider using a binary search tree or hash table.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Feb 11th, 2008, 3:14 AM   #9
rhish.franks
Programmer
 
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1 rhish.franks is on a distinguished road
Re: Search method for singly linked list

I will suggest you to use indexd search even for singly linked list! just create a array of structure for linked list. also for this you have to keep the linked list sorted. the code for that is also easy and this method is usefull if heavy searching is there and less insertions are to be done.insertion can be done in sorted manner!
rhish.franks is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list problems bl00dninja C++ 6 Feb 17th, 2008 10:30 AM
dev c++ software, template problem cairo C++ 11 Jun 2nd, 2006 12:42 PM
Singly Linked List Help Firebar Java 3 May 22nd, 2005 10:56 AM
User-defined creatNode and deleteNode functions for a doubly-linked list jgs C 2 Apr 28th, 2005 8:53 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC