/* linkListT.h * * This is a class for a sorted linked list of type * "LLT". // The data type LLT must allow a > comparison. */ #include #include template class LinkedList { private: struct node { LLT info; node* next; }; typedef node* nodeptr; nodeptr start; int count; public: LinkedList() // Constructor { start = NULL; count = 0; } ~LinkedList() // Destructor { nodeptr p = start, n; while (p != NULL) { n = p; p = p->next; delete n; } } void AddNode(LLT x); void DeleteNode(LLT x); void ShowNodes(); bool IsInList(LLT x); int Size(); }; template void LinkedList::AddNode(LLT x) { nodeptr n, prev, curr; n = new node; n->info = x; count++; if (start == NULL) { start = n; n->next = NULL; } else { curr = start; while ( curr != NULL && x > curr->info ) { prev = curr; curr = curr->next; } if (curr == start) { n->next = start; start = n; } else { prev->next = n; n->next = curr; } } } template void LinkedList::DeleteNode(LLT x) { nodeptr prev, curr; curr = start; while (curr != NULL && x > curr->info) { prev = curr; curr = curr->next; } if (x == curr->info) { if ( curr == start ) start = start->next; else prev->next = curr->next; delete curr; count--; } } template void LinkedList::ShowNodes() { nodeptr p = start; while (p != NULL) { cout << p->info << endl; p = p->next; } } template bool LinkedList::IsInList(LLT x) { nodeptr p = start; while (p != NULL && x > p->info) p = p->next; return (x == p->info); } template int LinkedList::Size() { return count; }