diff options
| -rw-r--r-- | CST 126/Homework_3/SinglyLinkedList.hpp | 29 | ||||
| -rw-r--r-- | CST 126/UnitTests/UnitTests.cpp | 36 |
2 files changed, 65 insertions, 0 deletions
diff --git a/CST 126/Homework_3/SinglyLinkedList.hpp b/CST 126/Homework_3/SinglyLinkedList.hpp index 75f9530..9046d58 100644 --- a/CST 126/Homework_3/SinglyLinkedList.hpp +++ b/CST 126/Homework_3/SinglyLinkedList.hpp @@ -37,6 +37,7 @@ bool Append(SinglyLinkedList* list, ListNode* node) { } travel->_next = node; + list->_size++; return true; } @@ -55,6 +56,7 @@ bool Prepend(SinglyLinkedList* list, ListNode* node) { list->_head = node; node->_next = travel; + list->_size++; return true; } @@ -120,6 +122,7 @@ bool InsertAfter(SinglyLinkedList* list, ListNode* nodeBefore, ListNode* nodeToI nodeToInsert->_next = nodeBefore->_next; nodeBefore->_next = nodeToInsert; + list->_size++; return true; } @@ -147,6 +150,7 @@ bool InsertBefore(SinglyLinkedList* list, ListNode* node, ListNode* nodeToInsert nodeToInsert->_next = node; temp->_next = nodeToInsert; + list->_size++; return true; } @@ -172,7 +176,32 @@ bool Clear(SinglyLinkedList* list) return true; } +bool ExtractFrom(SinglyLinkedList* list, ListNode* NodetoExtract) +{ + if (list->_size == 0 || NodetoExtract == nullptr) + return false; + + if (NodetoExtract == list->_head) + { + list->_head = NodetoExtract->_next; + delete NodetoExtract; + list->_size--; + return true; + } + + ListNode* NodeBefore = list->_head; + while (NodeBefore->_next != NodetoExtract) + { + NodeBefore = NodeBefore->_next; + } + + NodeBefore->_next = NodetoExtract->_next; + delete NodetoExtract; + list->_size--; + + return true; +} #endif diff --git a/CST 126/UnitTests/UnitTests.cpp b/CST 126/UnitTests/UnitTests.cpp index f4f3683..78eee5d 100644 --- a/CST 126/UnitTests/UnitTests.cpp +++ b/CST 126/UnitTests/UnitTests.cpp @@ -213,5 +213,41 @@ namespace LinkedListUnitTests //Assert Assert::AreEqual(0ull, linkedList._size); } + + TEST_METHOD(ExtractFromLinkedList) + { + //Arrange + SinglyLinkedList linkedList{}; + ListNode* node = new ListNode{ 1, nullptr }; + ListNode* node2 = new ListNode{ 2, nullptr }; + ListNode* node3 = new ListNode{ 3, nullptr }; + ListNode* node4 = new ListNode{ 4, nullptr }; + ListNode* node5 = new ListNode{ 5, nullptr }; + ListNode* node6 = new ListNode{ 6, nullptr }; + + //Act + bool success = Append(&linkedList, node); + success = Append(&linkedList, node2); + success = Append(&linkedList, node3); + success = Append(&linkedList, node4); + success = Append(&linkedList, node6); + success = InsertBefore(&linkedList, node6, node5); + + //Assert + + ListNode* travel = linkedList._head; + + for (auto i = 1; i <= 6; i++) + { + Assert::AreEqual(i, travel->_data); + travel = travel->_next; + } + + + success = ExtractFrom (&linkedList, node4); + + Assert::AreEqual(5ull, linkedList._size); + } + }; } |