From 588e303cd56b1c215497e203d5647de24a3fff8b Mon Sep 17 00:00:00 2001 From: AverageGuy Date: Fri, 5 Jul 2024 05:27:47 -0400 Subject: [PATCH] Added the simplelist header file. This version should compile. --- SimpleList.h | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 SimpleList.h diff --git a/SimpleList.h b/SimpleList.h new file mode 100644 index 0000000..8e2c9d5 --- /dev/null +++ b/SimpleList.h @@ -0,0 +1,104 @@ +#ifndef SIMPLE_LIST_H +#define SIMPLE_LIST_H + +template +class SimpleList { +private: + struct Node { + T data; + Node* next; + + Node(const T& value) : data(value), next(nullptr) {} + }; + + Node* head; + Node* tail; + Node* read_position; + size_t size; + +public: + SimpleList() : head(nullptr), tail(nullptr), read_position(nullptr), size(0) {} + + ~SimpleList() { + clear(); + } + + void push_back(const T& value) { + Node* new_node = new Node(value); + if (!head) { + head = tail = read_position = new_node; + } else { + tail->next = new_node; + tail = new_node; + } + ++size; + } + + void pop_front() { + if (head) { + Node* temp = head; + head = head->next; + if (read_position == temp) { + read_position = head; + } + delete temp; + --size; + if (!head) { + tail = nullptr; + read_position = nullptr; + } + } + } + + T* read() { + if (read_position) { + T* value = &(read_position->data); + read_position = read_position->next; + return value; + } + return nullptr; + } + + void rewind() { + read_position = head; + } + + bool is_exhausted() const { + return read_position == nullptr; + } + + size_t get_size() const { + return size; + } + + void clear() { + while (head) { + Node* temp = head; + head = head->next; + delete temp; + } + tail = nullptr; + read_position = nullptr; + size = 0; + } + + class iterator { + private: + Node* current; + public: + iterator() : current(nullptr) {} // Add this line: default constructor + iterator(Node* node) : current(node) {} + + T& operator*() { return current->data; } + T* operator->() { return ¤t->data; } + iterator& operator++() { if (current) current = current->next; return *this; } + bool operator!=(const iterator& other) const { return current != other.current; } + bool operator==(const iterator& other) const { return current == other.current; } + }; + + // Add these methods to the SimpleList class if they're not already present + iterator begin() { return iterator(head); } + iterator end() { return iterator(nullptr); } +}; + +#endif // SIMPLE_LIST_H