![]() |
#2
apull2021-01-10 21:29
|
我的代码:

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Stu
{
int id;
int results;
struct Stu *next;
}Stu;
Stu *create_Stu(int n)
{
Stu *head=(Stu*)malloc(sizeof(Stu));
if(head==NULL)
return NULL;
head->id=-1;
head->results=-1;
head->next=NULL;
Stu *temp=head;
int i;
for(i=0;i<n;i++)
{
Stu *new_node=(Stu*)malloc(sizeof(Stu));
if(new_node==NULL)
return 0;
scanf("%d",&new_node->id);
scanf("%d",&new_node->results);
new_node->next=NULL;
temp->next=new_node;
temp=new_node;
}
return head;
}
Stu *merge_Stu(Stu *s1,Stu *s2)
{
if(s1==0||s2==0)
return 0;
Stu *s1_tail=s1;
while(s1_tail->next!=0)
s1_tail=s1_tail->next;
s1_tail->next=s2->next;
free(s2);
return s1;
}
void sort_Stu(Stu *stu)
{
if(stu==0)
return;
Stu *t1,*t2,*temp,t;
t1=t2=temp=0;
for(t1=stu->next;t1->next!=0;t1=t1->next)
{
temp=t1;
for(t2=t1->next;t2!=0;t2=t2->next)
{
if(temp->id>t2->id)
temp=t2;
}
if(temp!=t1)
{
t=*t1;
*t1=*temp;
*temp=t;
t.next=t1->next;
t1->next=t2->next;
t2->next=t.next;
}
}
}
void print_Stu(Stu *s)
{
if(s==0||s->next!=0)
return;
Stu *node=s->next;
while(node!=0)
{
printf("%d %d\n",node->id,node->results);
node=node->next;
}
}
void destory_Stu(Stu *s)
{
if(s==0)
return;
Stu *p=s;
Stu *p1=0;
while(p!=0)
{
p1=p->next;
free(p);
p=p1;
}
}
int main ()
{
int N, M;
scanf("%d%d", &N, &M);
//创建两个链表
Stu *students1 = create_Stu(N);
Stu *students2 = create_Stu(M);
//合并两个链表
Stu *students = merge_Stu(students1, students2);
//对新链表中的内容按学号升序排列并打印
sort_Stu(students);
print_Stu(students);
destory_Stu(students);
return 0;
}
#include <string.h>
#include <stdlib.h>
typedef struct Stu
{
int id;
int results;
struct Stu *next;
}Stu;
Stu *create_Stu(int n)
{
Stu *head=(Stu*)malloc(sizeof(Stu));
if(head==NULL)
return NULL;
head->id=-1;
head->results=-1;
head->next=NULL;
Stu *temp=head;
int i;
for(i=0;i<n;i++)
{
Stu *new_node=(Stu*)malloc(sizeof(Stu));
if(new_node==NULL)
return 0;
scanf("%d",&new_node->id);
scanf("%d",&new_node->results);
new_node->next=NULL;
temp->next=new_node;
temp=new_node;
}
return head;
}
Stu *merge_Stu(Stu *s1,Stu *s2)
{
if(s1==0||s2==0)
return 0;
Stu *s1_tail=s1;
while(s1_tail->next!=0)
s1_tail=s1_tail->next;
s1_tail->next=s2->next;
free(s2);
return s1;
}
void sort_Stu(Stu *stu)
{
if(stu==0)
return;
Stu *t1,*t2,*temp,t;
t1=t2=temp=0;
for(t1=stu->next;t1->next!=0;t1=t1->next)
{
temp=t1;
for(t2=t1->next;t2!=0;t2=t2->next)
{
if(temp->id>t2->id)
temp=t2;
}
if(temp!=t1)
{
t=*t1;
*t1=*temp;
*temp=t;
t.next=t1->next;
t1->next=t2->next;
t2->next=t.next;
}
}
}
void print_Stu(Stu *s)
{
if(s==0||s->next!=0)
return;
Stu *node=s->next;
while(node!=0)
{
printf("%d %d\n",node->id,node->results);
node=node->next;
}
}
void destory_Stu(Stu *s)
{
if(s==0)
return;
Stu *p=s;
Stu *p1=0;
while(p!=0)
{
p1=p->next;
free(p);
p=p1;
}
}
int main ()
{
int N, M;
scanf("%d%d", &N, &M);
//创建两个链表
Stu *students1 = create_Stu(N);
Stu *students2 = create_Stu(M);
//合并两个链表
Stu *students = merge_Stu(students1, students2);
//对新链表中的内容按学号升序排列并打印
sort_Stu(students);
print_Stu(students);
destory_Stu(students);
return 0;
}