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