I have this problem when I input. The program will freeze and a pop out window will open and says " .exe has stopped working. "
It is just a simple insert and display fuction of a singly linked list. I tried everything. I rewrote the code and find another way of inserting. I tried different compiler.. It works on turbo C but I am using devC++.
- Is this a compiler error?
Here is the code:
#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;
}
I think I have found out what line it stopped working.
On the function insert_node
The line last->next=temp;
It seems I can't find what I had done wrong.