Commit 702f90c78935a47ce646f3d12b02bac9cd100552

Authored by Grzegorz Jabłoński
1 parent 27cf1cf7

Added example of list with iterator

Showing 23 changed files with 76 additions and 0 deletions
examples09/03-list/list.cpp 0 → 100644
  1 +#include <iostream>
  2 +#include <stddef.h>
  3 +
  4 +class List {
  5 + struct BaseNode {
  6 + BaseNode *prev;
  7 + BaseNode *next;
  8 + };
  9 +
  10 + struct Node : public BaseNode {
  11 + int data;
  12 + };
  13 +
  14 + BaseNode FirstLast {.prev = &FirstLast, .next = &FirstLast};
  15 +
  16 +public:
  17 +
  18 +// List() {
  19 +// FirstLast.next = &FirstLast;
  20 +// FirstLast.prev = &FirstLast;
  21 +// };
  22 +
  23 + ~List() {
  24 + BaseNode *ptr = FirstLast.next;
  25 + while (ptr != &FirstLast) {
  26 + BaseNode *next = ptr->next;
  27 + delete ptr;
  28 + ptr = next;
  29 + }
  30 + }
  31 +
  32 + class Iterator {
  33 + BaseNode *ptr;
  34 +
  35 + public:
  36 + Iterator(BaseNode *p) : ptr(p){};
  37 +
  38 + int &operator*() { return static_cast<Node *>(ptr)->data; }
  39 + Iterator &operator++() {
  40 + ptr = ptr->next;
  41 + return *this;
  42 + }
  43 + bool operator!=(const Iterator &o) { return ptr != o.ptr; }
  44 + friend class List;
  45 + };
  46 +
  47 + void insert(Iterator i, int v) {
  48 + Node *n = new Node;
  49 + n->prev = i.ptr->prev;
  50 + n->next = i.ptr;
  51 + i.ptr->prev->next = n;
  52 + i.ptr->prev = n;
  53 + n->data = v;
  54 + }
  55 +
  56 + Iterator begin() { return Iterator(FirstLast.next); };
  57 + Iterator end() { return Iterator(&FirstLast); };
  58 +};
  59 +
  60 +int main() {
  61 + List c;
  62 + c.insert(c.begin(), 1);
  63 + c.insert(c.begin(), 2);
  64 + c.insert(c.begin(), 3);
  65 +
  66 + for (auto i : c)
  67 + std::cout << i << std::endl;
  68 +}
... ...
examples09/03-list/makefile 0 → 100644
  1 +all: list
  2 +
  3 +list: list.cpp
  4 + g++ -std=c++2a -g -Wall -pedantic $^ -o $@
  5 +.PHONY: clean
  6 +
  7 +clean:
  8 + -rm list
0 9 \ No newline at end of file
... ...
examples09/03-algorithm/algo1.cpp renamed to examples09/04-algorithm/algo1.cpp
examples09/03-algorithm/algo2.cpp renamed to examples09/04-algorithm/algo2.cpp
examples09/03-algorithm/makefile renamed to examples09/04-algorithm/makefile
examples09/04-functionobject/fun1.cpp renamed to examples09/05-functionobject/fun1.cpp
examples09/04-functionobject/fun2.cpp renamed to examples09/05-functionobject/fun2.cpp
examples09/04-functionobject/fun3.cpp renamed to examples09/05-functionobject/fun3.cpp
examples09/04-functionobject/makefile renamed to examples09/05-functionobject/makefile
examples09/05-unordered_set/employee.h renamed to examples09/06-unordered_set/employee.h
examples09/05-unordered_set/makefile renamed to examples09/06-unordered_set/makefile
examples09/05-unordered_set/unordered_set.cpp renamed to examples09/06-unordered_set/unordered_set.cpp
examples09/05-unordered_set/unordered_set2.cpp renamed to examples09/06-unordered_set/unordered_set2.cpp
examples09/06-closures/fun1.cpp renamed to examples09/07-closures/fun1.cpp
examples09/06-closures/fun2.cpp renamed to examples09/07-closures/fun2.cpp
examples09/06-closures/fun3.cpp renamed to examples09/07-closures/fun3.cpp
examples09/06-closures/fun4.cpp renamed to examples09/07-closures/fun4.cpp
examples09/06-closures/fun5.cpp renamed to examples09/07-closures/fun5.cpp
examples09/06-closures/makefile renamed to examples09/07-closures/makefile
examples09/07-fifo/fifo1_bad.cpp renamed to examples09/08-fifo/fifo1_bad.cpp
examples09/07-fifo/fifo2.cpp renamed to examples09/08-fifo/fifo2.cpp
examples09/07-fifo/fifo3.cpp renamed to examples09/08-fifo/fifo3.cpp
examples09/07-fifo/makefile renamed to examples09/08-fifo/makefile