#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
int *elem;
int len;
}list;
void elemValues(list& L,int n);
void MengeList_sq(list& L1, list& L2, list& L3);
int main() {
list la, lb, lc;
lc.elem= new int;
elemValues(la,3);
elemValues(lb,5);
MengeList_sq(la, lb, lc);
for (int i = 0; i < la.len + lb.len; i++) {
printf("%d\t", lc.elem[i]);
}
system("pause");
return 0;
}
void elemValues(list &L,int n) {
L.elem = new int;
L.len = 0;
int* tmp = L.elem;
printf("请输入数据");
for (int i = 0; i < n; i++) {
scanf_s("%d", tmp);
tmp++;
L.len++;
}
}//初始化数据
void MengeList_sq(list& L1, list& L2, list& L3) {
int* pa, * pb,*pc, * pa_end, * pb_end;
pc = L3.elem;
pa = L1.elem;
pa_end = L1.elem + L1.len - 1;
pb = L2.elem;
pb_end = L2.elem + L2.len - 1;
while ((pa <= pa_end) && (pb <= pb_end)) {
if (*pa > *pb)
*pc++ = *pb++;
else
*pc++ = *pa++;
}
while (pa <= pa_end)*pc++ = *pa++;
while (pb <= pb_end)*pc++ = *pb++;
}
有序表的合并--c++
最新推荐文章于 2024-11-11 08:43:45 发布
本文介绍了如何在C语言中使用结构体和函数`elemValues`和`MengeList_sq`对两个整数列表进行合并排序,最后将结果打印出来。
摘要由CSDN通过智能技术生成