1 /* 2 * intlist.cpp 3 * 4 * Beispielprogramm eingebettete Klassen. 5 * 6 * Autor: H.Drachenfels 7 * Erstellt am: 14.3.2019 8 */
9 #include "intlist.h"
10
11 //----------------------------------------------------- Klasse intlist::element
12 class intlist::element final
13 {
14 element *next;
15 int n;
16
17 element(element *e, int m)
18 : next(e), n(m)
19 { }
20
21 friend class intlist;
22 friend class intlist::iterator;
23 };
24
25 //----------------------------------------- Member-Funktionen intlist::iterator
26 intlist::iterator::iterator(element *e)
27 : current(e)
28 { }
29
30 bool intlist::iterator::operator!=(const iterator& i) const
31 {
32 return this->current != i.current;
33 }
34
35 int& intlist::iterator::operator*() const
36 {
37 return this->current->n;
38 }
39
40 intlist::iterator& intlist::iterator::operator++()
41 {
42 this->current = this->current->next;
43 return *this;
44 }
45
46 //--------------------------------------------------- Member-Funktionen intlist
47 intlist::intlist()
48 : head(nullptr)
49 { }
50
51 intlist::~intlist()
52 {
53 element *e = this->head;
54 while (e != nullptr)
55 {
56 element *x = e;
57 e = e->next;
58 delete x;
59 }
60 }
61
62 intlist& intlist::insert(int n)
63 {
64 this->head = new element(this->head, n);
65 return *this;
66 }
67
68 intlist::iterator intlist::begin()
69 {
70 return intlist::iterator(this->head);
71 }
72
73 intlist::iterator intlist::end()
74 {
75 return intlist::iterator(nullptr);
76 }
77