aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChanin Timbal <[email protected]>2024-06-09 14:47:58 -0700
committerChanin Timbal <[email protected]>2024-06-09 14:47:58 -0700
commit7e19095215e616f00cc6e0b1a35610eebabaeafe (patch)
tree5a7c3d74efbd5670b82843011ccb3e88d1464fec
parentCompleted Functions and Unit tests for Prepend, Insert Before, Insert Afterm ... (diff)
downloadhomework-1-chaninnohea-7e19095215e616f00cc6e0b1a35610eebabaeafe.tar.xz
homework-1-chaninnohea-7e19095215e616f00cc6e0b1a35610eebabaeafe.zip
All functions complete and Unit test complete for each function.
-rw-r--r--CST 126/Homework_3/SinglyLinkedList.hpp29
-rw-r--r--CST 126/UnitTests/UnitTests.cpp36
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);
+ }
+
};
}