C中的单链表(插入节点)
Singly linked list in C(Inserting a node)
我在输入的时候遇到了这个问题。该程序将冻结,并弹出 window 将打开并显示“.exe 已停止工作。”
只是一个简单的单向链表的插入和显示功能。我尝试了一切。我重写了代码并找到了另一种插入方式。我尝试了不同的编译器。它适用于 Turbo C,但我使用的是 devC++。
- 这是编译器错误吗?
代码如下:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <string.h>
typedef struct process
{
int pNum;
struct process *next;
}node;
node *create_node(node x)
{
node *temp;
temp=(node *)malloc(sizeof(node));
if(temp==NULL)
{
return 0;
}
else
{
temp->pNum=x.pNum;
temp->next=NULL;
}
return temp;
}
node *insert_node(node *head,node *last,node x)
{
node *temp;
temp=create_node(x);
if(head==NULL && head==last)
{
head=temp;
last=temp;
head->next=NULL;
last->next=NULL;
}
else
{
last->next=temp;
last=temp;
last->next=NULL;
}
return head;
}
int main()
{
node *head=NULL,*last=NULL,*temp,input;
int i,x,y,num;
printf("INPUT NUMBER: ");
scanf("%d",&num);
x=0;
y=6;
for(i=0;i<num;i++)
{
gotoxy(39,y);
scanf("%d",&input.pNum);
head=insert_node(head, last, input);
y++;
}
getch();
return 0;
}
我想我已经找到它停止工作的线路了。
关于函数 insert_node
行last->next=temp;
我好像找不到我哪里做错了。
在您的代码中,在第一个条目之后,头指针将指向新值。因为您正在分配该调用函数的 return 值。但最后的价值不会受到影响。因为您将其称为按价值传递。在
下次 head == last 将失败。
它将转到 else 块,而您正在访问
last->next=temp;
这就像访问null
指针这就是原因。如果你需要避免这种情况,你需要调用 last 作为引用传递。
你需要这个:
node *insert_node(node *head, node **last, node x)
{
node *temp;
temp=create_node(x);
if(head==NULL && head== *last)
{
head=temp;
*last=temp;
head->next=NULL;
(*last)->next=NULL;
}
else
{
(*last)->next=temp;
(*last)=temp;
(*last)->next=NULL;
}
return head;
}
这样调用:
head=insert_node(head, &last, input);
您的函数需要修改 last
指针。在 C 中,包括指针在内的值是按值传递的。因此,如果您在函数内部修改函数参数,则传递给函数的参数不会被修改。这就是您的程序中发生的事情。
在修改中,我们不简单地传递 last
指针,而是传递指向 last
指针的指针,这将允许我们修改 last
指针 main
函数。
简单示例:
int func1(int x)
{
x = 10;
return 2;
}
int func2(int *x)
{
*x = 10;
return 2;
}
...
int x = 3;
printf ("%d\n", func1(x));
printf ("%d\n", x);
printf ("%d\n", func2(&x));
printf ("%d\n", x);
将打印:
2
3
2
10
我在输入的时候遇到了这个问题。该程序将冻结,并弹出 window 将打开并显示“.exe 已停止工作。”
只是一个简单的单向链表的插入和显示功能。我尝试了一切。我重写了代码并找到了另一种插入方式。我尝试了不同的编译器。它适用于 Turbo C,但我使用的是 devC++。
- 这是编译器错误吗?
代码如下:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <string.h>
typedef struct process
{
int pNum;
struct process *next;
}node;
node *create_node(node x)
{
node *temp;
temp=(node *)malloc(sizeof(node));
if(temp==NULL)
{
return 0;
}
else
{
temp->pNum=x.pNum;
temp->next=NULL;
}
return temp;
}
node *insert_node(node *head,node *last,node x)
{
node *temp;
temp=create_node(x);
if(head==NULL && head==last)
{
head=temp;
last=temp;
head->next=NULL;
last->next=NULL;
}
else
{
last->next=temp;
last=temp;
last->next=NULL;
}
return head;
}
int main()
{
node *head=NULL,*last=NULL,*temp,input;
int i,x,y,num;
printf("INPUT NUMBER: ");
scanf("%d",&num);
x=0;
y=6;
for(i=0;i<num;i++)
{
gotoxy(39,y);
scanf("%d",&input.pNum);
head=insert_node(head, last, input);
y++;
}
getch();
return 0;
}
我想我已经找到它停止工作的线路了。
关于函数 insert_node
行last->next=temp;
我好像找不到我哪里做错了。
在您的代码中,在第一个条目之后,头指针将指向新值。因为您正在分配该调用函数的 return 值。但最后的价值不会受到影响。因为您将其称为按价值传递。在 下次 head == last 将失败。
它将转到 else 块,而您正在访问
last->next=temp;
这就像访问null
指针这就是原因。如果你需要避免这种情况,你需要调用 last 作为引用传递。
你需要这个:
node *insert_node(node *head, node **last, node x)
{
node *temp;
temp=create_node(x);
if(head==NULL && head== *last)
{
head=temp;
*last=temp;
head->next=NULL;
(*last)->next=NULL;
}
else
{
(*last)->next=temp;
(*last)=temp;
(*last)->next=NULL;
}
return head;
}
这样调用:
head=insert_node(head, &last, input);
您的函数需要修改 last
指针。在 C 中,包括指针在内的值是按值传递的。因此,如果您在函数内部修改函数参数,则传递给函数的参数不会被修改。这就是您的程序中发生的事情。
在修改中,我们不简单地传递 last
指针,而是传递指向 last
指针的指针,这将允许我们修改 last
指针 main
函数。
简单示例:
int func1(int x)
{
x = 10;
return 2;
}
int func2(int *x)
{
*x = 10;
return 2;
}
...
int x = 3;
printf ("%d\n", func1(x));
printf ("%d\n", x);
printf ("%d\n", func2(&x));
printf ("%d\n", x);
将打印:
2
3
2
10