Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Linked Lists

$
0
0

I am currently working on a code using Linked List. I would like my code to do the following:

1. User Enters integers between 1-60 in the first list
2. The application to check for all the repeated entries, remove them from the initial list and store them on a second list

A couple of friends have helped me get to the following code.

#include <list>
#include <map>
#include <iostream>
#include <algorithm>

typedef std::list<int> IntList;
typedef std::map<int, int> IntMap;

using namespace std;

int main()
{
    IntList myList;
    IntList myDups;
    IntMap myMap;

   
    int num;
    bool numOk;
    do
    {
          cin >> num;
          numOk = ( num >= 1 && num <= 60 );
          if ( numOk ) 
          {
               // add to the linked list
               myList.push_back(num);

              // record this entry in the map
               myMap[num]++;
          } 
   }  while (numOk);
   IntMap::iterator it = myMap.begin();
   while ( it != myMap.end() )
   {
       if ( it->second > 1 )
      {
           myDups.push_back(it->first);

          
           myList.erase( find(myList.begin(), myList.end(), it->first));
       }++it;
   }
}

Now that the first list has only one copy of each entry, the application should randomly select five nodes (using a loop). After the selection is made, the stored information on the node should be shown and that node to be deleted (but temporarily).  Once the data from the fifth node is shown, all the deleted nodes to be re-stored to the original, without creating a duplicate. If the user wishes to select another set of (5) nodes, he/she has the liberty to do so.

Together with this, the statistics of the second list to be shown in a summary.


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>