使用链表实现河内塔的异常错误

时间:2018-10-08 16:31:38

标签: c++ recursion linked-list towers-of-hanoi

我正在尝试使用链接列表实现河内塔。这是代码。

    struct tower_ {
    int size;
    node* head;
    tower_() {
        size = 0;
        head = NULL;
    }
    int pop() {
        if (size == 0)
            return -1;
        else {
            size--;
            node* iter = head;
            while (iter->next && iter->next->next != NULL)
                iter = iter->next;
            int rt = iter->next->value;
            delete iter->next;
            iter->next = NULL;
            return rt;
        }

    }
    int insert(int x) {
        node* newNode = new node;
        newNode->value = x;
        newNode->next = NULL;
        if (size == MAX_SIZE)
            return -1;
        else if (head == NULL) {
            head = newNode;
            size++;
            return 0;
        }
        else {
            node* iter = head;
            while (iter->next != NULL)
                iter = iter->next;
            iter->next = newNode;
            size++;
            return 0;
        }
    }
    void printTower() {
        node* iter = new node;
        for (iter = head; iter != NULL; iter = iter->next) {
            std::cout << iter->value << " ";
        }
        std::cout << "\n";
    }
};

void hanoi(int size, tower_ source, tower_ target, tower_ aux) {
    if (size == 1) {
    target.insert(source.pop());
    return;
}
    hanoi(size - 1, source, aux, target);
    hanoi(size - 1, aux, target, source);
}

int main() {
    tower_ A, B, C;
    A.insert(3);
    A.insert(2);
    A.insert(1);
    std::cout << "A: ";
    A.printTower();
    hanoi(3, A, C, B);
    std::cout << "\n" << "C: ";
    C.printTower();
    return 0;
}

运行时,我得到以下输出。

A: 3 2 1

C:

我似乎没有发现pop()insert()函数有任何问题,我已经分别测试了它们。我进行了调试,发现可能是由于-1函数的pop()返回空列表所致。 另一方面,当我将河内的定义更改为:

void hanoi(int size, tower_ source, tower_ target, tower_ aux) {
    if (size > 0) {
        hanoi(size - 1, source, aux, target);
        target.insert(source.pop());
        hanoi(size - 1, aux, target, source);
    }

它在以下行引发异常错误: int rt = iter->next->value;Exception thrown: read access violation. iter->next was nullptr.

执行此操作的正确方法是什么?任何线索将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

调试器会有所帮助。当您每次修改塔时打印出塔时,就会发生这种情况。

void hanoi(int size, tower_ source, tower_ target, tower_ aux) {
    if (size == 1) {
        std::cout << "target before insert: ";
        target.printTower() ;
        std::cout << std::endl;
        std::cout << "source before pop: "; 
        source.printTower() ;
        std::cout << std::endl;

        target.insert(source.pop());


        std::cout << "target after insert: ";
        target.printTower() ;
        std::cout << std::endl;
        std::cout << "source after pop: " ;
        source.printTower();
        std::cout << std::endl << "------------------------------------" <<  std::endl;
        return;
    }
    hanoi(size - 1, source, aux, target);
    hanoi(size - 1, aux, target, source);
}

A: 3 2 1 

target before insert: 

source before pop: 3 2 1 

target after insert: 1 

source after pop: 3 2 

------------------------------------
target before insert: 

source before pop: 

target after insert: -1 

source after pop: 

------------------------------------
target before insert: 3 2 

source before pop: 

target after insert: 3 2 -1 

source after pop: 

------------------------------------
target before insert: 

source before pop: 3 2 -1 

target after insert: -1 

source after pop: 3 2 

------------------------------------

C: