Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链表&x2014;在末尾插入节点_C_Data Structures_Linked List - Fatal编程技术网

C 链表&x2014;在末尾插入节点

C 链表&x2014;在末尾插入节点,c,data-structures,linked-list,C,Data Structures,Linked List,此代码中有什么错误。在插入操作期间,当插入第二个元素时,程序停止,窗口显示程序已停止工作。在构建日志中,它显示进程以状态-1073741510终止,有时进程以状态255终止。即使主函数中有一个return语句 #include <stdio.h> #include <conio.h> #include <stdlib.h> void insert(int); void print(); struct node { int data; stru

此代码中有什么错误。在插入操作期间,当插入第二个元素时,程序停止,窗口显示程序已停止工作。在构建日志中,它显示
进程以状态-1073741510终止,有时
进程以状态255终止。即使主函数中有一个return语句

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

void insert(int);
void print();
struct node
{
    int data;
    struct node *link;
};
struct node *temp, *temp1, *temp2, *head, *end;
int main()
{
    int i, ch, a;

    head = NULL;
    temp = (node*)malloc(sizeof(node));
    printf("Enter the number of items");
    scanf("%d", &ch);
    for(i = 0; i < ch; i++)
    {
        printf("\nEnter the number");
        scanf("%d", &a);
        insert(a);
        **call to insert**
        print();
    }
    return 0;
}

void insert(int x)
{
    temp = (node*)malloc(sizeof(struct node));
    temp->data = x;
    temp->link = NULL;
    temp2 = head;
    if(head == NULL)
    {
        head = temp;
    }

    else
    {
        while(temp2 != NULL)
        {
            temp2 = temp2->link;
        }
        temp2->link = temp;
    }
}

void print()
{
    temp1 = head;
    printf("\nthe list is:");
    while(temp1 != NULL)
    {
        printf("%d", temp1->data);
        temp1 = temp1->link;
    }
}
#包括
#包括
#包括
空白插入(int);
作废打印();
结构节点
{
int数据;
结构节点*链接;
};
结构节点*temp、*temp1、*temp2、*head、*end;
int main()
{
int i,ch,a;
head=NULL;
temp=(node*)malloc(sizeof(node));
printf(“输入项目数量”);
scanf(“%d”和“ch”);
对于(i=0;i数据=x;
temp->link=NULL;
temp2=头部;
if(head==NULL)
{
压头=温度;
}
其他的
{
while(temp2!=NULL)
{
temp2=temp2->link;
}
temp2->link=temp;
}
}
作废打印()
{
temp1=头部;
printf(“\n列表为:”);
while(temp1!=NULL)
{
printf(“%d”,temp1->data);
temp1=temp1->link;
}
}

此部分函数

else
{
    while(temp2 != NULL)
    {
        temp2 = temp2->link;
    }
    temp2->link = temp;
}
这是错误的。退出循环后,节点
temp2
等于NULL。因此,这项声明

    temp2->link = temp;
导致未定义的行为

按以下方式更改代码段

else
{
    while(temp2->link != NULL)
    {
        temp2 = temp2->link;
    }
    temp2->link = temp;
}
这句话也是主要的

temp = (node*)malloc(sizeof(node));
没有意义,会导致内存泄漏

除了变量头之外,这些全局变量的声明

struct node *temp, *temp1, *temp2, *head, *end;

也没有意义。

您的代码由于错误而崩溃-如果您在调试器中运行它,它会告诉您它在哪一行崩溃,您希望能够看到您犯的真正明显的错误。
temp2->link=temp
temp2
在while-loop之后为
NULL
。全局变量太多!
print()
中的
temp1
应该是
print()
本地的。最好将
头也传递给函数。
insert()
中的
temp
temp2
也应该是局部变量。在这里,您需要小心处理
head
——最好将其传递给函数并返回新的head(与旧的head相同,除非将第一个元素添加到列表中)。您定义了
end
,但从不使用它。那太草率了。