473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Search through a linked list for the value X

33
I am trying to write a function to
find(x) -- Searches through the list for the value x and returns an Iterator pointing to the first node containing value x. If x is not found in the list, it returns an Iterator pointing to NULL. ( I want to use linear search that starts from one end and checks each element one by one.
so I wrote this code:

Expand|Select|Wrap|Line Numbers
  1. //find function
  2. template<typename T>
  3. void LinkedList<T>::find(T value)
  4. {
  5.     Node<T>* pos=first;
  6.     while(pos){
  7.         if (pos.get()==value){
  8.             return pos;
  9.         }
  10.         else{
  11.             pos=pos->next;
  12.         }
  13.     }
  14. return pos=NULL;
  15.  
  16. }
so now lets say in the main part we have

iter2 = rebelList2.find("book");

but my code dosent work
would you help me?
thanks
Feb 14 '08 #1
4 2544
APEJMAN
33
I changed the program to this:

but still dosent work
.................................................. .................
Expand|Select|Wrap|Line Numbers
  1. //find function
  2. template<typename T>
  3. Iterator<T> LinkedList<T>::find(T value) 
  4. {
  5.  
  6.     bool Found;
  7.     Iterator<T> iter;
  8.     iter.position=first;
  9.  
  10.  
  11.    Found = false;
  12.  
  13.    while ((! Found) && (iter.position!=NULL)){
  14.        if (iter.position->data == value)
  15.          return iter;
  16.       else
  17.           iter.position->next;
  18.    }
  19.  
  20.    iter.position=first->prev;
  21.    return iter ;
  22.    }
.................................................. ..............
Feb 14 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Use the STL list template.

You have another post with more linked list code that is in trouble. Please stop tortuing yourself, and us.
Feb 14 '08 #3
APEJMAN
33
why you people are so RUDE!
I made a mistake! OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

by the way thanks
Feb 14 '08 #4
sicarie
4,677 Expert Mod 4TB
why you people are so RUDE!
I made a mistake! OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

by the way thanks
You know, we find it rude when posters create multiple threads on the same question (is this three on linked lists for you now?), and ask things that can be answered with a simple Google search.

It's also interesting to me that you get upset when weaknessforcats - the most prolific and knowledgable poster in the C/C++ forum - shows you the correct answer. You became upset when we (weaknessforcats) helped you. And we (weaknessforcats) are rude because of it... yeah... hmm...
Feb 16 '08 #5

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

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
1
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
8
by: NMarks | last post by:
Hello all, I have created a database for my work that stores information for keys/locks, doors and employees. Specifically the database contains all the information of our lock system, which...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.