From 1f87bd00ad5ed35cba8a2601c3c8a8c50e16d6ab Mon Sep 17 00:00:00 2001 From: askiiart Date: Fri, 23 Feb 2024 12:49:00 -0600 Subject: [PATCH] Add initial code --- labs/cosc-2436/4.14/C++/Node.h | 61 ++++++++++++++++++++ labs/cosc-2436/4.14/C++/SortedNumberList.h | 32 +++++++++++ labs/cosc-2436/4.14/C++/main.cpp | 67 ++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 labs/cosc-2436/4.14/C++/Node.h create mode 100644 labs/cosc-2436/4.14/C++/SortedNumberList.h create mode 100644 labs/cosc-2436/4.14/C++/main.cpp diff --git a/labs/cosc-2436/4.14/C++/Node.h b/labs/cosc-2436/4.14/C++/Node.h new file mode 100644 index 0000000..ccc63b4 --- /dev/null +++ b/labs/cosc-2436/4.14/C++/Node.h @@ -0,0 +1,61 @@ +#ifndef NODE_H +#define NODE_H + +class Node { +protected: + double data; + Node* next; + Node* previous; + +public: + // Constructs this node with the specified numerical data value. The next + // and previous pointers are each assigned nullptr. + Node(double initialData) { + data = initialData; + next = nullptr; + previous = nullptr; + } + + // Constructs this node with the specified numerical data value, next + // pointer, and previous pointer. + Node(double initialData, Node* nextNode, Node* previousNode) { + data = initialData; + next = nextNode; + previous = previousNode; + } + + virtual ~Node() { + } + + // Returns this node's data. + virtual double GetData() { + return data; + } + + // Sets this node's data. + virtual void SetData(double newData) { + data = newData; + } + + // Gets this node's next pointer. + virtual Node* GetNext() { + return next; + } + + // Sets this node's next pointer. + virtual void SetNext(Node* newNext) { + next = newNext; + } + + // Gets this node's previous pointer. + virtual Node* GetPrevious() { + return previous; + } + + // Sets this node's previous pointer. + virtual void SetPrevious(Node* newPrevious) { + previous = newPrevious; + } +}; + +#endif diff --git a/labs/cosc-2436/4.14/C++/SortedNumberList.h b/labs/cosc-2436/4.14/C++/SortedNumberList.h new file mode 100644 index 0000000..783df73 --- /dev/null +++ b/labs/cosc-2436/4.14/C++/SortedNumberList.h @@ -0,0 +1,32 @@ +#ifndef SORTEDNUMBERLIST_H +#define SORTEDNUMBERLIST_H +#include "Node.h" + +class SortedNumberList { +private: + // Optional: Add any desired private functions here + +public: + Node* head; + Node* tail; + + SortedNumberList() { + head = nullptr; + tail = nullptr; + } + + // Inserts the number into the list in the correct position such that the + // list remains sorted in ascending order. + void Insert(double number) { + // Your code here + } + + // Removes the node with the specified number value from the list. Returns + // true if the node is found and removed, false otherwise. + bool Remove(double number) { + // Your code here (remove placeholder line below) + return false; + } +}; + +#endif diff --git a/labs/cosc-2436/4.14/C++/main.cpp b/labs/cosc-2436/4.14/C++/main.cpp new file mode 100644 index 0000000..8cb527a --- /dev/null +++ b/labs/cosc-2436/4.14/C++/main.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include "Node.h" +#include "SortedNumberList.h" +using namespace std; + +void PrintList(SortedNumberList& list); +vector SpaceSplit(string source); + +int main(int argc, char *argv[]) { + // Read the line of input numbers + string inputLine; + getline(cin, inputLine); + + // Split on space character + vector terms = SpaceSplit(inputLine); + + // Insert each value and show the sorted list's contents after each insertion + SortedNumberList list; + for (auto term : terms) { + double number = stod(term); + cout << "List after inserting " << number << ": " << endl; + list.Insert(number); + PrintList(list); + } + + /* + // Read the input line with numbers to remove + getline(cin, inputLine); + terms = SpaceSplit(inputLine); + + // Remove each value + for (auto term : terms) { + double number = stod(term); + cout << "List after removing " << number << ": " << endl; + list.Remove(number); + PrintList(list); + } + */ + + return 0; +} + +// Prints the SortedNumberList's contents, in order from head to tail +void PrintList(SortedNumberList& list) { + Node* node = list.head; + while (node) { + cout << node->GetData() << " "; + node = node->GetNext(); + } + cout << endl; +} + +// Splits a string at each space character, adding each substring to the vector +vector SpaceSplit(string source) { + vector result; + size_t start = 0; + for (size_t i = 0; i < source.length(); i++) { + if (' ' == source[i]) { + result.push_back(source.substr(start, i - start)); + start = i + 1; + } + } + result.push_back(source.substr(start)); + return result; +}