c-在尝试从链表中删除时,我丢失了整个列表,而不是一个int



这是我的所有代码,我不确定你是否需要所有代码。我正在尝试从列表中删除一个特定的数字,并打印剩余的代码。然而,使用此代码,我得到以下输出:

Enter your integer: 5
Enter your integer: 7
Enter your integer: 3
Enter your integer: 0
List is: 
3 -> 7 -> 5 -> 
What do you want to delete? 7
3 -> 
What do you want to delete? 0
Bye.

我只是对如何从开始指向下一个整数感到困惑。TIA。

#include <stdio.h>
#include <stdlib.h>
typedef struct intList {
int number;
struct intList *next;
} INT_LIST;
int main() {
INT_LIST *start = NULL, *temp;
/* Insert into list */
while(1) {
int userInput;
printf("Enter your integer: ");
scanf("%d", &userInput);
if(userInput == 0) {
break;
}
INT_LIST *temp = malloc(sizeof(INT_LIST));
temp->number = userInput;
temp->next = start;
start = temp;
}

/* Print list */
printf("List is: n");
INT_LIST *trvPtr = start;
while (trvPtr != NULL) {
if (trvPtr == NULL) {
printf("%d", trvPtr->number);
} else {
printf("%d -> ", trvPtr->number);
trvPtr = trvPtr->next;
}
}
printf("n");

/* Search and delete value */
int valueToBeDeleted;
while (1) {
printf("What do you want to delete? ");
scanf("%d", &valueToBeDeleted);
if (valueToBeDeleted == 0) {
printf("Bye.");
break;
}
if (start == NULL) {
printf("Can’t delete from an empty list.");
} else if (start->number == valueToBeDeleted) { 
start = start->next;
} else {
trvPtr = start;
while ( (trvPtr->next != NULL) && (trvPtr->next->number != valueToBeDeleted) ) {
trvPtr = trvPtr->next;
}
if (trvPtr->next == NULL) {
printf("Can’t find %d in the list n", valueToBeDeleted); 
} else {
trvPtr->next = (trvPtr->next)->next;
printf("%d -> n", trvPtr->number);
}
}
}
return 0;
}



#include <stdio.h>
#include <stdlib.h>

typedef struct intList {
int number;
struct intList *next;
} INT_LIST;

int main() {
INT_LIST *start = NULL, *temp;
/* Insert into list */
while(1) {
int userInput;
printf("Enter your integer: ");
scanf("%d", &userInput);
if(userInput == 0) {
break;
}

INT_LIST *temp = malloc(sizeof(INT_LIST));
temp->number = userInput;
temp->next = start;

start = temp;
}

/* Print list */
printf("List is: n");
INT_LIST *trvPtr = start;
while (trvPtr != NULL) {
if (trvPtr == NULL) {
printf("%d", trvPtr->number);
} else {
printf("%d -> ", trvPtr->number);
trvPtr = trvPtr->next;
}
}
printf("n");

/* Search and delete value */
int valueToBeDeleted;
while (1) {
printf("What do you want to delete? ");
scanf("%d", &valueToBeDeleted);
if (valueToBeDeleted == 0) {
printf("Bye.");
break;
}
if (start == NULL) {
printf("Can’t delete from an empty list.");
} else if (start->number == valueToBeDeleted) { 
start = start->next;
} else {
trvPtr = start;
while ( (trvPtr->next != NULL) && (trvPtr->next->number != valueToBeDeleted) ) {
trvPtr = trvPtr->next;
}
if (trvPtr->next == NULL) {
printf("Can’t find %d in the list n", valueToBeDeleted); 
} else {
trvPtr->next = (trvPtr->next)->next;
printf("%d -> n", trvPtr->number);
}
}
}

return 0;
}

除了start = start->next;会造成内存泄漏外,您的删除逻辑也非常复杂。如果同时使用节点指针的地址和节点指针,则无需处理特殊情况,例如第一个节点,也无需跟踪前一个节点。参见Linus了解指针

delete例程只使用指针地址和指向节点的指针进行迭代。找到要删除的节点后,只需将该地址的当前节点替换为列表中的下一个节点即可。保留一个指向当前节点的指针,用于释放节点内存。

在您的情况下,这将简化您的删除例程为:

INT_LIST **pp = &start
INT_LIST *p = start;

for (; p; pp = &pp->next, p = p->next)
if (p->number == valueToBeDeleted) {
*pp = p->next;
free (p);
break;
}

其中pp保存指针的地址(例如指针到指针(,p只是节点指针。当为p->number == valueToBeDeleted时,当前节点的地址处的指针被替换为p->nextp仍然指向当前节点,因此它被用于free(p)——在被p->next替换之前位于该地址的节点。

阅读本文以进一步了解该方法,如果您有进一步的问题,请告诉我。

相关内容

  • 没有找到相关文章

最新更新