SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | ||
3 | using namespace std; | |
4 | ||
5 | template <typename H> class Table { | |
6 | public: | |
7 | virtual void del(H x) = 0; | |
8 | virtual void insert(H x) = 0; | |
9 | virtual H* max() = 0; | |
10 | virtual H* min() = 0; | |
11 | virtual void print() = 0; | |
12 | }; | |
13 | ||
14 | template <typename H> class Node{ | |
15 | private: | |
16 | H key; | |
17 | Node<H>* next; | |
18 | public: | |
19 | Node(H x){ | |
20 | key = x; | |
21 | next = NULL; | |
22 | } | |
23 | H getKey() { return key; } | |
24 | H* getPrtKey() { return &key; } | |
25 | Node<H>* getNext() { return next; } | |
26 | void setKey(H key) { this->key = key; } | |
27 | void setNext(Node<H>* next) { this->next = next; } | |
28 | }; | |
29 | ||
30 | template <typename H> class MyTable: public Table<H> { | |
31 | private: | |
32 | int size; | |
33 | Node<H>* head, *tail; | |
34 | ||
35 | public: | |
36 | - | if(head) |
36 | + | MyTable(){ |
37 | size = 0; | |
38 | head = tail = NULL; | |
39 | } | |
40 | bool isEmpty(){ | |
41 | if(!head) | |
42 | return true; | |
43 | return false; | |
44 | - | cout << temp->getKey() << ","; |
44 | + | |
45 | void print() { | |
46 | Node<H>* temp = head; | |
47 | cout << "["; | |
48 | while(temp){ | |
49 | cout << temp->getKey(); | |
50 | if(temp->getNext()) cout << ","; | |
51 | temp = temp->getNext(); | |
52 | } | |
53 | cout << "]" << endl; | |
54 | } | |
55 | void insert(H x){ | |
56 | size++; | |
57 | Node<H>* temp = new Node<H>(x); | |
58 | - | |
58 | + | |
59 | head = tail = temp; | |
60 | return; | |
61 | } | |
62 | tail->setNext(temp); | |
63 | tail = temp; | |
64 | } | |
65 | H* max(){ | |
66 | if(isEmpty()){ | |
67 | return NULL; | |
68 | } | |
69 | Node<H>* temp = head; | |
70 | H* n = head->getNext()->getPrtKey(); | |
71 | while(temp){ | |
72 | if(temp->getKey() > *n) | |
73 | n = temp->getPrtKey(); | |
74 | temp = temp->getNext(); | |
75 | } | |
76 | return n; | |
77 | } | |
78 | ||
79 | H* min(){ | |
80 | if(isEmpty()){ | |
81 | return NULL; | |
82 | } | |
83 | Node<H>* temp = head; | |
84 | H* n = head->getNext()->getPrtKey(); | |
85 | while(temp){ | |
86 | if(temp->getKey() < *n) | |
87 | n = temp->getPrtKey(); | |
88 | temp = temp->getNext(); | |
89 | } | |
90 | return n; | |
91 | } | |
92 | void del(H x){ | |
93 | Node<H>* temp = head; | |
94 | Node<H>* temp2 = NULL; | |
95 | Node<H>* prev = NULL; | |
96 | Node<H>* prev2 = NULL; | |
97 | size --; | |
98 | while(temp){ | |
99 | if(temp->getKey() == x){ | |
100 | temp2 = temp; | |
101 | prev2 = prev; | |
102 | } | |
103 | prev = temp; | |
104 | temp = temp->getNext(); | |
105 | } | |
106 | if(!prev2){ | |
107 | head = temp2->getNext(); | |
108 | return; | |
109 | } | |
110 | prev2->setNext(temp2->getNext()); | |
111 | delete(temp2); | |
112 | } | |
113 | }; | |
114 | ||
115 | template <typename H> class OrderedTable: public Table<H>{ | |
116 | private: | |
117 | int size; | |
118 | Node<H>* head, *tail; | |
119 | ||
120 | public: | |
121 | OrderedTable(){ | |
122 | size = 0; | |
123 | head = tail = NULL; | |
124 | } | |
125 | bool isEmpty(){ | |
126 | if(!head) | |
127 | return true; | |
128 | return false; | |
129 | } | |
130 | void print() { | |
131 | Node<H>* temp = head; | |
132 | cout << "["; | |
133 | while(temp){ | |
134 | cout << temp->getKey(); | |
135 | if(temp->getNext()) cout << ","; | |
136 | temp = temp->getNext(); | |
137 | } | |
138 | cout << "]" << endl; | |
139 | } | |
140 | void insert(H x){ | |
141 | size++; | |
142 | Node<H>*news = new Node<H>(x); | |
143 | Node<H>*prev = NULL; | |
144 | Node<H>*temp = head; | |
145 | ||
146 | if(isEmpty()){ | |
147 | head = tail = news; | |
148 | return; | |
149 | } | |
150 | if(temp->getKey() > x && temp == head){ | |
151 | head = news; | |
152 | news->setNext(temp); | |
153 | return; | |
154 | } | |
155 | prev = temp; | |
156 | temp = temp->getNext(); | |
157 | while(temp){ | |
158 | if(temp->getKey() > x && temp == tail){ | |
159 | prev->setNext(news); | |
160 | news->setNext(temp); | |
161 | return; | |
162 | } | |
163 | if(temp->getKey() > x){ | |
164 | prev->setNext(news); | |
165 | news->setNext(temp); | |
166 | return; | |
167 | } | |
168 | prev = temp; | |
169 | temp = temp->getNext(); | |
170 | } | |
171 | tail->setNext(news); | |
172 | tail = news; | |
173 | } | |
174 | ||
175 | H* max(){ | |
176 | if(isEmpty()){ | |
177 | return NULL; | |
178 | } | |
179 | return tail->getPrtKey(); | |
180 | } | |
181 | ||
182 | H* min(){ | |
183 | if(isEmpty()){ | |
184 | return NULL; | |
185 | } | |
186 | return head->getPrtKey(); | |
187 | } | |
188 | void del(H x){ | |
189 | Node<H>* temp = head; | |
190 | Node<H>* temp2 = NULL; | |
191 | Node<H>* prev = NULL; | |
192 | Node<H>* prev2 = NULL; | |
193 | size --; | |
194 | while(temp){ | |
195 | if(temp->getKey() == x){ | |
196 | temp2 = temp; | |
197 | prev2 = prev; | |
198 | } | |
199 | prev = temp; | |
200 | temp = temp->getNext(); | |
201 | } | |
202 | if(!prev2){ | |
203 | head = temp2->getNext(); | |
204 | return; | |
205 | } | |
206 | if(temp2 == tail) | |
207 | tail = prev2; | |
208 | prev2->setNext(temp2->getNext()); | |
209 | delete(temp2); | |
210 | } | |
211 | }; | |
212 | ||
213 | template <typename H> class NodeCir{ | |
214 | private: | |
215 | H key; | |
216 | NodeCir<H>* next, *prev; | |
217 | public: | |
218 | NodeCir(H x){ | |
219 | key = x; | |
220 | next = prev = NULL; | |
221 | } | |
222 | H getKey() { return key; } | |
223 | NodeCir<H>* getNext() { return next; } | |
224 | NodeCir<H>* getPrev() { return prev; } | |
225 | void setKey(H key) { this->key = key; } | |
226 | void setNext(NodeCir<H>* next) { this->next = next; } | |
227 | void setPrev(NodeCir<H>* prev) { this->prev = prev; } | |
228 | }; | |
229 | ||
230 | template <typename H> class CircularTable: public Table<H>{ | |
231 | private: | |
232 | int size; | |
233 | NodeCir<H>* current; | |
234 | ||
235 | public: | |
236 | CircularTable(){ | |
237 | size = 0; | |
238 | current = NULL; | |
239 | } | |
240 | bool isEmpty(){ | |
241 | if(!current) | |
242 | return true; | |
243 | return false; | |
244 | } | |
245 | void insert(H x){ | |
246 | NodeCir<H>* temp = new NodeCir<H>(x); | |
247 | if(isEmpty()){ | |
248 | current = temp; | |
249 | temp->setNext(current); | |
250 | temp->setPrev(current); | |
251 | return; | |
252 | } | |
253 | current->getPrev()->setNext(temp); | |
254 | temp->setPrev(current->getPrev()); | |
255 | temp->setNext(current); | |
256 | current->setPrev(temp); | |
257 | } | |
258 | ||
259 | void del(H x){ | |
260 | NodeCir<H>* temp = current->getPrev(); | |
261 | NodeCir<H>* sup = current; | |
262 | while(temp != current){ | |
263 | if(temp->getKey() == x){ | |
264 | sup->setPrev(temp->getPrev()); | |
265 | temp->getPrev()->setNext(sup); | |
266 | delete(temp); | |
267 | return; | |
268 | } | |
269 | sup = temp; | |
270 | temp = temp->getPrev(); | |
271 | } | |
272 | } | |
273 | ||
274 | void print(){ | |
275 | NodeCir<H>* temp = current; | |
276 | cout << "[" << temp->getKey() << ","; | |
277 | temp = current->getNext(); | |
278 | while(temp != current){ | |
279 | cout << temp->getKey(); | |
280 | if(temp->getNext() != current){ | |
281 | cout << ","; | |
282 | } | |
283 | temp = temp->getNext(); | |
284 | } | |
285 | cout << "]" << endl; | |
286 | } | |
287 | ||
288 | H* max() {} | |
289 | H* min() {} | |
290 | }; | |
291 | ||
292 | int main(){ | |
293 | /*MyTable<int>* T = new MyTable<int>(); | |
294 | T->insert(3); | |
295 | T->insert(7); | |
296 | T->insert(1); | |
297 | T->insert(8); | |
298 | T->insert(5); | |
299 | T->insert(2); | |
300 | T->insert(6); | |
301 | T->insert(1); | |
302 | T->insert(8); | |
303 | T->print(); | |
304 | T->del(8); | |
305 | T->del(7); | |
306 | T->del(1); | |
307 | T->print(); | |
308 | cout << "Elemento MAX " << *(T->max()) << endl; | |
309 | cout << "Elemento MIN " << *(T->min()) << endl; | |
310 | ||
311 | OrderedTable<int>* T = new OrderedTable<int>(); | |
312 | T->insert(3); | |
313 | T->insert(7); | |
314 | T->insert(1); | |
315 | T->insert(8); | |
316 | T->insert(5); | |
317 | T->insert(2); | |
318 | T->insert(6); | |
319 | T->insert(1); | |
320 | T->insert(8); | |
321 | T->print(); | |
322 | T->del(8); | |
323 | T->del(7); | |
324 | T->del(1); | |
325 | T->print(); | |
326 | cout << "Elemento MAX " << *(T->max()) << endl; | |
327 | cout << "Elemento MIN " << *(T->min()) << endl; | |
328 | */ | |
329 | CircularTable<int>* T = new CircularTable<int>(); | |
330 | T->insert(3); | |
331 | T->insert(7); | |
332 | T->insert(1); | |
333 | T->insert(8); | |
334 | T->insert(5); | |
335 | T->insert(2); | |
336 | T->insert(6); | |
337 | T->insert(1); | |
338 | T->insert(8); | |
339 | T->print(); | |
340 | T->del(8); | |
341 | T->del(7); | |
342 | T->del(1); | |
343 | T->print(); | |
344 | ||
345 | return 0; | |
346 | } |