链接的列表在类C 中实现,显示分段故障

Linked list implementation in classes c++, showing Segmentation fault

本文关键字:实现 显示 分段 故障 列表 链接      更新时间:2023-10-16

我知道在此循环中发生了分割故障:( while(temp != NULL){temp = temp->next;}),但我不知道为什么。

#include<iostream>
using namespace std;
class zDepthList {
        typedef struct node {
                int data;
                node* next;
                node* prev;
        } Node;
public:
        zDepthList() {
                head = NULL;
        }
        zDepthList(int array[], int length) {
                Node *temp, *ptr;
                int i = 0;
                while(i != length - 1) {
                        temp = head;
                        ptr = new Node;
                        ptr->data = array[i];
                        i++;
                        ptr->next = NULL;
                        if(head == NULL) {
                                head = ptr;
                                ptr->prev = NULL;
                        }
                        else {
                                while(temp != NULL) {
                                        temp = temp->next;
                                }
                        }
                        temp->next = ptr;
                        ptr->prev = temp;
                }
        }
        void out(const char order) {
                cout << head->data << endl;
        return;
        }
private:
        Node *head;
};

对于初学者,您必须将head初始化为NULL

之后,循环

                    else {
                            while(temp != NULL) {
                                    temp = temp->next;
                            }
                    }
                    temp->next = ptr;
                    ptr->prev = temp;

指针temp等于NULL,因为它是中断循环的条件。因此,此语句

                    temp->next = ptr;

导致不确定的行为。

如果您有双链接列表,也很自然地引入数据成员tail,很容易附加新节点。

因此您应该包括

class zDepthList {
//...
private:
        Node *head, *tail;
};

在这种情况下,构造函数可以查看以下方式

    zDepthList() : head( nullptr ), tail( nullptr )
    {
    }
    zDepthList( const int a[], size_t n ) : head( nullptr ), tail( nullptr )
    {
        for ( size_t i = 0; i < n; i++ )
        {
            Node *tmp = new Node { a[i], nullptr, tail };
            tail == nullptr ? head = tmp : tail->next = tmp;
            tail = tmp;
        }
    }

这是一个指示的程序

#include <iostream>
class zDepthList {
    typedef struct node {
        int data;
        node* next;
        node* prev;
    } Node;
public:
    zDepthList() : head(nullptr), tail(nullptr)
    {
    }
    zDepthList(const int a[], size_t n) : head(nullptr), tail(nullptr)
    {
        for (size_t i = 0; i < n; i++)
        {
            Node *tmp = new Node{ a[i], nullptr, tail };
            tail == nullptr ? head = tmp : tail->next = tmp;
            tail = tmp;
        }
    }

    std::ostream & out( std::ostream &os = std::cout ) const
    {
        for (Node *current = head; current; current = current->next)
        {
            os << current->data << ' ';
        }
        return os;
    }
private:
    Node *head, *tail;
};
int main()
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    zDepthList l(a, sizeof(a) / sizeof(*a));
    l.out() << std::endl;
}

程序输出是

0 1 2 3 4 5 6 7 8 9

您永远不会设置head,而是访问它。这意味着它是非初始化的,这是UB。

您有2个ctors,只有在没有任何参数的情况下调用它。

才能初始化 head