有关链表的插入函数出错
刚接触到链表的基本操作,但不知道插入函数哪里出了问题,调试了好久也不知道为什么。希望好心人帮忙解答,感激不尽。
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
char name[20];
char ID[20];
char s[10];
int age;
struct student *link;
}node;
//创建长度为n的单链表
node *create(int n)
{
node *head,*precious,*current;
int cnt;
int label = 1;
for(cnt = 0;cnt < n;cnt++)
{
if(label == 1)
{
precious = (node *)malloc(sizeof(node));
printf("Input the information just like:name ID gender age \n");
scanf("%s%s%s%d",precious->name,precious->ID,precious->s,&precious->age);
precious->link = NULL;
head = precious;
label = 0;
}
else
{
current = (node*)malloc(sizeof(node));
printf("Input the information just like:name ID gender age \n");
scanf("%s%s%s%d",current->name,current->ID,current->s,¤t->age);
current->link = NULL;
precious->link = current;
precious = current;
}
}
return head;
}
//输出整个链表
void print(node *link_chain)
{
node *temp = link_chain;
while(temp != NULL)
{
printf("%s %s %s %d\n",temp->name,temp->ID,temp->s,temp->age);
temp = temp->link;
}
}
//插入节点
node *insert(node *link_chain,node *temp) //函数出错但找不出原因
{
node *p0,*p1,*p2;
p1 = link_chain;
p0 = temp;
while(p0->age > p1->age && p1->link != NULL)
{
p2 = p1;
p1 = p1->link;
}
if(p0->age <= p1->age)
{
if(p1 == link_chain)
{p0->link = p1;link_chain = p0;}
else
{p2->link = p0;p0->link = p1;}
}
else
{
p1->link = p0;
p0->link = NULL;
}
return link_chain;
}
int main()
{
node *link_chain = create(3);
node *link_chain2 = NULL;
node *insert_link;
printf("Input the insert node: name ID gender age \n");
scanf("%s%s%s%d",link_chain2->name,link_chain2->ID,link_chain2->s,&link_chain2->age);
link_chain2->link = NULL;
insert_link = insert(link_chain,link_chain2);
print(insert_link);
return 0;
}
insert
[解决办法]
link_chain2 这个没有分配内存,分配内存就好了。
[解决办法]
node *link_chain2 = NULL; 改为
node *link_chain2 = (node *)malloc(sizeof(node));
[解决办法]
node *link_chain2 = NULL; 改为
node *link_chain2 = (node *)malloc(sizeof(node));
[解决办法]
指针使用前分配空间
[解决办法]
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
char name[20];
char ID[20];
char s[10];
int age;
struct student *next;
}node;
//创建长度为n的单链表
node *create(int n)
{
node *head, *precious, *current;
int cnt;
int label = 1;
for(cnt = 0;cnt < n;cnt++)
{
if(label == 1)
{
precious = (node *)malloc(sizeof(node));
if (NULL == precious)
{
fprintf(stderr, "malloc failed!\n");
exit(1);
}
printf("Input the information just like:name ID gender age \n");
scanf("%s%s%s%d",precious->name,precious->ID,precious->s,&precious->age);
precious->next = NULL;
head = precious;
label = 0;
}
else
{
current = (node*)malloc(sizeof(node));
if (NULL == current)
{
fprintf(stderr, "malloc failed!\n");
exit(1);
}
printf("Input the information just like:name ID gender age \n");
scanf("%s%s%s%d",current->name, current->ID, current->s, ¤t->age);
current->next = NULL;
precious->next = current;
precious = current;
}
}
return head;
}
//输出整个链表
void print(node *next_chain)
{
node *temp = next_chain;
while(temp != NULL)
{
printf("%s %s %s %d\n",temp->name,temp->ID,temp->s,temp->age);
temp = temp->next;
}
}
//插入节点
node *insert(node *next_chain,node *temp) //函数出错但找不出原因
{
node *p0, *p1, *p2;
p1 = next_chain;
p0 = temp;
while (p0->age > p1->age && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
//return next_chain;
}
if(p0->age <= p1->age)
{
if(p1 == next_chain)
{
p0->next = p1;
next_chain = p0;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
return next_chain;
}
int main()
{
node *next_chain = create(3);
node *next_chain2 = NULL;
node *insert_next;
next_chain2 = (node*)malloc(sizeof(node));
if (NULL == next_chain2)
{
fprintf(stderr, "malloc failed!\n");
exit(1);
}
printf("Input the insert node: name ID gender age \n");
scanf("%s%s%s%d",next_chain2->name,next_chain2->ID,next_chain2->s,&next_chain2->age);
next_chain2->next = NULL;
insert_next = insert(next_chain, next_chain2);
print(insert_next);
return 0;
}