C++ Programming assignment Implement the following specification of UnsortedType
ID: 640030 • Letter: C
Question
C++ Programming assignment
Implement the following specification of UnsortedType using a circular linked list as the implementation structure.
template <class ItemType>
struct NodeType;
/* Assumption: ItemType is a type for which the operators
"<" and "==" are defined
Explanation / Answer
#include using namespace std; template struct Node { T data; Node * next; Node(T data) : data(data), next(NULL) {} }; template class CircularLinkedList { public: CircularLinkedList() : head(NULL) {} ~CircularLinkedList(); void addNode(T data); void deleteNode(T data); template friend std::ostream & operatornext; delete(t); } delete tmp; head = NULL; } } template void CircularLinkedList::addNode(T data) { Node * t = new Node(data); if (head == NULL) { t->next = t; head = t; return; } Node * tmp = head; while (tmp->next != head) { tmp = tmp->next; } tmp->next = t; t->next = head; } template void CircularLinkedList::deleteNode(T data) { Node * tmp = head; Node * prev = NULL; while (tmp->next != head) { if (tmp->data == data) break; prev = tmp; tmp = tmp->next; } if (tmp == head) { while (tmp->next != head) { tmp = tmp->next; } tmp->next = head->next; delete head; head = tmp->next; } else { prev->next = tmp->next; delete tmp; } } template std::ostream & operatorRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.