#include #include #include #include // for smart pointers. using namespace std; struct NodeT { int data; shared_ptr next; }; // typedef shared_ptr NodeP; using NodeP = shared_ptr; // recursive version /* NodeP Insert(NodeP ptr, int d) { if (ptr == nullptr or ptr->data > d) { NodeP tmp = make_shared(); tmp->data = d; tmp->next = ptr; return tmp; } else { ptr->next = Insert(ptr->next, d); return ptr; } } */ void Insert(NodeP & ptr, int d) { NodeP tmp, current, trail; tmp = make_shared(); tmp->data = d; if (ptr == nullptr or ptr->data > d) { tmp->next = ptr; ptr = tmp; } else { current = ptr->next; trail = ptr; while (current != nullptr and current->data < d) { trail = current; current = current->next; } tmp->next = trail->next; trail->next = tmp; } } bool Comp(vector & ary, NodeP list) { NodeP listTmp { list }; auto vectorTmp { begin(ary) }; while (vectorTmp != end(ary) and listTmp != nullptr) { if (*vectorTmp != listTmp->data) { return false; } vectorTmp++; listTmp = listTmp ->next; } return (listTmp == nullptr and vectorTmp == end(ary)); } int main() { srand(static_cast(time(nullptr))); int data; vectorcmp; NodeP list; NodeP tmp; for(int i=0; i < 10; i++) { data = rand() % 1000; //list = Insert(list, data); Insert(list, data); cmp.push_back(data); } sort(begin(cmp), end(cmp)); if (!Comp(cmp, list)) { cout << "Error in the data " << endl; } for(tmp = list; tmp != nullptr; tmp=tmp->next ) { cout << tmp->data << " "; } cout << endl; return 0; }