【发布时间】:2015-10-20 11:32:17
【问题描述】:
我在输入时遇到了这个问题。程序将冻结并打开一个弹出窗口并显示“.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;
我好像找不到我做错了什么。
【问题讨论】:
-
现在,只是一个整数。
-
当您调用
insert_node(head, last, input)时,您希望last被修改,但事实并非如此。 -
@RachitAhuja 指向指针?也许是因为我的函数返回了一个指针?我尝试了另一种方法,只是 void。仍然没有为我工作。
-
我之所以说这可能是编译器错误,因为我已经多次使用此代码,仅使用 Turbo C。但现在,我使用的是 devC++。
标签: c singly-linked-list