deleteall(key) 函数中这个语句 "cur = prev->next;" 的逻辑我不明白
I don't understand the logic of this statement "cur = prev->next;" in deleteall(key) function
这个程序创建了一个链表,在开头插入,并有一个deleteall(key)
函数,它会删除所有具有键值
的节点
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next; //creation a linked list with insertion at beginning
};
struct node* head = NULL;
int totaldeleted = 0;
void create(n) {
int i;
struct node *newnode, *temp;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
if(head==NULL){
head=newnode;
}
temp = head;
for(i=2; i<=n; i++){
int data;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &data);
newnode -> data = data;
newnode -> next = NULL;
newnode -> next = head;
head = newnode;
}
}
void display(){
struct node* temp;
printf("\nThe Linked list is: ");
temp = head;
while(temp!= NULL){
printf("%d", temp -> data);
temp = temp -> next;
}
}
int deleteall(key){
struct node *prev, *cur;
if(head == NULL){
prev = NULL;
cur = NULL;
printf("\nList is empty!");
}
while(head != NULL && head->data == key){
prev = head;
head = head -> next;
free(prev);
totaldeleted++;
}
prev = NULL;
cur = head;
while(cur != NULL){
if(cur->data == key && prev != NULL){
prev -> next = cur->next;
free(cur);
cur = prev->next; // I cannot understand this logic.
totaldeleted++;
}
else {
prev = cur;
cur = cur -> next;
}
}
return totaldeleted;
}
int main(){
int n, key;
printf("Enter the number of nodes: ");
scanf("%d", &n);
create(n);
display();
printf("\nEnter the key: ");
scanf("%d", &key);
totaldeleted = deleteall(key);
printf("Total deleted: %d", totaldeleted);
display();
printf("\n");
}
就在该行之前,cur
指向 已删除的 节点 - 此节点现在已被释放,因此 cur
指针的值不确定。我们要将 cur
指向 跟随 已删除节点的节点。如果它没有被删除,我们可以从 cur->next
中获取它。但是,cur->next
无效。我们可以使用临时变量在 free
...
之前存储 cur->next
的值
然而,在 free
之前有这一行:
prev -> next = cur->next;
即我们需要的值现在已经分配给 prev->next
,因此我们不需要临时变量,但可以从 prev->next
.
中获取它
这个程序创建了一个链表,在开头插入,并有一个deleteall(key)
函数,它会删除所有具有键值
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next; //creation a linked list with insertion at beginning
};
struct node* head = NULL;
int totaldeleted = 0;
void create(n) {
int i;
struct node *newnode, *temp;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
if(head==NULL){
head=newnode;
}
temp = head;
for(i=2; i<=n; i++){
int data;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &data);
newnode -> data = data;
newnode -> next = NULL;
newnode -> next = head;
head = newnode;
}
}
void display(){
struct node* temp;
printf("\nThe Linked list is: ");
temp = head;
while(temp!= NULL){
printf("%d", temp -> data);
temp = temp -> next;
}
}
int deleteall(key){
struct node *prev, *cur;
if(head == NULL){
prev = NULL;
cur = NULL;
printf("\nList is empty!");
}
while(head != NULL && head->data == key){
prev = head;
head = head -> next;
free(prev);
totaldeleted++;
}
prev = NULL;
cur = head;
while(cur != NULL){
if(cur->data == key && prev != NULL){
prev -> next = cur->next;
free(cur);
cur = prev->next; // I cannot understand this logic.
totaldeleted++;
}
else {
prev = cur;
cur = cur -> next;
}
}
return totaldeleted;
}
int main(){
int n, key;
printf("Enter the number of nodes: ");
scanf("%d", &n);
create(n);
display();
printf("\nEnter the key: ");
scanf("%d", &key);
totaldeleted = deleteall(key);
printf("Total deleted: %d", totaldeleted);
display();
printf("\n");
}
就在该行之前,cur
指向 已删除的 节点 - 此节点现在已被释放,因此 cur
指针的值不确定。我们要将 cur
指向 跟随 已删除节点的节点。如果它没有被删除,我们可以从 cur->next
中获取它。但是,cur->next
无效。我们可以使用临时变量在 free
...
cur->next
的值
然而,在 free
之前有这一行:
prev -> next = cur->next;
即我们需要的值现在已经分配给 prev->next
,因此我们不需要临时变量,但可以从 prev->next
.