bool SinglyLinkedList<Type>::remove(int key) { if (size <= 0) { return false; } LinkedListNode<Type>* pLoc = head; LinkedListNode<Type>* pPre = nullptr; while (pLoc->next != nullptr && pLoc->getKey() != key) { Efficiency::totalDataStructureOperations++; pPre = pLoc; pLoc = pLoc->next; } if (pLoc->getKey() != key) return false; if (pLoc == head) { Efficiency::totalDataStructureOperations++; head = pLoc->next; size--; return true; } pPre->next = pLoc->next; if (pPre->next == nullptr) rear = pPre; delete pLoc; size--; Efficiency::totalDataStructureOperations++; return true; }
Type SinglyLinkedList<Type>::get(int key) { LinkedListNode<Type>* pLoc = head; while (pLoc != nullptr) { if (pLoc->getKey() == key) { Efficiency::totalDataStructureOperations++; return pLoc->getData(); } Efficiency::totalDataStructureOperations++; pLoc = pLoc->next; } throw "not found"; }