不能从单链表中删除
cannot delete from a singly linked list
所以我有这个 struct
:
struct Book {
char *title;
char *author;
int pages;
struct Book *next;
}*start=NULL;
我有以下用于删除的功能:
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->pages == x) {
current = current->next;
break;
}
}
}
出于某种原因,这不起作用。我做错了什么?
您要按原样离开列表,您需要 if(current->pages == x)
,然后 previous->next = current->next;
,还要使用 free(current)
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->next != NULL){
if(current->next->pages == x) {
current->next = current->next->next;
break;
}
}
else if(current->pages == x){
free(current);
current = NULL;
}
}
}
您需要实际从列表中删除节点。此外,最好将逻辑分为从列表结构中删除列表节点的方法和实际删除它的方法。
struct Book* unlink(int x, struct Book *root) {
struct Book *current = root, *prev = root, *next = NULL;
while (current) {
if(current->pages == x) {
next = current->next;
if (prev != NULL) {
prev->next = next;
}
return current;
}
prev = current;
current = current->next;
}
return NULL;
}
int delete(int x, struct Book* root) {
struct Book *found = unlink(x, root);
if (found != NULL) {
free(found->title);
free(found->actor);
free(found);
return 0;
}
return -1;
}
首先了解从链表中删除节点的逻辑。
假设你有一个 link-list 作为 x1->x2->x3->x4 并且你想从中删除 x3。所以要删除x3,你所要做的就是让x2的next
指针指向x4。但在你这样做之前,你应该释放分配给节点 x3 的内存。
这里有一个简单的代码来实现这个:
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->next != NULL){
if(current->next->pages == x) {
Struct Book * temp = current->next; //save the address of the node to be deleted
current->next = current->next->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp); //free the memory
break;
}
}
}
}
上述实现不包含待删除节点为链表头节点或根节点的情况。
void delete(int x) {
if(start == NULL)
return;
struct Book *current;
if(start->pages == x)
{
Struct Book * temp = start; //save the address of the node to be deleted
start = start->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp);
return;
}
for(current = start; current; current = current->next)
{
if(current->next != NULL){
if(current->next->pages == x) {
Struct Book * temp = current->next; //save the address of the node to be deleted
current->next = current->next->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp); //free the memory
break;
}
}
}
}
所以我有这个 struct
:
struct Book {
char *title;
char *author;
int pages;
struct Book *next;
}*start=NULL;
我有以下用于删除的功能:
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->pages == x) {
current = current->next;
break;
}
}
}
出于某种原因,这不起作用。我做错了什么?
您要按原样离开列表,您需要 if(current->pages == x)
,然后 previous->next = current->next;
,还要使用 free(current)
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->next != NULL){
if(current->next->pages == x) {
current->next = current->next->next;
break;
}
}
else if(current->pages == x){
free(current);
current = NULL;
}
}
}
您需要实际从列表中删除节点。此外,最好将逻辑分为从列表结构中删除列表节点的方法和实际删除它的方法。
struct Book* unlink(int x, struct Book *root) {
struct Book *current = root, *prev = root, *next = NULL;
while (current) {
if(current->pages == x) {
next = current->next;
if (prev != NULL) {
prev->next = next;
}
return current;
}
prev = current;
current = current->next;
}
return NULL;
}
int delete(int x, struct Book* root) {
struct Book *found = unlink(x, root);
if (found != NULL) {
free(found->title);
free(found->actor);
free(found);
return 0;
}
return -1;
}
首先了解从链表中删除节点的逻辑。
假设你有一个 link-list 作为 x1->x2->x3->x4 并且你想从中删除 x3。所以要删除x3,你所要做的就是让x2的next
指针指向x4。但在你这样做之前,你应该释放分配给节点 x3 的内存。
这里有一个简单的代码来实现这个:
void delete(int x) {
struct Book *current;
for(current = start; current; current = current->next) {
if(current->next != NULL){
if(current->next->pages == x) {
Struct Book * temp = current->next; //save the address of the node to be deleted
current->next = current->next->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp); //free the memory
break;
}
}
}
}
上述实现不包含待删除节点为链表头节点或根节点的情况。
void delete(int x) {
if(start == NULL)
return;
struct Book *current;
if(start->pages == x)
{
Struct Book * temp = start; //save the address of the node to be deleted
start = start->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp);
return;
}
for(current = start; current; current = current->next)
{
if(current->next != NULL){
if(current->next->pages == x) {
Struct Book * temp = current->next; //save the address of the node to be deleted
current->next = current->next->next; //remove the node from the linked list
free(temp->author);
free(...); //whatever memory you want to release
free(temp); //free the memory
break;
}
}
} }