C 链接列表不会停止增长

C++ linked list doesn stop growing

本文关键字:链接 列表      更新时间:2023-10-16

我正在尝试编写一个函数,该函数使用双重链接列表来计算fibonacci编号,但是由于某种原因,当我编译并运行链接列表时,一遍又一遍地添加1个数字。

这应该是SSCCE:

#include <iostream>
using namespace std;

class node {
    public:
        int value;
        node* previous;
        node* next;
};//node
class number {
    public:
        node* start;
        node* end;
        node* add (int value);
        void show (int K);
        number ();
        void destroy ();
        void copy (number gg1);
        void addition (number gg1, number gg2, int K);
        void fibonacci (int K, int times);
};//number
number::number () {
    start = NULL;
    end = NULL;
}
int power (int K) {
    int L = 1;
    for (int i = (K-1); i > 0; i--) {
        L = L*10;
    }
    return L;
}
int checksize (int value) {
    int counter = 0;
    while (value != 0) {
        value = value / 10;
        counter += 1;
    }
    return counter;
}
void number::show (int K) {
    node* current;
    cout << "nValue:" << endl;
    if (start == NULL) {
        cout << "nNothingn" << endl;
    }
    if (start != NULL) {
        current = start;
        while (current != NULL) {
            if (current->value == 0) {
                for (int i = 0; i < K; i++) {
                    cout << "0";
                }
            cout << "n";
            }
            else {
                int size = checksize (current->value);
                for (int j = size; j < K; j++) {
                    cout << "0";
                }
            cout << current->value << endl;
            }
            current = current->next;
        }   
    }
    //cout << "n";
}
int main () {
    number gg1;
    number gg2;
    number gg3;
    const int K = 5;
    gg1.fibonacci (K, 10);
}
node* number::add(int value) {   
        node* currentcode;                   
        if (start == NULL){                    
            currentcode = new node;      
            start = currentcode;               
            end = currentcode;            
            currentcode->next =  NULL;    
            currentcode->previous = NULL;    
            currentcode->value = value;
            return currentcode;
        }
        if (start != NULL) {                    
            currentcode = new node;    
            currentcode->next = NULL;   
            end->next = currentcode;  
            currentcode->previous = end;  
            end = currentcode;          
            currentcode->value = value;
            return currentcode;
        }
         return NULL;
}
void number::addition (number gg1, number gg2, int K) {
    int value1, value2, value3;
    int carry = 0;
    node* current1;
    node* current2;
    current1 = gg1.start;
    current2 = gg2.start;
    while (current1 != NULL || current2 != NULL) {
        if (current1 != NULL && current2 !=NULL) {
            value1 = current1->value;
            value2 = current2->value;
            value3 = value1 + value2 + carry;
            current1 = current1->next;
            current2 = current2->next;
        }
        else if (current1 == NULL && current2 != NULL) {
            value3 = current2->value + carry;
            current2 = current2->next;
        }
        else if (current1 != NULL && current2 == NULL) {
            value3 = current1->value + carry;
            current1 = current1->next;
        }
        checksize(value3);
        if (value3 > power(K)) {
            value3 = value3 - 10*(power(K));
            carry = 1;
        }
        else 
            carry = 0;
        add(value3);
        if ((current1 == NULL && current2 == NULL) && (carry == 1))
            add(1);
    }
}
void number::destroy () {
    node* current;
    node* current2;
    if (start != NULL) {
        current = start;
        current2 = current->next;
        while (current2 != NULL) {
            delete current;
            current = current2;
            current2 = current->next;
        }
        delete current;
    }
}   
void number::fibonacci (int K, int times) {
    number g1;
    number g2;
    number g3;
    destroy ();
    g1.add (1);
    g2.add (1);
    g3.addition (g1, g2, K);
    g2.copy(g1);
    g1.show(K);
    g2.show(K);
        //g1.copy(g3);
        //g1.show(K);
        //g2.show(K);
        //g3.show(K);
        //g3.addition (g1, g2, K);
        //g3.show(K);
        //g2.copy(g1);
        //g1.copy(g3);
    /*for (int i = 0; i < 2; i++) {
        g3.addition (g1, g2, K);
        g3.show(K);
        g2.copy(g1);
        g1.copy(g3);
    }*/
    copy(g3);
}
void number::copy (number gg1) {
    int value;
    destroy ();
    node* current = gg1.start;
    while (current != NULL) {
        value = current->value;
        add(value);
        current = current->next;
    }
}

每当我运行fibonacci功能时,它都会在终端中为我提供无尽的1。 number 类只是一个基本的双重链接指针列表。

添加函数独立工作正常,复制也很好。实际上,一切都很好,直到这个。使用循环完成功能很容易,但是此错误使我无法这样做。有人知道我的错误是什么吗?预先感谢。

现在,您的内存访问无效,因为在destroy()中的每个节点上调用delete并未将内存删除,但它仅标记免费内存。

建议的校正:

void number::destroy () {
    node* current;
    node* current2;
    if (start != NULL) {
        current = start;
        current2 = current->next;
        while (current2 != NULL) {
            delete current;
            current = current2;
            current2 = current->next;
        }
        delete current;
    }
    start = NULL; // so you can't access the now non-existing list anymore.
    end = NULL;
}

备注:

  • 班级名称应通过广泛适应的约定是资本优先。
  • 您不应在副本和加法功能中逐个值,而是const-ref。
  • 在这种情况下,最好使用operator=而不是copycopy可以是copy_fromcopy_to,调用函数copy是歧义的。
  • 可以在可以的情况下最好地用于循环。
  • Node不是类,而是结构,最好称其为结构。

新代码也可以看起来像:

Number& Number::operator=(const Number& n)
{
  destroy();
  for(Node* current = gg1.start; current; current = current->next)
    add(current->value);
}
void Number::destroy() 
{
    Node* temp;
    for(Node* current = start; current; current = current->next, delete temp)
      temp = current;
    start = NULL;
    end = NULL;
}