1 /* 2 * intlist.cpp 3 * 4 * Beispielprogramm eingebettete Klassen. 5 * Variante mit const_iterator und Iterator-Hilfstypen fuer std-Algorithmen. 6 * 7 * Autor: H.Drachenfels 8 * Erstellt am: 11.11.2019 9 */
10 #include "intlist.h"
11
12 //----------------------------------------------------- Klasse intlist::element
13 class intlist::element final
14 {
15 element *next;
16 int n;
17
18 element(element *e, int m)
19 : next(e), n(m)
20 { }
21
22 friend class intlist;
23 friend class intlist::iterator;
24 };
25
26 //----------------------------------------- Member-Funktionen intlist::iterator
27 intlist::iterator::iterator(element *e)
28 : current(e)
29 { }
30
31 bool intlist::iterator::operator!=(const iterator& i) const
32 {
33 return this->current != i.current;
34 }
35
36 int& intlist::iterator::operator*() const
37 {
38 return this->current->n;
39 }
40
41 intlist::iterator& intlist::iterator::operator++()
42 {
43 this->current = this->current->next;
44 return *this;
45 }
46
47 //----------------------------------- Member-Funktionen intlist::const_iterator
48 intlist::const_iterator::const_iterator(const element *e)
49 : current(e)
50 { }
51
52 bool intlist::const_iterator::operator!=(const const_iterator& i) const
53 {
54 return this->current != i.current;
55 }
56
57 const int& intlist::const_iterator::operator*() const
58 {
59 return this->current->n;
60 }
61
62 intlist::const_iterator& intlist::const_iterator::operator++()
63 {
64 this->current = this->current->next;
65 return *this;
66 }
67
68 //--------------------------------------------------- Member-Funktionen intlist
69 intlist::intlist() : head(nullptr)
70 { }
71
72 intlist::~intlist()
73 {
74 element *e = this->head;
75 while (e != nullptr)
76 {
77 element *x = e;
78 e = e->next;
79 delete x;
80 }
81 }
82
83 intlist& intlist::insert(int n)
84 {
85 this->head = new element(this->head, n);
86 return *this;
87 }
88
89 intlist::iterator intlist::begin()
90 {
91 return intlist::iterator(this->head);
92 }
93
94 intlist::iterator intlist::end()
95 {
96 return intlist::iterator(nullptr);
97 }
98
99 intlist::const_iterator intlist::begin() const
100 {
101 return intlist::const_iterator(this->head);
102 }
103
104 intlist::const_iterator intlist::end() const
105 {
106 return intlist::const_iterator(nullptr);
107 }
108
109 intlist::const_iterator intlist::cbegin() const
110 {
111 return intlist::const_iterator(this->head);
112 }
113
114 intlist::const_iterator intlist::cend() const
115 {
116 return intlist::const_iterator(nullptr);
117 }
118