summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/my_list.h
Unidiff
Diffstat (limited to 'noncore/apps/opie-reader/my_list.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-reader/my_list.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/my_list.h b/noncore/apps/opie-reader/my_list.h
index b3f0cc0..f180d3d 100644
--- a/noncore/apps/opie-reader/my_list.h
+++ b/noncore/apps/opie-reader/my_list.h
@@ -7,81 +7,84 @@ class CList
7 struct node 7 struct node
8 { 8 {
9 T data; 9 T data;
10 node* next; 10 node* next;
11 node(T _data, node* _next = NULL) : data(_data), next(_next) {} 11 node(T _data, node* _next = NULL) : data(_data), next(_next) {}
12 node() : next(NULL) {}; 12 node() : next(NULL) {};
13 }; 13 };
14 protected: 14 protected:
15 node* front; 15 node* front;
16 node* back; 16 node* back;
17 public: 17 public:
18 CList() : front(NULL), back(NULL) {} 18 CList() : front(NULL), back(NULL) {}
19 ~CList() 19 ~CList()
20 { 20 {
21 if (front != NULL) 21 if (front != NULL)
22 { 22 {
23 while (front != NULL) 23 while (front != NULL)
24 { 24 {
25 node *p = front; 25 node *p = front;
26 front = p->next; 26 front = p->next;
27 delete p; 27 delete p;
28 } 28 }
29 } 29 }
30 } 30 }
31 T& first() { return front->data; }
32 T& last() { return back->data; }
31 T* operator[](int n) 33 T* operator[](int n)
32 { 34 {
33 node* current = front; 35 node* current = front;
34 while (n-- > 0) 36 while (n-- > 0)
35 { 37 {
36 if ((current = current->next) == NULL) 38 if ((current = current->next) == NULL)
37 return NULL; 39 return NULL;
38 } 40 }
39 return &(current->data); 41 return &(current->data);
40 } 42 }
41 void push_front(const T& t) 43 void push_front(const T& t)
42 { 44 {
43 node* n = new node(t,front); 45 node* n = new node(t,front);
44 if (front == NULL) 46 if (front == NULL)
45 { 47 {
46 front = back = n; 48 front = back = n;
47 } 49 }
48 else 50 else
49 front = n; 51 front = n;
50 } 52 }
51 void push_back(const T& t) 53 void push_back(const T& t)
52 { 54 {
53 node* n = new node(t); 55 node* n = new node(t);
54 if (front == NULL) 56 if (front == NULL)
55 { 57 {
56 front = back = n; 58 front = back = n;
57 } 59 }
58 else 60 else
59 { 61 {
60 back->next = n; 62 back->next = n;
61 back = n; 63 back = n;
62 } 64 }
63 } 65 }
66 bool isEmpty() { return (front == NULL); }
64 void erase(unsigned int n) 67 void erase(unsigned int n)
65 { 68 {
66 node* p = front; 69 node* p = front;
67 node* last = front; 70 node* last = front;
68 while (n-- > 0) 71 while (n-- > 0)
69 { 72 {
70 last = p; 73 last = p;
71 p = p->next; 74 p = p->next;
72 if (p == NULL) return; 75 if (p == NULL) return;
73 } 76 }
74 if (p == front) 77 if (p == front)
75 { 78 {
76 front = p->next; 79 front = p->next;
77 } 80 }
78 else 81 else
79 { 82 {
80 last->next = p->next; 83 last->next = p->next;
81 } 84 }
82 if (p == back) 85 if (p == back)
83 { 86 {
84 back = last; 87 back = last;
85 } 88 }
86 delete p; 89 delete p;
87 } 90 }
@@ -134,39 +137,47 @@ class CList
134 } 137 }
135 class iterator 138 class iterator
136 { 139 {
137 node* current; 140 node* current;
138 public: 141 public:
139 iterator(node* _c) : current(_c) {} 142 iterator(node* _c) : current(_c) {}
140 iterator& operator++() 143 iterator& operator++()
141 { 144 {
142 current = current->next; 145 current = current->next;
143 return *this; 146 return *this;
144 } 147 }
145 iterator& operator++(int) 148 iterator& operator++(int)
146 { 149 {
147 current = current->next; 150 current = current->next;
148 return *this; 151 return *this;
149 } 152 }
150 T operator*() 153 T operator*()
151 { 154 {
152 return current->data; 155 return current->data;
153 } 156 }
154 T* operator->() 157 T* operator->()
155 { 158 {
156 return &(current->data); 159 return &(current->data);
157 } 160 }
161 T* pContent()
162 {
163 return &(current->data);
164 }
158 bool operator!=(iterator t) 165 bool operator!=(iterator t)
159 { 166 {
160 return (current != t.current); 167 return (current != t.current);
161 } 168 }
169 bool operator==(iterator t)
170 {
171 return (current == t.current);
172 }
162 }; 173 };
163 iterator begin() 174 iterator begin()
164 { 175 {
165 return iterator(front); 176 return iterator(front);
166 } 177 }
167 iterator end() 178 iterator end()
168 { 179 {
169 return iterator(NULL); 180 return iterator(NULL);
170 } 181 }
171}; 182};
172#endif 183#endif