题目 1052: [编程入门]链表合并

本人菜鸟一枚,目前刚刚接触链表题目,哪里写的不好请多多指教

(^∀^●)ノシ

题目描述

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

输入格式

第一行,a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成

输出格式

按照学号升序排列的数据

样例输入

复制

2 3
5 100
6 89
3 82
4 95
2 10

样例输出

复制

2 10
3 82
4 95
5 100
6 89
#include<stdio.h>
#include<stdlib.h>
typedef struct Student{
	int num;
	int grade;
	struct Student *next; 
}Node,*node;
node creat(int n);//创建链表 
void unite(node head1,node head2);// 结合链表 
void order(node head,int sum);//排序链表 
void output(node head);//打印链表
int main(){
	int a,b;
	scanf("%d%d",&a,&b);//输入的两个链表元素数量 
	node head1=creat(a);//创造两个链表 ,head1,2为头节点 
	node head2=creat(b);
	unite(head1,head2);//结合两个链表 
	order(head1,a+b);//排序两个链表 
	return 0;
} 
node creat(int n){
	node head=(node)malloc(sizeof(Node));//head为头结点 ,尾插创造 
	head->next=NULL;
	node p=head;//p为头指针 
	for(int i=0;i<n;i++){
		node q=(node)malloc(sizeof(Node));
		q->next=NULL;
		p->next=q;
		p=q;
		scanf("%d%d",&(*q).num&(*q).grade);
	}
	return head;
}
void unite(node head1,node head2){
	node p;
	p=head1;
	while(p->next!=NULL)
	p=p->next;
	p->next=head2->next;
	head2->next=NULL;
}
void order(node head,int sum){
	node p=head->next;//p指向首结点 
	int t1,t2;//t1学号中间变量,t2成绩中间变量
	int cnt=0;//cnt为链表长度,这里选用冒泡排序 
	while(cnt<sum){
		node q=p;
		while(q->next!=NULL){
			if((q->num)>(q->next->num)){
				t1=q->num;
				q->num=q->next->num;
				q->next->num=t1;
				t2=q->grade;
				q->grade=q->next->grade;
				q->next->grade=t2;
			}
			q=q->next;
		}
		cnt++;
	} 
	output(head);
}
void output(node head){
	node p=head->next;
	node q;
	while(p!=NULL){
	printf("%d %d\n",p->num,p->grade);
	q=p;
	p=p->next;
	free(q);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是有序链表合并的C语言代码: ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; struct node* merge(struct node* list1, struct node* list2) { struct node* result = NULL; struct node** tail = &result; while (1) { if (list1 == NULL) { *tail = list2; break; } else if (list2 == NULL) { *tail = list1; break; } if (list1->data <= list2->data) { *tail = list1; list1 = list1->next; } else { *tail = list2; list2 = list2->next; } tail = &((*tail)->next); } return result; } void printList(struct node* list) { while (list) { printf("%d ", list->data); list = list->next; } printf("\n"); } int main() { struct node* list1 = NULL; struct node* list2 = NULL; list1 = (struct node*)malloc(sizeof(struct node)); list1->data = 1; list1->next = (struct node*)malloc(sizeof(struct node)); list1->next->data = 3; list1->next->next = (struct node*)malloc(sizeof(struct node)); list1->next->next->data = 5; list1->next->next->next = NULL; list2 = (struct node*)malloc(sizeof(struct node)); list2->data = 2; list2->next = (struct node*)malloc(sizeof(struct node)); list2->next->data = 4; list2->next->next = (struct node*)malloc(sizeof(struct node)); list2->next->next->data = 6; list2->next->next->next = NULL; printf("List 1: "); printList(list1); printf("List 2: "); printList(list2); struct node* mergedList = merge(list1, list2); printf("Merged list: "); printList(mergedList); return 0; } ``` 这里我使用了指针来操作链表,对于两个有序链表 `list1` 和 `list2`,我首先定义一个指向结果链表的指针 `tail`,然后使用循环比较 `list1` 和 `list2` 中的节点值大小,将较小的节点移动到 `tail` 指向的链表中,并更新 `tail` 指向较小值的节点。重复这个过程直到有一个链表被遍历完,最后将 `tail` 指向另一个链表的剩余部分,即得到了合并后的有序链表。 希望这段代码能帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值