From 7d1e737ec906d4c75aa9c104198959700096dcb4 Mon Sep 17 00:00:00 2001 From: rPatrickWarner Date: Sat, 11 May 2024 11:27:43 -0700 Subject: created insert and deletion functions --- CST 126/Homework2/nextdest.txt | Bin 128 -> 93 bytes CST 126/Homework3/SinglyLinkedList.hpp | 65 ++++++++++++++++++++++++++++----- CST 126/Homework3/main.cpp | 2 - 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/CST 126/Homework2/nextdest.txt b/CST 126/Homework2/nextdest.txt index 0be8381..1efd69c 100644 Binary files a/CST 126/Homework2/nextdest.txt and b/CST 126/Homework2/nextdest.txt differ diff --git a/CST 126/Homework3/SinglyLinkedList.hpp b/CST 126/Homework3/SinglyLinkedList.hpp index 22118d6..1d30f0d 100644 --- a/CST 126/Homework3/SinglyLinkedList.hpp +++ b/CST 126/Homework3/SinglyLinkedList.hpp @@ -1,6 +1,6 @@ #ifndef SINGLY_LINKED_LIST_HPP #define SINGLY_LINKED_LIST_HPP - +#include template struct ListNode { @@ -20,30 +20,77 @@ struct SinglyLinkedList template inline bool Append(SinglyLinkedList* list, ListNode* node) { - - //we have list - //we need to add node to end - //Empty set first node as head if (list->_size == 0) { list->_head = node; list->_size++; return true; } - - //ifnotempty - ListNode* TravelNode = nullptr; + ListNode* TraverseNode = nullptr; for (TravelNode = list->_head; TravelNode->_next!= nullptr;) { TravelNode = TravelNode->_next; } TravelNode->_next = node; - return true;//ifsuccess + return true; } +template +inline bool Prepend(SinglyLinkedList* list, ListNode* node) +{ + if (list->_size == 0) + { + list->_head = node; + list->_size++; + return true; + } + node->_next = list->_head; + list->_head = node; + return true; + +} +template +inline bool RemoveFirst(SinglyLinkedList* list, ListNode* node) +{ + if (list->_size == 0) + { + std::cout << "Empty list... there is nothing to delete!" << std::endl; + return true; + } + ListNode* Temp = list->_head; + list->_head = node->_next; + list->_size--; + delete Temp; + return true; +} +template +inline bool RemoveLast(SinglyLinkedList* list, ListNode* node) +{ + if (list->_size == 0) + { + std::cout << "Empty list... there is nothing to delete!" << std::endl; + return true; + } + if (list->size == 1) + { + delete list->_head; + list->_head = nullptr; + list->_size--; + return true; + } + ListNode* TraverseNode = list->_head; + while (TraverseNode->_next->_next != nullptr) + { + TraverseNode = TraverseNode->_next; + } + delete TraverseNode->_next; + TraverseNode->_next = nullptr; + list->_size--; + return true; +} #endif \ No newline at end of file diff --git a/CST 126/Homework3/main.cpp b/CST 126/Homework3/main.cpp index 2536baa..9939411 100644 --- a/CST 126/Homework3/main.cpp +++ b/CST 126/Homework3/main.cpp @@ -11,7 +11,5 @@ int main(const int argc, char* argv[]) - - return 0; } \ No newline at end of file -- cgit v1.2.3