This lab has two parts:
Part 1
Start with your code from Lab #6. Delete all but the function that
prompts for the name of a file and reads its contents into a list.
Add a new function, Print, that takes a list of integers
and prints them to the screen. Verify that your code works by reading
the contents of the file into a list, then passing it to
Print.
Part 2
Add a new function, OrderedInsert, to your program:
// PRE: The input list is ordered from smallest to largest // POST: n has been inserted into orderedList such that it remains ordered void OrderedInsert(/*in*/ int n, /*inout*/ list<int>& orderedList)Now modify your program so that you create a sorted copy of the list containing the contents of the file. That is, declare a second list and add each item from the first list to it, one by one, via your OrderedInsert function. Print out the sorted copy as well to make sure your program works:
Enter file name: data Input values: 10 7 3 5 7 9 1 Sorted copy: 1 3 5 7 7 9 10
Note: The iterator used within the OrderedInsert
function cannot be a const_iterator since we need to do
insertions with respect to it. All other list iterators in your program
can be constant. Thus, you may want both:
typedef list<int>::const_iterator constIntItor; typedef list<int>::iterator intItor;