嗨,大家好,我學習C,我無法理解這樣的代碼:鏈表指針
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//delete a link with given key
struct node* delete(int key) {
//start from the first link
struct node* current = head;
struct node* previous = NULL;
//if list is empty
if(head == NULL) {
return NULL;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == head) {
//change first to point to next link
head = head->next;
} else {
//bypass the current link
previous->next = current->next;
}
return current;
}
代碼是實際工作,這是消除用C從鏈表元素,但我不知道如何,如果我們沒有碰到頭結構變量(這是我的麻煩):
//bypass the current link
previous->next = current->next;
我明白的代碼,但我不明白的變量頭怎麼會變化,如果我們不要做頭=某事。
而且,它是如何的更多鈔票,以具有相同的名稱(當前)
感謝
順便說一句,我發現這裏的代碼兩個變量:https://www.tutorialspoint.com/data_structures_algorithms/linked_list_program_in_c.htm
因爲你省略本教程中的函數定義和發佈荒謬內嵌代碼。 –
你真的錯過了功能的一部分。更詳細的參見教程。您在這裏找到的部分只能找到要刪除的部分。代碼在下面刪除。 – Kajienk