已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并,按学号升序排列。

本文详细介绍了一个使用C语言实现的链表操作案例,包括链表的创建、合并与排序等功能,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define NULL 0
int n=0;
struct student {
int num;
char name[10];
struct student *next;

};

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

struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%d %s",&p1->num,p1->name);

while(p1->num!=0){
        n++;
if(n==1){head=p1;}
    else p2->next=p1;
            p2=p1;
            p1=(struct student *)malloc(LEN);
            scanf("%d %s",&p1->num,p1->name);

    }
        p2->next=NULL;
        n=0;
        return head;

}




struct student *ronghe(struct student *p1,struct student *p2){   //链表合并函数


struct student *p=p1;

while(p1->next!=NULL) p1=p1->next;
   p1->next=p2;




return p;
}


void paixu(struct student *p){                                  //链表排序函数                                         
 char tname[20];
 int tnum,k=0,i=1;

 struct student *p1;
 p1=p;
 while(i!=0)
    {
        while(p1->next!=NULL){
                if(p1->num>p1->next->num)  {
                    tnum=p1->num;
                    p1->num=p1->next->num;
                    p1->next->num=tnum;

                    strcpy(tname,p1->name);
                    strcpy(p1->name,p1->next->name);
                    strcpy(p1->next->name,tname);
                    k++;
                }
                p1=p1->next;
            }
            p1=p;
        if(k>0){k=0;}
        else i=0;

    }







}

void print(struct student *p){                                  //打印链接函数


while(p!=NULL){
    printf("%d %s\n",p->num,p->name);
    p=p->next;
}
}


int main()
{
struct student *p1=creat();
struct student *p2=creat();
struct student *p=ronghe(p1,p2);
paixu(p);
print(p);

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值