#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;}
数据结构顺序表合并
最新推荐文章于 2022-07-13 13:54:22 发布