c语言链表问题,先建两个链表,再连接,然后按小到大排序,最后输出,求找错 10
#include<stdio.h>#include<malloc.h>#defineSIZEsizeof(structstudent)intn=0;structstude...
#include<stdio.h>
#include<malloc.h>
#define SIZE sizeof(struct student )
int n=0;
struct student
{
long num;
float score;
struct student *next;
};
struct student* creat()
{
int m=0;
struct student *p1,*p2,*head;
p1 = p2 = (struct student *) malloc(SIZE);
printf("Enter the number and the score of student\n");
scanf("%ld%f",&(p1->num),&(p1->score));
while(p1->num !=0)
{
n++;
m++;
if(n==1)
head = p1;
else
p2->next = p1;
p2=p1;
p2->next = NULL;
p1= (struct student *) malloc(SIZE);
printf("Enter the number and the score of student\n");
scanf("%ld%f",&(p1->num),&(p1->score));
}
return head;
}
void combination(struct student *p1,struct student *p2)
{
struct student *p;
p=p1;
while(p->num !=0)
p++;
p->next = p2;
}
void rank(struct student * p)
{
int i,j;
long number;
float score1;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if((p+j)->num>(p+j+1)->num)
{
number = (p+j)->num;
(p+j)->num=(p+j+1)->num;
(p+j+1)->num =number;
score1 = (p+j)->score;
(p+j)->score = (p+j+1)->score;
(p+j+1)->score = score1;
}
}
}
}
void print(struct student *p)
{
while(p->num!=0)
{
printf("%ld %f\n",p->num,p->score);
p = p->next;
}
}
void main()
{
struct student *p1,*p2;
p1 = creat();
p2 = creat();
combination(p1,p2);
rank(p1);
print(p1);
} 展开
#include<malloc.h>
#define SIZE sizeof(struct student )
int n=0;
struct student
{
long num;
float score;
struct student *next;
};
struct student* creat()
{
int m=0;
struct student *p1,*p2,*head;
p1 = p2 = (struct student *) malloc(SIZE);
printf("Enter the number and the score of student\n");
scanf("%ld%f",&(p1->num),&(p1->score));
while(p1->num !=0)
{
n++;
m++;
if(n==1)
head = p1;
else
p2->next = p1;
p2=p1;
p2->next = NULL;
p1= (struct student *) malloc(SIZE);
printf("Enter the number and the score of student\n");
scanf("%ld%f",&(p1->num),&(p1->score));
}
return head;
}
void combination(struct student *p1,struct student *p2)
{
struct student *p;
p=p1;
while(p->num !=0)
p++;
p->next = p2;
}
void rank(struct student * p)
{
int i,j;
long number;
float score1;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if((p+j)->num>(p+j+1)->num)
{
number = (p+j)->num;
(p+j)->num=(p+j+1)->num;
(p+j+1)->num =number;
score1 = (p+j)->score;
(p+j)->score = (p+j+1)->score;
(p+j+1)->score = score1;
}
}
}
}
void print(struct student *p)
{
while(p->num!=0)
{
printf("%ld %f\n",p->num,p->score);
p = p->next;
}
}
void main()
{
struct student *p1,*p2;
p1 = creat();
p2 = creat();
combination(p1,p2);
rank(p1);
print(p1);
} 展开
1个回答
2015-10-27
展开全部
太长了。这里有代码,我就不贴了,自己去看,链表的创建,删除,归并http://blog.csdn.net/peerslee/article/details/49451277
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询