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. Advertisements

  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. Advertisements

  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. Advertisements

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. Sharp

    Circular linked list

    Sharp, Mar 5, 2005, in forum: Java
    Replies:
    3
    Views:
    1,541
    Thomas G. Marshall
    Mar 5, 2005
  2. Vince
    Replies:
    4
    Views:
    409
    Old Wolf
    Nov 30, 2004
  3. Kiuhnm
    Replies:
    16
    Views:
    902
    Jonathan Mcdougall
    Jan 3, 2005
  4. Booser
    Replies:
    1
    Views:
    1,669
    CBFalconer
    Feb 9, 2004
  5. HalFas`
    Replies:
    3
    Views:
    998
    Daniel Pitts
    Nov 11, 2007
  6. Abby Lee
    Replies:
    5
    Views:
    681
    Abby Lee
    Aug 2, 2004
  7. Sven
    Replies:
    1
    Views:
    256
    Roy Smith
    Mar 7, 2013
  8. Sven
    Replies:
    3
    Views:
    269
    Steven D'Aprano
    Mar 8, 2013
Loading...