From 4646ce2a21cd6cba10af60cc3e821eb83ee7e5f9 Mon Sep 17 00:00:00 2001 From: Chanin Timbal Date: Wed, 5 Jun 2024 15:25:47 -0700 Subject: Created node struct and singlylinkedlist struct, and append function. Also complete Unit Testing for this section. --- CST 126/Homework_3/SinglyLinkedList.hpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CST 126/Homework_3/SinglyLinkedList.hpp (limited to 'CST 126/Homework_3/SinglyLinkedList.hpp') diff --git a/CST 126/Homework_3/SinglyLinkedList.hpp b/CST 126/Homework_3/SinglyLinkedList.hpp new file mode 100644 index 0000000..8fbf2b2 --- /dev/null +++ b/CST 126/Homework_3/SinglyLinkedList.hpp @@ -0,0 +1,45 @@ +#ifndef SINGLY_LINKED_LIST_HPP +#define SINGLY_LINKED_LIST_HPP + + +struct ListNode { + int _data = 0; + ListNode* _next = nullptr; +}; + +struct SinglyLinkedList { + size_t _size; + ListNode* _head = nullptr; +}; + + +//SinglyLinkedList singlylinkedlist{}; +//ListNode listnode{}; +// +//listnode._data = 1; +//singlylinkedlist._head = &listnode; + +bool Append(SinglyLinkedList* list, ListNode* node) { + + //if empty + if (list->_size == 0) + { + list->_head = node; + list->_size++; + return true; + } + + //if not empty + ListNode* travel = nullptr; + for (travel = list->_head; travel->_next != nullptr;) + { + travel = travel->_next; + } + + travel->_next = node; + return true; +} + + + +#endif -- cgit v1.2.3