Skip to content

Commit

Permalink
Added the simplelist header file. This version should compile.
Browse files Browse the repository at this point in the history
  • Loading branch information
AverageGuy committed Jul 5, 2024
1 parent 80c4191 commit 588e303
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions SimpleList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef SIMPLE_LIST_H
#define SIMPLE_LIST_H

template<typename T>
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 &current->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

0 comments on commit 588e303

Please sign in to comment.