CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012

    Question Mapping a linked list issue

    Hello guys,

    I am new to the forum and I kindly ask for help (excuse me for my bad language)

    I have a task to write a program Simple Diary (in C++) which has to store notes in a text file but the program have to use data structure. More over the program has to have the ability to search notes by date and print those notes, but the contents of the file must be loaded into the program each time the program starts.

    I thought to use list to store the notes into a text file and if the user wants to search, the contents of the file to be stored into another list. Then I wanted to map this list in order to be able to search notes by keyword (date) and print it on the screen if there is a match.

    The problem is that I do not know how to map a linked list. Please help me on this. (If there are posts of this kind, please point me to them.)

    Thanks in advance.

  2. #2
    Join Date
    Apr 1999

    Re: Mapping a linked list issue

    Quote Originally Posted by Symus View Post
    The problem is that I do not know how to map a linked list. Please help me on this. (If there are posts of this kind, please point me to them.)
    #include <list>
    #include <map>
    #include <string>
    #include <iostream>
    struct Note
       std::string noteMessage;
       // add more information to denote a single note
       Note(const std::string& m = ""): noteMessage(m) {}
       std::string getNote() const { return noteMessage; }
    typedef std::list< Note > NoteList;  // a linked list of Notes
    typedef std::map<int, NoteList > NoteMap;  // a map of some number to a list of Notes.
    using namespace std;
    int main()
        NoteMap nMap;
        NoteList nList;
        nList.push_back(Note("This is note 0"));
        nList.push_back(Note("This is note 1"));
        nList.push_back(Note("This is note 2"));
        nMap.insert(make_pair(0, nList));
        NoteMap::iterator it = nMap.begin();
        while ( it != nMap.end() )
           cout << "Diary number is " << it->first << endl;
           NoteList::iterator it2 = it->second.begin();
           while (it2 != it->second.end()) 
                cout <<  "Note title is " << it2->getNote() << endl;
    That entire code maps a number to a linked list of notes using the C++ standard library. Sorting and searching then becomes trivial, which is why I didn't do it above. Note that there are just a few comments in the code -- I leave it to you to look up what that code does above. If you really want to be a good C++ programmer, code as I've written must be immediately understandable, even if you never saw the code before. So you might as well learn what I wrote and how it works.

    If you can't use this type of code, the you will get little help coding a linked list yourself unless you coded it and have issues with it (and in that case, the answer would be for you to debug your code).


    Paul McKenzie

  3. #3
    Join Date
    Jul 2012

    Re: Mapping a linked list issue

    Hello Paul,

    First I would like to thank you for your reply - so thank you

    It is a little hard for me to fully understand the code above because we did not learned all the things which I see in your code. Still i got the basic idea which you represented. I have developed my own code and I had to use multimap. If it is OK with you I can show you what I did and you could check it?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width