circular list search

D

dave

I created a circular linked list
whats the best way to search function for a name in the list and retrun that
node address


class PhoneBook
{
friend PhoneBookUserInterface;
public:
PhoneBook();
~PhoneBook(){};
void add(PhoneBookRec &pbr);
void remove(PhoneBookRec &pbr);
PhoneBookRec search(PhoneBookRec &key);
// PhoneBookRec get_rec(int i){return data;}
int get_number_of_entries();
void save(string filename);
void load(string filename);


private: //functions
void insertAfter(ListNode * pInsertAfterThisNode, PhoneBookRec & pbr);
ListNode * findInsertionPtr(PhoneBookRec &pbr);
ListNode * findDeletePtr(PhoneBookRec &pbr);
void deleteAt(ListNode * pDeleteThisNode);


private: //variables
ListNode * pTop;
ListNode *pBottom;

};



#endif
 
J

Jacques Labuschagne

dave said:
I created a circular linked list
whats the best way to search function for a name in the list and retrun that
node address

This is hardly different to searching through any other list. You start
at the first node (pTop?) and you keep following ListNode::next pointers
until you either hit the end (i.e. you're back at the first node) or
until you find the key you're looking for.

Would you like to refine your question?

Jacques.
 
K

Kai-Uwe Bux

dave said:
I created a circular linked list
whats the best way to search function for a name in the list and retrun
that node address


class PhoneBook
{
friend PhoneBookUserInterface;
public:
PhoneBook();
~PhoneBook(){};
void add(PhoneBookRec &pbr);
void remove(PhoneBookRec &pbr);
PhoneBookRec search(PhoneBookRec &key);
// PhoneBookRec get_rec(int i){return data;}
int get_number_of_entries();
void save(string filename);
void load(string filename);


private: //functions
void insertAfter(ListNode * pInsertAfterThisNode, PhoneBookRec & pbr);
ListNode * findInsertionPtr(PhoneBookRec &pbr);
ListNode * findDeletePtr(PhoneBookRec &pbr);
void deleteAt(ListNode * pDeleteThisNode);


private: //variables
ListNode * pTop;
ListNode *pBottom;

};



#endif


I would suggest not to roll your own code, but to use std::list, or maybe
std::map or std::multimap in the first place. Those container offer
anything you need to build a PhoneBook. So why would you want to dirty your
hands by touching raw pointers?


Best

Kai-Uwe Bux
 
K

Karl Heinz Buchegger

dave said:
I created a circular linked list
whats the best way to search function for a name in the list and retrun that
node address

The 'best' way is the way you can solve that homework assignment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top