3. Implement the UnsortedList class to store a list of numbers that are input in
ID: 3797790 • Letter: 3
Question
3. Implement the UnsortedList class to store a list of numbers that are input into the list from data.txt.
- create a main.cpp file that gets the numbers from the file
- insert the number 7 into the list
- insert another number 300 into the list
- delete the number 6 from the list
- print out the following:
--the entire list
- the greatest
- the least
2. There should be four files, the main.cpp, UnsortedList.cpp, ItemType.h, and the output file two called outfile2.txt
- Yes you need to make your program output an "outfile2.txt"
Contents of “data.txt
10 8 250 800 -1 6
Explanation / Answer
main.cpp:
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
std::fstream myfile("data.txt");
std::ofstream myfile1("output2.txt");
int a;
while (myfile >> a)
{
mylist.push_back(a);
}
it = mylist.begin();
std::cout << ' ';
std :: cout << "Initial List:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << ' ';
std :: cout << "Insert 7:";
mylist.insert (it,7);
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << ' ';
std :: cout << "Intest 300:";
mylist.insert (it,300);
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << ' ';
std :: cout << "Delete 6:";
mylist.remove(6);
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << ' ';
std::cout << "Final list:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
myfile1 << *it << std::endl;
std::cout << ' ';
std::cout << ' ';
return 0;
}
UnsortedList.cpp:
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
std::fstream myfile("output2.txt");
int a;
int largest;
while (myfile >> a)
{
mylist.push_back(a);
}
it = mylist.begin();
for (it=mylist.begin(); it!=mylist.end(); ++it)
if (*it>largest){
largest=*it;
}
std::cout <<"Largest is:";
std::cout <<largest;
std::cout << ' ';
return 0;
}
data.txt:
10 8 250 800 -1 6
output2.txt:
10 8 250 800 -1 7 300
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.