Diffstat (limited to 'noncore/apps/opie-reader/my_list.h') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/my_list.h | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/my_list.h b/noncore/apps/opie-reader/my_list.h new file mode 100644 index 0000000..b3f0cc0 --- a/dev/null +++ b/noncore/apps/opie-reader/my_list.h | |||
@@ -0,0 +1,172 @@ | |||
1 | #ifndef __MY_LIST_H | ||
2 | #define __MY_LIST_H | ||
3 | |||
4 | template<class T> | ||
5 | class CList | ||
6 | { | ||
7 | struct node | ||
8 | { | ||
9 | T data; | ||
10 | node* next; | ||
11 | node(T _data, node* _next = NULL) : data(_data), next(_next) {} | ||
12 | node() : next(NULL) {}; | ||
13 | }; | ||
14 | protected: | ||
15 | node* front; | ||
16 | node* back; | ||
17 | public: | ||
18 | CList() : front(NULL), back(NULL) {} | ||
19 | ~CList() | ||
20 | { | ||
21 | if (front != NULL) | ||
22 | { | ||
23 | while (front != NULL) | ||
24 | { | ||
25 | node *p = front; | ||
26 | front = p->next; | ||
27 | delete p; | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | T* operator[](int n) | ||
32 | { | ||
33 | node* current = front; | ||
34 | while (n-- > 0) | ||
35 | { | ||
36 | if ((current = current->next) == NULL) | ||
37 | return NULL; | ||
38 | } | ||
39 | return &(current->data); | ||
40 | } | ||
41 | void push_front(const T& t) | ||
42 | { | ||
43 | node* n = new node(t,front); | ||
44 | if (front == NULL) | ||
45 | { | ||
46 | front = back = n; | ||
47 | } | ||
48 | else | ||
49 | front = n; | ||
50 | } | ||
51 | void push_back(const T& t) | ||
52 | { | ||
53 | node* n = new node(t); | ||
54 | if (front == NULL) | ||
55 | { | ||
56 | front = back = n; | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | back->next = n; | ||
61 | back = n; | ||
62 | } | ||
63 | } | ||
64 | void erase(unsigned int n) | ||
65 | { | ||
66 | node* p = front; | ||
67 | node* last = front; | ||
68 | while (n-- > 0) | ||
69 | { | ||
70 | last = p; | ||
71 | p = p->next; | ||
72 | if (p == NULL) return; | ||
73 | } | ||
74 | if (p == front) | ||
75 | { | ||
76 | front = p->next; | ||
77 | } | ||
78 | else | ||
79 | { | ||
80 | last->next = p->next; | ||
81 | } | ||
82 | if (p == back) | ||
83 | { | ||
84 | back = last; | ||
85 | } | ||
86 | delete p; | ||
87 | } | ||
88 | void sort() | ||
89 | { | ||
90 | int i,j,inc,n; | ||
91 | T v; | ||
92 | T* item; | ||
93 | node* t; | ||
94 | t = front; | ||
95 | n = 0; | ||
96 | while (t != NULL) | ||
97 | { | ||
98 | n++; | ||
99 | t = t->next; | ||
100 | } | ||
101 | if (n >= 2) | ||
102 | { | ||
103 | item = new T[n]; | ||
104 | i = 0; | ||
105 | t = front; | ||
106 | for (t = front, i = 0; t != NULL; t = t->next, i++) | ||
107 | { | ||
108 | item[i] = t->data; | ||
109 | } | ||
110 | |||
111 | for (inc = 1; inc <= n; inc = 3*inc+1); | ||
112 | |||
113 | do | ||
114 | { | ||
115 | inc /= 3; | ||
116 | for (i = inc; i < n; i++) | ||
117 | { | ||
118 | v = item[i]; | ||
119 | for (j = i; v < item[j-inc] && j >= inc; j -= inc) | ||
120 | { | ||
121 | item[j] = item[j-inc]; | ||
122 | } | ||
123 | item[j] = v; | ||
124 | } | ||
125 | } | ||
126 | while (inc > 1); | ||
127 | for (t = front, i = 0; t != NULL; t = t->next, i++) | ||
128 | { | ||
129 | t->data = item[i]; | ||
130 | } | ||
131 | // back = *(item[n-1]); | ||
132 | delete [] item; | ||
133 | } | ||
134 | } | ||
135 | class iterator | ||
136 | { | ||
137 | node* current; | ||
138 | public: | ||
139 | iterator(node* _c) : current(_c) {} | ||
140 | iterator& operator++() | ||
141 | { | ||
142 | current = current->next; | ||
143 | return *this; | ||
144 | } | ||
145 | iterator& operator++(int) | ||
146 | { | ||
147 | current = current->next; | ||
148 | return *this; | ||
149 | } | ||
150 | T operator*() | ||
151 | { | ||
152 | return current->data; | ||
153 | } | ||
154 | T* operator->() | ||
155 | { | ||
156 | return &(current->data); | ||
157 | } | ||
158 | bool operator!=(iterator t) | ||
159 | { | ||
160 | return (current != t.current); | ||
161 | } | ||
162 | }; | ||
163 | iterator begin() | ||
164 | { | ||
165 | return iterator(front); | ||
166 | } | ||
167 | iterator end() | ||
168 | { | ||
169 | return iterator(NULL); | ||
170 | } | ||
171 | }; | ||
172 | #endif | ||