ITeye博客

(转)合并两个有序线性表的元素(C语言版)

nchu2020 · 2014-02-25 19:49 · 数据结构,线性表排序 · 编程语言 · Markdown

  1. //合并两个有序线性表的元素保证无重复元素
  2. #include<stdio.h>
  3. struct LinerList
  4. {
  5. int *list;
  6. int size;
  7. int MAXSIZE;
  8. };
  9. 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<L3.MAXSIZE&&i<L1.size&&j<L2.size;k++)
  19. {//这里的大括号可不加
  20. if(L1.list[i]>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<L3.MAXSIZE&&i<L1.size)
  30. L3.list[k++]=L1.list[i++];
  31. while(k<L3.MAXSIZE&&j<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<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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。