I was given a piece of code that declares a singly list, doubly list and a vector. It fills these containers with random numbers, sorts them, then calls a function InsertNextTo. I have to implement this InsertNextTo fuction, description of which is below.
In main.cpp, implement the function insertNextTo, which takes a Container and an element, and adds the element to the container such that if it already appears in the container, it will be added next to its copy; otherwise it will be added at the end.
For example, if vector v has the elements [18, 3, 19, 6, 5, 10, 20, 17], after calling insertNextTo(v,6) it will look like this: [18, 3, 19, 6, 6, 5, 10, 20, 17].
After calling insertNextTo(v, 8), it will look like this: [18, 3, 19, 6, 6, 5, 10, 20, 17, 8].
The code is below, the relevant fucntion all the way at the end. I've written my question as a comment in the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <time.h>
#include <vector>
#include "llist.h" // singly linked list class
#include "dllist.h" // doubly linked list class
#include "timing.h"
template <class Iter>
void selectionSort(Iter, Iter); // implement
template <class Container, class T>
void insertNextTo(Container &, const T&); // implement
struct time {
uint64 start;
std::string name;
time(const std::string& str="") : start(GetTimeMs64()), name(str) {}
void getTimeElapsed() {
std::cout << name << "... " << GetTimeMs64() - start << std::endl;
}
void startAgain(const std::string& str="") {
start = GetTimeMs64();
name = str;
}
};
int main(int argc, char** argv) {
srand (time(NULL));
int sz;
std::cout << "Enter data size: ";
std::cin >> sz;
int arr1[sz];
int arr2[sz];
for (int i = 0; i < sz; i++) {
arr1[i] = rand() % 100;
arr2[i] = rand() % 100;
}
struct time t("filling list");
List<int> lst;
for (int i = 0; i < sz; i++) { lst.push_front(arr1[i]); }
t.getTimeElapsed();
t.startAgain("filling dlist");
DList<int> dlst;
for (int i = 0; i < sz; i++) { dlst.push_front(arr1[i]); }
t.getTimeElapsed();
t.startAgain("filling vector");
std::vector<int> v;
for (int i = 0; i < sz; i++) { v.push_back(arr1[i]); }
t.getTimeElapsed();
t.startAgain("sorting list");
selectionSort(lst.begin(), lst.end());
t.getTimeElapsed();
t.startAgain("sorting dlist");
selectionSort(dlst.begin(), dlst.end());
t.getTimeElapsed();
t.startAgain("sorting vector");
selectionSort(v.begin(), v.end());
t.getTimeElapsed();
t.startAgain("adding more to list");
for(int i = 0; i < sz; i++) { insertNextTo(lst, arr2[i]); }
t.getTimeElapsed();
t.startAgain("adding more to dlist");
for(int i = 0; i < sz; i++) { insertNextTo(dlst, arr2[i]); }
t.getTimeElapsed();
t.startAgain("adding more to vector");
for(int i = 0; i < sz; i++) { insertNextTo(v, arr2[i]); }
t.getTimeElapsed();
}
template <class Iter>
void selectionSort(Iter begin, Iter end)
{
Iter it, jt, minIt;
for(it = begin; it != end; ++it)
{
minIt = it;
for(jt = ++it; jt != end; ++jt)
{
if(*jt < *minIt)
minIt = jt;
}
std::swap(*it, *minIt);
}
}
template <class Container, class T>
void insertNextTo(Container &C, const T& val)
{
Container iter; // this is where im getting an error.
// what should the type of my iterator be?
// I am not allowed to include the <iterator> header file.
for(iter = C.begin(); iter != C.end(); ++iter)
{
if(*iter == val)
{
C.insert(++iter, val);
return;
}
}
C.insert(iter, val);
}
|