我知道在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