aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework_3
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 /CST 126/Homework_3
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
Diffstat (limited to 'CST 126/Homework_3')
-rw-r--r--CST 126/Homework_3/SinglyLinkedList.hpp42
1 files changed, 42 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