Проблема с моей функцией вставки двусвязного списка - PullRequest
0 голосов
/ 26 апреля 2020

Я пытаюсь завершить sh задание для моих структур данных класса c ++. Я должен определить двойной связанный список функций (например, insert () size () remove ()), который был предоставлен моим инструктором. Инструктор также включает основной файл, который запускает тесты для моего кода, чтобы посмотреть, работает ли он.

Я получаю его сообщение об ошибке: * Запуск тестов dlist * Проверка пустого списка ... FAILED: размер пустого списка равен! = 0.

Я пытался переписать определение Функция size () и insert () и я не понимаю, почему я получаю его ошибку.

мой тестовый код инструкторов:

bool test_empty() {
    std::cout << "Checking empty list...\n";
    dlist e;
    if(!verify(e))
        return false;
if(e.size() != 0) {
    std::cout << "FAILED: size of empty list is != 0.\n";
    return false;
}

if(!e.empty()) {
    std::cout << "FAILED: empty list is not .empty().\n";
    return false;
}

return true;
}

мой код для реализаций:

 void insert(node *previous, int value){
    if(previous == nullptr){
        node* n = new node;
        n->value = value;
        n->prev = previous;
        n->next = nullptr;
        return;
        }
    node* n = _head; //made a pointer to start at the head
    while( n!= previous )//make n go down the list until it hits previous
        {n = n->next;}
    node* store_next = n->next; //store the address of the prev pointer of the next node
    node* a = new node;//create the node that will be inserted
    a->value = value;
    n->next = a;// the pointer n points to the new node
    a->prev = n; //the prev in the new node points to the previous
    a->next = store_next; //the next in the new node points to the next node
    store_next->prev = a; //the next node's prev points to the new inserted node
}
 int size() const{
        node* n = _head;
        int size = 0;
        while(n != nullptr){
            size++;
            n = n -> next;
        }
        return size;
    }

Вот мой конструктор по умолчанию и структура двойного связанного списка, которую требует мой профессор, чтобы я использовал

class dlist {
  public:
    dlist() {
    }

struct node {
    int value;
    node* next;
    node* prev;
};
...