数据结构 线性表 顺序表的归并

举报
悦来客栈的老板 发表于 2020/12/30 01:54:42 2020/12/30
【摘要】 #include <stdio.h>#include <stdlib.h> #define LIST_INIT_SIZE 100#define LISTLNCREMENT 10#define OK 1#define ERROR 0#define OVERFLOW -2 typedef int ElemType; typedef struct{ ElemType *elem; ...

      #include <stdio.h>
      #include <stdlib.h>
      #define LIST_INIT_SIZE 100
      #define LISTLNCREMENT 10
      #define OK 1
      #define ERROR 0
      #define OVERFLOW -2
      typedef int ElemType;
      typedef struct
      {
      	ElemType *elem;
     	int length;
     	int listsize;
      }SqList;
      int InitList_Sq(SqList &L)
      {
      	L.elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
     	if (!L.elem)
      	{
     		exit(OVERFLOW);
      	}
      	L.length = 0;
      	L.listsize = LIST_INIT_SIZE;
     	return OK;
      }
      void Create_Sq(SqList &L, int n)
      {
     	int i;
     	for (i=0; i<n; i++)
      	{
     		scanf("%d",&L.elem[i]);
      	}
      	L.length = n;
      }
      void Display_Sq(SqList L)
      {
     	int i;
     	for (i=0; i<L.length ; i++)
      	{
     		printf("%d ",L.elem[i]);
      	}
     	printf("\n");
      }
      void MergeList_Sq(SqList La, SqList Lb, SqList &Lc)
      {/*已知顺序表La和Lb的元素按值非递减排序,归并La和Lb得到新的顺序线性表Lc,Lc的值也是按非递减排序*/
      	ElemType *pa,*pb,*pc,*pa_last,*pb_last;
      	pa = La.elem ;
      	pb = Lb.elem ;
      	Lc.listsize = Lc.length = La.length + Lb.length ;
      	pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));
     	if (!pc)
      	{
     		exit(OVERFLOW);
      	}
      	pa_last = La.elem + La.length - 1;
      	pb_last = Lb.elem + Lb.length - 1;
     	while (pa <= pa_last && pb <= pb_last)
      	{
     		if (*pa <= *pb)
      		{
      			*pc++ = *pa++;
      		}
     		else
      		{
      			*pc++ = *pb++;
      		}
      	}
     	while (pa <= pa_last)
      	{
      		*pc++ = *pa++;
      	}
     	while (pb <= pb_last)
      	{
      		*pc++ = *pb++;
      	}
      }
      void sort(SqList &L)
      {
     	int i,j;
     	int temp;
     	for (i=0; i<L.length-1; i++)
      	{
     		for (j=L.length-2; j>=i; j--)
      		{
     			if (L.elem[j+1] < L.elem [j])
      			{
       temp = L.elem[j+1] ;
       L.elem[j+1]  = L.elem[j];
       L.elem[j] = temp;
      			}
      		}
      	}
      }
      int main()
      {
     	int n;
      	SqList La,Lb,Lc;
      	InitList_Sq(La);
      	InitList_Sq(Lb);
     	printf("请输入链表La的长度: ");
     	scanf("%d",&n);
     	printf("请输入 %d 个数据: ",n);
      	Create_Sq(La,n);
     	printf("请输入链表Lb的长度: ");
     	scanf("%d",&n);
     	printf("请输入 %d 个数据: ",n);
      	Create_Sq(Lb,n);
      	sort(La);
      	sort(Lb);
      	MergeList_Sq(La,Lb,Lc);
     	printf("两个链表归并后的链表是: ");
      	Display_Sq(Lc);
     	return 0;
      }
  
 

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/17078337

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。