---
title: "（转）合并两个有序线性表的元素(C语言版)"
author: "nchu2020"
date: "2014-02-25T19:49:37Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2014-02-25T19:49:37Z"
id: 2021847
url: "https://nchu.iteye.com/blog/2021847"
---

01. //合并两个有序线性表的元素保证无重复元素
02. #include&lt;stdio.h&gt;
03. struct LinerList
04. {
05. int \*list;
06. int size;
07. int MAXSIZE;
08. };
09. int main()
10. {
11. int list1\[15]={2,5,7,8,10,14,19,22,25,30};
12. int list2\[15]={3,5,8,9,11,18,22,28,30,32,35};
13. int list3\[30];
14. struct LinerList L1={list1,10,15};
15. struct LinerList L2={list2,11,15};
16. struct LinerList L3={list3,0,30};
17. int i,j,k;
18. for(i=j=k=0;k&lt;L3.MAXSIZE&&i&lt;L1.size&&j&lt;L2.size;k++)
19. {//这里的大括号可不加
20. if(L1.list\[i]&gt;L2.list\[j])
21. L3.list\[k]=L2.list\[j++];
22. else if(L1.list\[i]==L2.list\[j])
23. {
24. L3.list\[k]=L1.list\[i++];
25. j++;
26. }
27. else L3.list\[k]=L1.list\[i++];
28. }
29. while(k&lt;L3.MAXSIZE&&i&lt;L1.size)
30. L3.list\[k++]=L1.list\[i++];
31. while(k&lt;L3.MAXSIZE&&j&lt;L2.size)
32. L3.list\[k++]=L2.list\[j++];
33. L3.size=k;
34. printf(" 合并后的数组长度是【%d】\\n各元素如下：\\n",L3.size);
35. for(k=0;k&lt;L3.size;k++)
36. printf("%4d",L3.list\[k]);
37. printf("\\n");
38. return 0;
39. }
40. 本文来源：[http://blog.csdn.net/michellehsiao/article/details/8632797](http://blog.csdn.net/michellehsiao/article/details/8632797)