If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Linked List - Random Nodes

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

1. User Enter integers between 1-60 (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
3. Now that the first list has only one copy of each entry, the application should randomly select a node (using for loop multiple times). After the selection is made, the stored information on the node should be shown and that node to be deleted (but temporarily)
4. After the required number of (5) node data has been printed, all the temporarily deleted nodes to be reinstored.
5. Once thats done, the details of the second list to printed in a summary form i.e frequency of each integer entered.

The above is an example of using the C++ library to implement the first two requirements. There are no memory leaks, no calls to operator new, etc. It isn't even the greatest of code using the standard library, but it worked the first time I tried it. From here, you can implement the rest of your requirements, but think on a much higher-level, and not get bogged down in the weeds of dynamic memory allocation, writing your own containers, etc. As a matter of fact, requirement #5 on your list is taken care of since the map has details of all the entries, including the number of times a number was entered.

From the code above, all that is needed is to know what a std::list is, what a std::map does, what iterators are, what algorithms such as find() do, etc. All of these are basic requirements to be a good C++ programmer in this day and age of C++ programming. Unless you're a student and this is a homework assignment, let the students (or experienced C++ STL library developers) worry about coding linked list classes.

Re: Linked List - Random Nodes

1. re-write the numbers in ascending order
2. cancel out all the numbers that have been repeated and noting them into another piece of paper. keeping in mind that one of the repeated numbers is left on the first piece of paper. re-write the numbers that have not been cancelled
3. Tally all the numbers and build up a stat table for all the numbers on the second piece of paper (*with the repeated numbers)

Re: Linked List - Random Nodes

Originally Posted by rockx

1. re-write the numbers in ascending order
2. cancel out all the numbers that have been repeated and noting them into another piece of paper. keeping in mind that one of the repeated numbers is left on the first piece of paper. re-write the numbers that have not been cancelled
3. Tally all the numbers and build up a stat table for all the numbers on the second piece of paper (*with the repeated numbers)

Okay, that's one approach. Another that you may find easier is just to visit the first node, then read the rest of the list removing any nodes that have the same value and putting them in the second list. Repeat for the next node.

Whichever approach you go with, once you have a plan, then you can think about how you need to implement it in code. Going with your approach, step one is to sort the list.