Changes between Initial Version and Version 1 of examples_from_lect


Ignore:
Timestamp:
01/21/16 22:07:33 (8 years ago)
Author:
Evgeny Linsky
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • examples_from_lect

    v1 v1  
     1{{{
     2#include <stddef.h>
     3
     4class BadList {
     5protected:
     6        BadList *next;
     7        int n;
     8       
     9public:
     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
     37class BadDoubleList : public BadList {
     38protected:
     39        BadDoubleList *prev;
     40       
     41public:
     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
     57void fill(BadList *l, int value, size_t num) {
     58        for(int i = 0; i < num; i++) {
     59                l->add(value);
     60        }
     61}
     62
     63int 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}}}