| 1 | {{{ |
| 2 | #include <stddef.h> |
| 3 | |
| 4 | class BadList { |
| 5 | protected: |
| 6 | BadList *next; |
| 7 | int n; |
| 8 | |
| 9 | public: |
| 10 | BadList(int first) { |
| 11 | n = first; |
| 12 | next = NULL; |
| 13 | } |
| 14 | virtual ~BadList() { |
| 15 | //? |
| 16 | } |
| 17 | |
| 18 | void virtual add(int value) { |
| 19 | BadList *current = this; |
| 20 | while ( current->next != NULL ) { |
| 21 | current = current->next; |
| 22 | } |
| 23 | current->next = new BadList(value); |
| 24 | } |
| 25 | |
| 26 | size_t length() { |
| 27 | size_t l = 0; |
| 28 | BadList *current = this; |
| 29 | while ( current->next != NULL) { |
| 30 | current = current->next; |
| 31 | l++; |
| 32 | } |
| 33 | return l; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | class BadDoubleList : public BadList { |
| 38 | protected: |
| 39 | BadDoubleList *prev; |
| 40 | |
| 41 | public: |
| 42 | BadDoubleList(int start) : BadList(start) { |
| 43 | prev = NULL; |
| 44 | } |
| 45 | |
| 46 | void virtual add(int value) { |
| 47 | BadDoubleList *current = this; |
| 48 | while ( current->next != NULL ) { |
| 49 | current = (BadDoubleList*) current->next; |
| 50 | } |
| 51 | current->next = new BadDoubleList(value); |
| 52 | ((BadDoubleList*) current->next)->prev = current; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | void fill(BadList *l, int value, size_t num) { |
| 58 | for(int i = 0; i < num; i++) { |
| 59 | l->add(value); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | int main() { |
| 64 | BadList l(1); |
| 65 | BadDoubleList dl(1); |
| 66 | |
| 67 | fill(&l, 5, 5); |
| 68 | fill(&dl, 6, 6); |
| 69 | return 0; |
| 70 | } |
| 71 | }}} |