C语言输入两个已知升序的链表合并 且按顺序输出来
#include<stdio.h>#include<string.h>#include<stdlib.h>structstudent{intnum;charname[20...
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int size=sizeof(struct student); //结点大小
void main()
{ struct student *conbine(struct student *list1,struct student *list2);
struct student *list,*list1,*list2,*p1,*p2,*p3,*p4;
void PRINT(struct student *list);
char name[20];
int num,n=0,score;
list1=NULL,list2=NULL;
printf("请输入学生的信息(学号,名字,成绩)\n");
scanf("%d%s%d",&num,name,&score);
while(score!=-1)
{
n++;
p1=(struct student *)malloc(size);
strcpy(p1->name,name);p1->score=score;p1->num=num;
p1->next=NULL;
if(n==1)list1=p1;
else
p2->next=p1;
p2=p1;
scanf("%d%s%d",&num,name,&score);}
printf("请输入学生的信息(学号,名字,成绩)\n");
scanf("%d%s%d",&num,name,&score);
n=0;
while(score!=-1)
{
n++;
p3=(struct student *)malloc(size);
strcpy(p3->name,name);p3->score=score;p3->num=num;
p3->next=NULL;
if(n==1)list2=p3;
else
p4->next=p3;
p4=p3;
scanf("%d%s%d",&num,name,&score);
}
list=conbine(list1,list2);
PRINT(list);
}
struct student *conbine(struct student *list1,struct student *list2)
{ struct student *list;
struct student *p=list;
int flag;
list=NULL;
list=(struct student *)malloc(size);
while(1)
{
if((list1->score)<(list2->score))
{
list=list1;
list1=list1->next;
if(list1==NULL)
{ break;
flag=0;}
list=list->next;
}
if((list1->score)>(list2->score))
{
list=list2;
list2=list2->next;
if(list2==NULL)
{ break;
flag=0;
}
list=list->next;
}
if(list==NULL)
break;
}
if(flag==0)
list->next=list2;
else
list->next=list1;
return (p);
}
void PRINT(struct student *list)
{
struct student *p=list;
while(p!=NULL){
printf("%6d%s%6d\n",p->num,p->name,p->score);
p=p->next;
}
printf("\n");
}
为什么输不出来 求教 烦扰了我一天 谢谢 展开
#include<string.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int size=sizeof(struct student); //结点大小
void main()
{ struct student *conbine(struct student *list1,struct student *list2);
struct student *list,*list1,*list2,*p1,*p2,*p3,*p4;
void PRINT(struct student *list);
char name[20];
int num,n=0,score;
list1=NULL,list2=NULL;
printf("请输入学生的信息(学号,名字,成绩)\n");
scanf("%d%s%d",&num,name,&score);
while(score!=-1)
{
n++;
p1=(struct student *)malloc(size);
strcpy(p1->name,name);p1->score=score;p1->num=num;
p1->next=NULL;
if(n==1)list1=p1;
else
p2->next=p1;
p2=p1;
scanf("%d%s%d",&num,name,&score);}
printf("请输入学生的信息(学号,名字,成绩)\n");
scanf("%d%s%d",&num,name,&score);
n=0;
while(score!=-1)
{
n++;
p3=(struct student *)malloc(size);
strcpy(p3->name,name);p3->score=score;p3->num=num;
p3->next=NULL;
if(n==1)list2=p3;
else
p4->next=p3;
p4=p3;
scanf("%d%s%d",&num,name,&score);
}
list=conbine(list1,list2);
PRINT(list);
}
struct student *conbine(struct student *list1,struct student *list2)
{ struct student *list;
struct student *p=list;
int flag;
list=NULL;
list=(struct student *)malloc(size);
while(1)
{
if((list1->score)<(list2->score))
{
list=list1;
list1=list1->next;
if(list1==NULL)
{ break;
flag=0;}
list=list->next;
}
if((list1->score)>(list2->score))
{
list=list2;
list2=list2->next;
if(list2==NULL)
{ break;
flag=0;
}
list=list->next;
}
if(list==NULL)
break;
}
if(flag==0)
list->next=list2;
else
list->next=list1;
return (p);
}
void PRINT(struct student *list)
{
struct student *p=list;
while(p!=NULL){
printf("%6d%s%6d\n",p->num,p->name,p->score);
p=p->next;
}
printf("\n");
}
为什么输不出来 求教 烦扰了我一天 谢谢 展开
1个回答
2015-12-09
展开全部
struct student *conbine(struct student *list1,struct student *list2) {
struct student *list;
struct student *p;
int flag;
list=NULL;
list=(struct student *)malloc(size);
p = list;///////
while(1) {
if((list1->score)<(list2->score)) {
list->next=list1;////////
list=list->next;
list1=list1->next;
if(list1==NULL) {
flag = 0;///////////
break;
}
}
////// if((list1->score)>(list2->score)) {
else {
list->next=list2;////////
list=list->next;
list2=list2->next;
if(list2==NULL) {
flag = 1;//////
break;
}
}
}
if(flag == 0)
list->next = list2;
else
list->next = list1;
return (p);
}
学链表、记得多画图
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询