#include using namespace std; template class Node { public: int Value; Node *Next; Node(T value) : Value(value), Next(NULL) { } }; template void PrintNode(Node *node) { while (node != NULL) { cout << node->Value << " - > "; node = node->Next; } cout << "NULL" << endl; } template class LinkedList { private: int myCount; public: Node *Head; Node *Tail; LinkedList(); Node *Get(int index); //Insert Operations void InsertHead(T val); void InsertTail(T val); void Insert(int index, T val); //Remove operations void RemoveHead(); void RemoveTail(); void Remove(int index); //Search operation int Search(T val); // Addition Operations int Count(); void PrintList(); }; template Node *LinkedList::Get(int index) { if (index < 0 || index > myCount) { return NULL; } Node *node = Head(); for (int i = 0; i < index; ++i) { node = node->Next; } return node; } template void LinkedList::InsertHead(T val) { Node *node = new Node(val); node->Next = Head; Head = node; if (myCount == 0) { Tail = Head; } myCount++; } template void LinkedList::InsertTail(T val) { if (myCount == 0) { InsertHead(val); return; } Node *node = new Node(val); Tail->Next = node; Tail = node; myCount++; } template void LinkedList::Insert(int index, T val) { if (index < 0 || index > myCount) { return; } if (index == 0) { InsertHead(val); return; } else if (index == myCount) { InsertTail(val); return; } Node *prevNode = Head; for (int i = 0; i < index - 1; ++i) { prevNode = prevNode->Next; } Node *nextNode = prevNode->Next; Node *node = new Node(val); node -> Next = nextNode; prevNode -> Next = node; myCount++; } template int LinkedList::Search(T val) { if (myCount == 0) { return -1; } int index = 0; Node * node = Head; while (node->Value != val) { index++; node = node-> Next; if (node == NULL) { return -1; } } return index; } template void LinkedList::RemoveHead() { if (myCount == 0) { return; } Node * node = Head; Head = Head -> Next; delete node; myCount--; } template void LinkedList::RemoveTail() { if (myCount == 0) { return; } if (myCount == 1) { RemoveHead(); return; } Node * prevNode = Head; Node * node = Head->Next; while (node->Next != NULL) { prevNode = prevNode->Next; node = node->Next; } prevNode->Next = NULL; Tail = prevNode; delete node; myCount--; } template void LinkedList::Remove(int index) { if (myCount == 0) { return; } if (index < 0 || index >= myCount) { return; } if (index == 0) { RemoveHead(); return; } else if (index == myCount - 1) { RemoveTail(); return; } Node * prevNode = Head; for (int i = 0; i < index - 1; ++i) { prevNode = prevNode->Next; } Node * node = prevNode -> Next; Node * nextNode = node ->Next; prevNode->Next = nextNode; delete node; myCount--; } int main() { Node *node1 = new Node(3.14); Node *node2 = new Node(6.92); Node *node3 = new Node(9.25); node1->Next = node2; node2->Next = node3; PrintNode(node1); return 0; }