circular list search

Discussion in 'C++' started by dave, Oct 8, 2005.

  1. dave

    dave Guest

    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
    dave, Oct 8, 2005
    #1
    1. Advertising

  2. dave wrote:

    > 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.
    Jacques Labuschagne, Oct 8, 2005
    #2
    1. Advertising

  3. dave

    Kai-Uwe Bux Guest

    dave wrote:

    > 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
    Kai-Uwe Bux, Oct 9, 2005
    #3
  4. dave wrote:
    >
    > 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.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 10, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    722
    Jonathan Mcdougall
    Jan 3, 2005
  2. Sven
    Replies:
    1
    Views:
    137
    Roy Smith
    Mar 7, 2013
  3. Sven
    Replies:
    3
    Views:
    147
    Steven D'Aprano
    Mar 8, 2013
  4. Chris Angelico
    Replies:
    0
    Views:
    111
    Chris Angelico
    Mar 7, 2013
  5. Chris Rebert
    Replies:
    0
    Views:
    110
    Chris Rebert
    Mar 7, 2013
Loading...

Share This Page