0
我有一個C程序,它創建了一個兩部分單向鏈表。
我使用結構來生成列表,節點和節點數據。 當結構被定義爲C結構段溢出
struct TList {
struct LNode* first;
struct LNode* last_left;
};
struct LNode {
struct LData* data;
struct LNode* next;
};
名單看起來像[ A B C ][ D E ]
其中, list->first = "A"
和list->last_left = "C"
下列功能重置鏈表光標。
void reset_list_cursor(struct TList *list) {
struct LNode *temp, *temp1, *temp2;
int i = (list_left_size(list) - 1);
for (i; i >= 0; i--) {
temp = list->last_left;
temp1 = list->first;
if (temp != NULL) {
temp2 = temp1->next;
while (temp2 != temp) {
temp1 = temp1->next;
temp2 = temp2->next;
}
list->last_left = temp1;
}
}
list->last_left = NULL;
}
但是,我得到一個分段錯誤,我已經把範圍縮小壽以下行...
void reset_list_cursor(struct TList *list) {
temp1 = temp1->next;
temp2 = temp2->next;
我知道你永遠不能叫temp = temp->next->next
,但是當你創建一個結構代替temp->next
不應該工作嗎?
如果知道temp和temp-> next不是NULL(或者更確切地說,已知是有效的),則'temp = temp-> next-> next;'在句法上是有效的並且是安全的。 。 – 2014-10-01 20:08:17
這意味着你取消了'NULL'。先不檢查'NULL',你可以避免它。 – HuStmpHrrr 2014-10-01 20:25:50
[MCVE](http://stackoverflow.com/help/mcve)會有所幫助。 – 2014-10-01 21:11:13