嗨,大家好,我学习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