数据结构顺序表合并

#include
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
using namespace std;
typedef struct {
ElemType *elem; i
nt length;
int listsize;}Sqlist;
Status 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;}
Status CreateList_Sq(Sqlist &L, int n) {
int i;
if (!L.elem || n<0 || n>MAXSIZE)
return ERROR;
for (i = 1; i <= n; i++) {
cin >> L.elem[i - 1]; }
L.length = n;
return OK;}
void TraveseList_Sq(Sqlist &L) {
int i;
cout << L.elem[0];
for (i = 1; i <= L.length-1; i++) {
cout << “,” << L.elem[i]; }
cout << endl;}
void MergeList_Sq(Sqlist &Lc, Sqlist La, Sqlist Lb) { ElemType * pa = La.elem;
ElemType * pb = Lb.elem;
Lc.listsize = Lc.length = La.length + Lb.length;
ElemType *pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));
if (!Lc.elem) exit(OVERFLOW);
ElemType * pa_last = pa + La.length - 1;
ElemType * pb_last = pb+ 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++;}
int main(){
Sqlist La, Lb, Lc;
int m, n;
InitList_Sq(&La);
InitList_Sq(&Lb);
InitList_Sq(&Lc);
cout << “请输入La的长度:”;
cin >> n; cout << “请输入La的元素:”;
CreateList_Sq(La, n);
TraveseList_Sq(La);
cout << “请输入Lb的长度:”;
cin >> m; cout << “请输入Lb的元素:”;
CreateList_Sq(Lb, m);
TraveseList_Sq(Lb);
MergeList_Sq(Lc, La, Lb);
TraveseList_Sq(Lc);
return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值