我知道在while循环中发生了段错误:(while(temp != NULL){temp = temp->next;}
),但我不知道为什么。class C++中的链表实现,显示分段错误
#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;
};
我们需要了解你在主程序中如何使用这个类。 – vincent
我们不应该为你做你的(家)工作。 –
我的主要传递数组的30个元素和数组的长度(zDepthList z(l,30);)。它调出函数(z.out('f'))。 – aashman