创建节点
一个创建节点的函数:struct student * create(int n)
{
//创建3个结构体指针变量
struct student *head,*p1,*p2;
int t=0;
//首先进行初始化
head=NULL;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
//创建链表
do
{
t=t+1;
if(t==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
}while(t<n);
p2->next = NULL;
free(p1);//输入的最后一个数被free掉;
return(head);
}
n为需要创建的节点个数,为什么我要创建一个节点时编译器就会发送错误报告?
----------------解决方案--------------------------------------------------------
感觉程序没问题,
如果可以,请贴出报错提示或完整代码
----------------解决方案--------------------------------------------------------
你初始化那边错了,应该用 head=(student *)malloc(sizeof(LEN));
head->next=NULL;这样才是头的初始化.你自己在看看吧
还有你那个新建节点的创建有错,是p=(student *)malloc(sizeof(LEN)); 才对.
[[italic] 本帖最后由 swiminfire 于 2007-12-20 21:49 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
整个程序
//将2个链表合并,并按升序排列
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student * next;
};
struct student * create(int n);
struct student * combine(struct student *lista, struct student *listb);
int
main()
{
struct student *list1,*list2,*list,*p;
int n1,n2;
// 调用函数创建2个链表,返回头指针
printf("please input the length of list1 and list2:\n");
scanf("%d%d",&n1,&n2);
printf("create list1,with the length of %d .\n",n1);
list1=create(n1);
p=list1;
if(p!=NULL)
{
do
{
printf("%d %.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
printf("create list2,with the length of %d .\n",n2);
list2=create(n2);
p=list2;
if(p!=NULL)
{
do
{
printf("%d %.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
printf("\n");
//合并链表
list = combine(list1,list2);
//输出合并后的链表
p=list;
if(p!=NULL)
{
do
{
printf("%d %.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
return(0);
}
struct student * create(int n)
{
//创建3个结构体指针变量
struct student *head,*p1,*p2;
int t=0;
//首先进行初始化
head=NULL;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
//创建链表
do
{
t=t+1;
if(t==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
}while(t<n);
p2->next = NULL;
free(p1);//输入的最后一个数被free掉;
return(head);
}
struct student * combine(struct student *lista, struct student *listb)
{
struct student *pa,*pb,*head,*p1;
pa=lista;
pb=listb;
if(pa==NULL)
return(listb);
else if(pb==NULL)
return(lista);
else
{
if(pa->num > pb->num)
{
head=pb;
pb=pb->next;
}
else
{
head=pa;
pa=pa->next;
}
p1=head;
while(pa->next!=NULL&& pb->next!=NULL)
{
if(pa->num > pb->num)
{
p1->next=pb;
pb=pb->next;
}
else
{
p1->next=pa;
pa=pa->next;
}
p1=p1->next;//
}
if(pa->next==NULL)
p1->next=pb;
else if(pb->next==NULL)
p1->next=pa;
return(head);
}
}
编译时没有错误
可是运行时系统就说遇到问题要关闭。。。。
[[italic] 本帖最后由 sunpy 于 2007-12-20 23:04 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
顺便看看这个程序
//将一个链表按逆序排列
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student * next;
};
struct student * create(int n);
struct student * revllist(struct student *head);
int
main()
{
struct student * head,*p,*rehead;
int length;
printf("please input the length of the list:\n");
scanf("%d",&length);
//创建链表
head = create(length);
p=head;
if(head!=NULL)
do
{
printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
//调用函数按逆序排列
rehead=revllist(head);
p=rehead;
if(p!=NULL)
do
{
printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return(0);
}
struct student * create(int n)
{
struct student *p1,*p2,*head;
head=NULL;
int t=0;
p1=(struct student * )malloc(LEN);
printf("please input the num and score of the student :\n");
scanf("%d%f",&p1->num,&p1->score);
do
{
t=t+1;
if(t==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student * )malloc(LEN);
printf("please input the num and score of the student :\n");
scanf("%d%f",&p1->num,&p1->score);
}while(t<n);
p2->next = NULL;
free(p1);
return(head);
}
struct student * revllist(struct student * head)
{
struct student * p,*pr,*revhead;
for(p=head;p!=NULL;p=p->next);
revhead=p;
pr=revhead;
while(pr!=head)
{
for(p=head;p!=pr;p=p->next);
pr->next=p;
pr=pr->next;
}
head->next=NULL;
return(revhead);
}
编译时没有错误
运行时系统就说遇到问题要关闭
----------------解决方案--------------------------------------------------------