aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework_3
diff options
context:
space:
mode:
authorChanin Timbal <[email protected]>2024-06-06 20:11:53 -0700
committerChanin Timbal <[email protected]>2024-06-06 20:11:53 -0700
commita1d340b239eeb75159640e6b3e2bdb5586251e55 (patch)
tree5d1a00bd6d9aa56b711e18b1476dd73f1ac53dd2 /CST 126/Homework_3
parentCreated node struct and singlylinkedlist struct, and append function. Also co... (diff)
downloadhomework-1-chaninnohea-a1d340b239eeb75159640e6b3e2bdb5586251e55.tar.xz
homework-1-chaninnohea-a1d340b239eeb75159640e6b3e2bdb5586251e55.zip
Created Prepend function for Homework 3 - Successful unit test for prepend function
Diffstat (limited to 'CST 126/Homework_3')
-rw-r--r--CST 126/Homework_3/SinglyLinkedList.hpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/CST 126/Homework_3/SinglyLinkedList.hpp b/CST 126/Homework_3/SinglyLinkedList.hpp
index 8fbf2b2..38c0072 100644
--- a/CST 126/Homework_3/SinglyLinkedList.hpp
+++ b/CST 126/Homework_3/SinglyLinkedList.hpp
@@ -40,6 +40,22 @@ bool Append(SinglyLinkedList* list, ListNode* node) {
return true;
}
+bool Prepend(SinglyLinkedList* list, ListNode* node) {
+ //if empty
+ if (list->_size == 0)
+ {
+ list->_head = node;
+ list->_size++;
+ return true;
+ }
+
+ //if not empty
+ ListNode* travel = list->_head;
+ list->_head = node;
+ node->_next = travel;
+
+ return true;
+}
#endif