C中的链表实现(调试-输出不正确)
我用C语言创建了这个链表程序。它从a3data.txt读取数据(这个文本文件的内容粘贴在下面的输出之后)。在文本文件中,插入和删除是命令。我希望能够读取INSERT命令,并在列表中插入下一个整数(下一行)。REMOVE命令应从列表中删除最后一个节点。正如您所看到的,我的删除功能不能正常工作,我不明白为什么。有人能帮我调试一下吗 输出C中的链表实现(调试-输出不正确),c,debugging,linked-list,C,Debugging,Linked List,我用C语言创建了这个链表程序。它从a3data.txt读取数据(这个文本文件的内容粘贴在下面的输出之后)。在文本文件中,插入和删除是命令。我希望能够读取INSERT命令,并在列表中插入下一个整数(下一行)。REMOVE命令应从列表中删除最后一个节点。正如您所看到的,我的删除功能不能正常工作,我不明白为什么。有人能帮我调试一下吗 输出 linux@computer ~/Documents/Data Structures/a3/code $ gcc exercise4.3.3.c linux@com
linux@computer ~/Documents/Data Structures/a3/code $ gcc exercise4.3.3.c
linux@computer ~/Documents/Data Structures/a3/code $ ./a.out
INSERT 0 5
INSERT 0 5 3
INSERT 0 5 3 19
REMOVE 0 5 3 19
INSERT 1
REMOVE 0
REMOVE 0
REMOVE 0
INSERT 4
INSERT 4 25
INSERT 4 25 5
REMOVE 0 25 5
INSERT 4
INSERT 4 874
REMOVE 0 874
REMOVE 0 874
INSERT 8
INSERT 8 75
INSERT 8 75 22
INSERT 8 75 22 6
REMOVE 0 75 22 6
INSERT 9
INSERT 9 31
INSERT 9 31 1
REMOVE 0 31 1
REMOVE 0 31 1
INSERT 419
INSERT 419 55
REMOVE 0 55
INSERT 5
文本文件
INSERT
5
INSERT
3
INSERT
19
REMOVE
INSERT
1
REMOVE
REMOVE
REMOVE
INSERT
4
INSERT
25
INSERT
5
REMOVE
INSERT
4
INSERT
874
REMOVE
REMOVE
INSERT
8
INSERT
75
INSERT
22
INSERT
6
REMOVE
INSERT
9
INSERT
31
INSERT
1
REMOVE
REMOVE
INSERT
419
INSERT
55
REMOVE
INSERT
5
#include <stdio.h>
#include <stdlib.h>
struct node {
int number;
struct node *next;
};
/* prototypes */
void ins(struct node *llist, int number);
void rem(struct node *llist);
void sho(struct node *llist);
int main(void)
{
int number;
char command[6];
struct node *llist;
llist = (struct node *)malloc(sizeof(struct node));
llist->number = 0;
llist->next = NULL;
FILE *file;
file = fopen("a3data.txt", "r");
if (file == NULL)
{
printf("\n----------------------------------------\n");
printf("| Error. Did not read file. Exiting. |\n");
printf("----------------------------------------\n\n");
exit(1);
}
else
{
while ((fscanf(file, "%s", command)) != EOF)
{
if((strcmp(command, "INSERT"))==0)
{
fscanf(file, "%d", &number);
printf("\nINSERT ", number);
ins(llist, number);
sho(llist);
}
else if((strcmp(command, "REMOVE"))==0)
{
printf("\n REMOVE ");
rem(llist);
sho(llist);
}
}
}
printf("\n");
free(llist);
return(0);
}
void ins(struct node *llist, int number)
{
while(llist->next != NULL)
{
llist = llist->next;
}
llist->next = (struct node *)malloc(sizeof(struct node));
llist->next->number = number;
llist->next->next = NULL;
}
void rem(struct node *llist)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
}
void sho(struct node *llist)
{
while(llist->next != NULL)
{
printf("%d ", llist->number);
llist = llist->next;
}
printf("%d", llist->number);
}
代码
INSERT
5
INSERT
3
INSERT
19
REMOVE
INSERT
1
REMOVE
REMOVE
REMOVE
INSERT
4
INSERT
25
INSERT
5
REMOVE
INSERT
4
INSERT
874
REMOVE
REMOVE
INSERT
8
INSERT
75
INSERT
22
INSERT
6
REMOVE
INSERT
9
INSERT
31
INSERT
1
REMOVE
REMOVE
INSERT
419
INSERT
55
REMOVE
INSERT
5
#include <stdio.h>
#include <stdlib.h>
struct node {
int number;
struct node *next;
};
/* prototypes */
void ins(struct node *llist, int number);
void rem(struct node *llist);
void sho(struct node *llist);
int main(void)
{
int number;
char command[6];
struct node *llist;
llist = (struct node *)malloc(sizeof(struct node));
llist->number = 0;
llist->next = NULL;
FILE *file;
file = fopen("a3data.txt", "r");
if (file == NULL)
{
printf("\n----------------------------------------\n");
printf("| Error. Did not read file. Exiting. |\n");
printf("----------------------------------------\n\n");
exit(1);
}
else
{
while ((fscanf(file, "%s", command)) != EOF)
{
if((strcmp(command, "INSERT"))==0)
{
fscanf(file, "%d", &number);
printf("\nINSERT ", number);
ins(llist, number);
sho(llist);
}
else if((strcmp(command, "REMOVE"))==0)
{
printf("\n REMOVE ");
rem(llist);
sho(llist);
}
}
}
printf("\n");
free(llist);
return(0);
}
void ins(struct node *llist, int number)
{
while(llist->next != NULL)
{
llist = llist->next;
}
llist->next = (struct node *)malloc(sizeof(struct node));
llist->next->number = number;
llist->next->next = NULL;
}
void rem(struct node *llist)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
}
void sho(struct node *llist)
{
while(llist->next != NULL)
{
printf("%d ", llist->number);
llist = llist->next;
}
printf("%d", llist->number);
}
rem()
到底应该做什么?移除第一个?现在它泄漏了malloc的内存,指针立即被覆盖。它的末尾还有一个llist
的修改,这是任何人都看不到的。引导到主程序,然后引用释放的内存。(我没有运行它,但阅读它时看起来就是这样)您的remove函数非常奇怪。它可能会帮助您提取一小块内存,其中包含一个示例列表,并跟踪rem
的功能。首先,为什么要在remove函数中分配内存?当您使用空闲(llist)
时,temp
指向的内存会发生什么变化?回答这些问题,您可能会了解更多有关链表的信息以及代码不起作用的原因。我在原始问题(作为编辑)的末尾发布了我尝试的解决方案(似乎适用于后进先出模式)。@stk这是一个改进,但仍然存在一些问题。正如@Nigel所说,你应该在纸上画出你的程序在做什么。我可以告诉你,我发现remove函数仍然存在两个问题,如果你把rem()
的功能画出来,你也会看到它们。谢谢你的帮助。rem()是否有可能破裂?它在终端中正确编译、执行和显示输出。到底是什么问题?