以下代码有点长,望有耐心的高手帮忙找错
#include<stdio.h>#include<stdlib.h>
struct data_array{
long num;
int score;
};
typedef struct student{
long num;
int score;
struct student*next;
}NODE;
NODE*insert(NODE*head,NODE*p0)
{
NODE*p1,*p2=p1=NULL;
if(head==NULL){
head=p0;
p0->next=NULL;
}
else{
p1=head;
while((p0->num>p1->num)&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
}
if(p1==head){
p0->next=head;
head=p0;
}
else if(p0->num<p1->num){
p2->next=p0;
p0->next=p1;
}
else{
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
NODE*create(struct data_array*array,int n)
{
int i;
NODE *p0,*head;
for(i=0;i<n;i++){
p0=(NODE*)malloc(sizeof(NODE));
p0->num=array[i].num;
p0->score=array[i].score;
p0->next=NULL;
if(i=0)
head=p0;
else
insert(head,p0);
}
return head;
}
void display(NODE*head)
{
NODE *p=head;
do{
printf("%8ld%4d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return ;
}
void copy_list(NODE *head1,NODE *head2)
{
NODE *p1=head1,*p2,*p;
int temp=1;
do{
p2=(NODE*)malloc(sizeof(NODE));
p2->num=p1->num;
p2->score=p1->score;
p2->next=NULL;
p1=p1->next;
if(temp==1){
head2=p=p2;
temp=0;}
else{
p->next=p2;
p=p2;
}
}while(p1!=NULL);
}
NODE *merge(NODE *head1,NODE *head2)
{
NODE *p1=head1,*p2=head2;
static NODE a;
int temp=1;
do{
if(p1->num<p2->num){
if(temp){
a=*p1;
temp=0;
p1=p1->next;
continue;
}
insert(&a,p1);
p1=p1->next;
}
else if(p1->num==p2->num){
if(p1->score>=p2->score){
if(temp){
a=*p1;
temp=0;
p1=p1->next;
p2=p2->next;
continue;
}
insert(&a,p1);
p1=p1->next;
p2=p2->next;
}
else{
if(temp){
a=*p2;
temp=0;
p2=p2->next;
p1=p1->next;
continue;
}
insert(&a,p2);
p2=p2->next;
p1=p1->next;
}
}
else{
if(temp){
a=*p2;
temp=0;
p2=p2->next;
continue;
}
insert(&a,p2);
p2=p2->next;
}
}while(p1!=NULL&&p2!=NULL);
if(p1==NULL)
do{
insert(&a,p2);
p2=p2->next;
}while(p2!=NULL);
else if(p2==NULL)
do{
insert(&a,p1);
p1=p1->next;
}while(p1!=NULL);
return &a;
}
NODE *del(NODE *head1,NODE *head2)
{
NODE *p1=head1,*p2=head2,*temp;
for(;p2!=NULL;p2=p2->next)
for(;p1!=NULL;temp=p1,p1=p1->next)
if(p1->num==p2->num){
if(p1==head1)
head1=p1->next;
else
temp->next=p1->next;
free(p1);
}
return head1;
}
int main(void)
{
struct data_array data_a[5]={{20304,75},{20311,89},{20303,62},{20307,87},{20320,79}};
struct data_array data_b[]={{20302,65},{20301,99},{20311,87},{20323,88},{20307,92},{20322,83}};
NODE *a,*b,*a_dup,*b_dup;
a=create(data_a,5);
b=create(data_b,6);
printf("a: ");
display(a);
printf("b: ");
display(b);
copy_list(a,a_dup);
copy_list(b,b_dup);
a=merge(a,b);
printf("a: ");
display(a);
del(b_dup,a_dup);
printf("b: ");
display(b_dup);
system("pause");
return 0;
}