我不明白爲什麼雙指針。它沒有任何目的。所以,我已經寫了一個簡單的程序來做你需要的。
假設這將是確定結構
typedef struct Mynbr Mynbr_t;
我的鏈表倒車功能將是這樣的(這是遞歸調用)
void reverseNumber(Mynbr_t* start) {
if (start == NULL) return;
static Mynbr_t* head;
Mynbr_t* current = start;
if (current->next != NULL) {
reverseNumber(current->next);
head->next = current;
head = current;
head->next = NULL;
} else
head = current;
}
您的
Mynbr
struct Mynbr {
int k;
struct Mynbr* next;
};
類型的結構
繼續,使用下面的代碼進行測試。它只是顛倒了名單。
int main() {
size_t Mynbr_size = sizeof(Mynbr_t);
Mynbr_t* start = (Mynbr_t*) malloc(Mynbr_size);
Mynbr_t* current = start;
int i;
for (i=0; i<10; i++) {
current->k = i;
if (i!=9) {
current->next = (Mynbr_t*) malloc(Mynbr_size);
current = current->next;
}
}
current = start;
Mynbr_t* last = NULL;
while (current != NULL) {
printf("%d\n", current->k);
current = current->next;
if (current != NULL)
last = current; // you need to grab this to loop through reverse order
}
reverseNumber(start);
current = last;
while (current != NULL) {
printf("%d\n", current->k);
current = current->next;
}
current = last;
Mynbr_t* temp;
while (current->next != NULL) {
temp = current;
current = current->next;
free(temp); // always free the allocated memory
} last = NULL;
return 0;
}
請發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。什麼是'current-> next'?它真的是Mynbr **而不是Mynbr *嗎? – MikeCAT
我認爲應該是 reverseNumber(&(current-> next)); – Gregg
請打開編譯器警告。 – MikeCAT