首页 > 试题广场 >

已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两

[问答题]

已有ab两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按

学号升序排列。

推荐

#include<stdio.h>

#include<malloc.h>

#define LEN sizeof(struct student)


struct student

{long num;

int score;

struct student *next;

};


struct student lista,listb;

int n,sum=0;


int main()

{struct student *creat(void);

struct student *insert(struct student  *,struct student*);

void print(struct studet*);

struct student *ahead,*bhead,*abh;

printf("input list a:\n");

ahead=creat();

sum=sum+n;

printf("input list b:\n");

bhead=creat();

sum=sum+n;

abh=insert(ahead,bhead);

print(abh);

return 0;

}


struct student *creat(void)              //建立链表函数

{struct student *p1,*p2,*head;

n=0;

p1=p2=(struct student *)malloc(LEN);

printf("input number & scores of student\n");

printf("if number is 0,stop inputing.\n');

scanf("%1d,%d",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{n=n+1;

if(n==1)

head=p1;

else

p2->next=p1;

p2=p1;

p1=(struct student *)malloc(LEN);

scanf("%1d,%d",&p1->num,&p1->score);

}

p2->next=NULL;

return(head);

}


struct student *insert(struct student *ah, struct student *bh)         //插入函数

{struct student *pa1,*pa2,*pb1,*pb2;

pa2=pa1=ah;

pb2=pb1=bh;

do

{while((pb1->num>pa1->num)&&(pa1->next!=NULL))

{pa2=pa1;

pa1=pa1->next;

}

if(pb1->num<=pa1->num)

{if(ah=pa1)

ah=pb1;

else

pa2->next=pb1;

pb1=pb1->next;

pb2->next=pa1;

pa2=pb2;

pb2=pb1;

}

}while((pa1->next!=NULL)||(pa1==NULL&& pb1!=NULL));

if(pb1!=NULL)&&(pb1->num>pa1->num)&&(pa1->next==NULL))

pa1->next=pb1;

return(ah);

}


void print( struct student *head)                //输出函数

{struct student  *p;

printf("There are %drecords:  \n",sum);

p=head

if(p!=NULL)

do

{printf("%1d %d\n",p->num,p->score);

p=p->next;

}while(p!=NULL);

}


发表于 2018-03-25 10:26:21 回复(0)
#include<stdio.h>
#include<malloc.h>    
int n,m;
typedef struct student
{
    int id;
    int score;
    struct student * next;
}stu;

stu * creat(int dd)  {
    stu * p,* head = NULL, * tail = head;
    
    for (int i = 0; i < dd; i++){
        p = (stu *)malloc(sizeof(struct student));  
        scanf("%d %d", &p->id, &p->score);
        p->next = NULL;
        if (head == NULL)    {
            head = p;
        }    else
            tail->next = p;
        tail = p;
    }
    return head;
}
stu *together(stu *stu1, stu *stu2)
{
    stu1[m].next=stu2;                        
    stu *p, *q, t;
    for (p=stu1; p!=NULL; p=p->next)          
        for (q=p->next; q!=NULL; q=q->next)
            if (p->id>q->id){
                t.id=p->id;
                p->id=q->id;
                q->id=t.id;
                
                t.score=p->score;
                p->score=q->score;
                q->score=t.score;
            }
    return stu1;
}
//输出
void print(stu * p){
    while (p != NULL)
    {
        printf("%d %d\n", p->id, p->score);
        p = p->next;
    }
}
int main()
{
 
    scanf("%d %d",&m,&n);
    stu * a, * b, * c;
    a = creat(m);
    b = creat(n);
    c = together(a,b);
    print(c);
    return 0;
}


发表于 2021-08-28 16:23:41 回复(0)