aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChanin Timbal <[email protected]>2024-06-06 21:05:37 -0700
committerChanin Timbal <[email protected]>2024-06-06 21:05:37 -0700
commit7b6fb1ad48216f24a9079048da93b2bae6671253 (patch)
tree712f9f4fac6ba2cf30ec20fed0c35477e2e37bc3
parentCreated Prepend function for Homework 3 - Successful unit test for prepend fu... (diff)
downloadhomework-1-chaninnohea-7b6fb1ad48216f24a9079048da93b2bae6671253.tar.xz
homework-1-chaninnohea-7b6fb1ad48216f24a9079048da93b2bae6671253.zip
Added RemoveLast function and its unit test
-rw-r--r--CST 126/Homework_3/SinglyLinkedList.hpp42
-rw-r--r--CST 126/UnitTests/UnitTests.cpp52
2 files changed, 94 insertions, 0 deletions
diff --git a/CST 126/Homework_3/SinglyLinkedList.hpp b/CST 126/Homework_3/SinglyLinkedList.hpp
index 38c0072..1b47d99 100644
--- a/CST 126/Homework_3/SinglyLinkedList.hpp
+++ b/CST 126/Homework_3/SinglyLinkedList.hpp
@@ -58,4 +58,46 @@ bool Prepend(SinglyLinkedList* list, ListNode* node) {
return true;
}
+bool RemoveFirst(SinglyLinkedList* list) {
+ if (list->_size == 0)
+ return false;
+
+ ListNode* first = list->_head;
+
+ list->_head = list->_head->_next;
+
+ list->_size--;
+
+ delete first;
+
+ return true;
+}
+
+bool RemoveLast(SinglyLinkedList* list) {
+ if (list->_size == 0)
+ return false;
+
+ /*if (list->_size == 1)
+ {
+ delete list->_head;
+ list->_head = nullptr;
+ list->_size--;
+ return true;
+ }
+ */
+
+
+ ListNode* SecondToLast = list->_head;
+ while (SecondToLast->_next->_next != nullptr)
+ {
+ SecondToLast = SecondToLast->_next;
+ }
+
+ delete SecondToLast->_next;
+ SecondToLast->_next = nullptr;
+ list->_size--;
+
+ return true;
+}
+
#endif
diff --git a/CST 126/UnitTests/UnitTests.cpp b/CST 126/UnitTests/UnitTests.cpp
index 27ad6af..4b17624 100644
--- a/CST 126/UnitTests/UnitTests.cpp
+++ b/CST 126/UnitTests/UnitTests.cpp
@@ -66,7 +66,59 @@ namespace LinkedListUnitTests
}
}
+ TEST_METHOD(RemoveFirstLinkedList)
+ {
+ //Arrange
+ SinglyLinkedList linkedList{};
+ ListNode* node = new ListNode{ 2, nullptr };
+ ListNode* node2 = new ListNode{ 1, nullptr };
+ ListNode* node3 = new ListNode{ 3, nullptr };
+
+ //Act
+ bool success = Append(&linkedList, node);
+ success = Prepend(&linkedList, node2);
+ success = Append(&linkedList, node3);
+ success = RemoveFirst(&linkedList);
+
+ ListNode* travel = linkedList._head;
+
+ //Assert
+ for (auto i = 2; i <= 3; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+ }
+ }
+
+ TEST_METHOD(RemoveLastLinkedList)
+ {
+ //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 };
+
+ //Act
+ bool success = Append(&linkedList, node);
+ success = Append(&linkedList, node2);
+ success = Append(&linkedList, node3);
+ success = Append(&linkedList, node4);
+ success = Append(&linkedList, node5);
+ success = RemoveLast(&linkedList);
+
+ ListNode* travel = linkedList._head;
+
+ //Assert
+
+ for (auto i = 1; i <= 4; i++)
+ {
+ Assert::AreEqual(i, travel->_data);
+ travel = travel->_next;
+ }
+ }
};
}