#include <iostream>
template <typename T>
class Node {
public:
T data;
Node* next;
Node(T val) : data(val), next(nullptr) {}
};
template <typename T>
class LinkedList {
private:
Node<T>* head;
public:
LinkedList() : head(nullptr) {}
void insert(T val) {
Node<T>* newNode = new Node<T>(val);
newNode->next = head;
head = newNode;
}
class Iterator {
private:
Node<T>* current;
public:
Iterator(Node<T>* start) : current(start) {}
T& operator*() const {
return current->data;
}
Iterator& operator++() {
current = current->next;
return *this;
}
bool operator!=(const Iterator& other) const {
return current != other.current;
}
};
Iterator begin() const {
return Iterator(head);
}
Iterator end() const {
return Iterator(nullptr);
}
};
int main() {
LinkedList<int> myList;
myList.insert(1);
myList.insert(2);
myList.insert(3);
for (LinkedList<int>::Iterator it = myList.begin(); it != myList.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}