"Плохой список"

Материал из SEWiki
Перейти к: навигация, поиск
#include <stddef.h>

class BadList {
protected:
        BadList *next;
        int n;
        
public: 
        BadList(int first) {
                n = first;
                next = NULL;
        }
        virtual ~BadList() { 
                //? 
         }
        
        virtual void add(int value) {
                BadList *current = this;
                while ( current->next != NULL ) {
                        current = current->next;
                }
                current->next = new BadList(value);
        }
        
        size_t length() {
                size_t l = 0;
                BadList *current = this;
                while ( current->next != NULL) {
                        current = current->next;
                        l++;
                }
                return l;
        }
};

class BadDoubleList : public BadList {
protected:
        BadDoubleList *prev;
        
public:
        BadDoubleList(int start) : BadList(start) {
                prev = NULL;
        }               
        
        virtual void add(int value) {
                BadDoubleList *current = this;
                while ( current->next != NULL ) {
                        current = (BadDoubleList*) current->next;
                }
               current->next = new BadDoubleList(value);
               ((BadDoubleList*) current->next)->prev = current;
        }        
        
};      


void fill(BadList *l, int value, size_t num) {
        for(int i = 0; i < num; i++) {
                l->add(value);
        }
}

int main() {
        BadList l(1);
        BadDoubleList dl(1);
        
        fill(&l, 5, 5);
        fill(&dl, 6, 6);
        return 0;
}