-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the simplelist header file. This version should compile.
- Loading branch information
1 parent
80c4191
commit 588e303
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ¤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 |