Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Node {
- string data;
- Node* next;
- Node(const string& line) : data(line), next(nullptr) {}
- private:
- friend ostream& operator<<(ostream& out, const Node& node) {
- return out << node.data;
- }
- };
- class List {
- public:
- class Iterator {
- public:
- explicit Iterator(Node* ptr) : curr(ptr) {}
- Iterator& operator++() {
- curr = curr->next;
- return *this;
- }
- string& operator*() {
- return curr->data;
- }
- const string& operator*()const {
- return curr->data;
- }
- Node* operator->() {
- return curr;
- }
- Node* operator->()const {
- return curr;
- }
- bool operator==(const Iterator& iter) {
- return curr == iter.curr;
- }
- bool operator!=(const Iterator& iter) {
- return curr != iter.curr;
- }
- private:
- Node* curr;
- };
- List() : head(nullptr), tail(nullptr) {}
- void add(const string& value) {
- auto node = new Node{ value };
- if (tail) {
- tail->next = node;
- tail = node;
- } else {
- head = tail = node;
- }
- }
- Iterator first() {
- return Iterator(head);
- }
- Iterator last() {
- return Iterator(tail);
- }
- void node_swap(Iterator& a, Iterator& b) {
- iter_swap(a, b);
- }
- Iterator begin()const {
- return Iterator(head);
- }
- Iterator end()const {
- return head != tail ? Iterator(tail->next) : Iterator(tail);
- }
- private:
- Node* head;
- Node* tail;
- };
- void show(const List& list) {
- for (auto& item : list) cout << item << '\n';
- puts("-----");
- }
- int main() {
- List list;
- list.add("first");
- list.add("...");
- list.add("last");
- show(list);
- auto first = list.first();
- auto last = list.last();
- list.node_swap(first, last);
- show(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement