#include<stdio.h>
#include<stdlib.h>
#define Max 15
typedef struct {
int length;
int* data;
}SeqList;
void InitList(SeqList &L) {
L.data = new int[Max];
if (!L.data)
return;
L.length = 0;
}
void PrintList(SeqList L) {
for (int i = 0; i < L.length; i++)
printf("%d ", L.data[i]);
printf("\n");
}
void CreateList(SeqList &L) {
int n;
printf("请输入顺序表元素个数:\n");
scanf_s("%d", &n);
printf("输入元素:\n");
for (int i = 0; i < n; i++) {
scanf_s("%d", &L.data[i]);
}
L.length = n;
}
void MergeList(SeqList A, SeqList B, SeqList &C) {
int* pa, * pb, * pa_last, * pb_last, * pc;
pa = A.data;
pb = B.data;
pa_last = A.data + A.length - 1;
pb_last = B.data + B.length - 1;
C.length = A.length + B.length;
C.data = new int[C.length];
pc = C.data;
while (pa <= pa_last && pb <= pb_last) {
if (*pa <= *pb) {
*pc= *pa;
pc++;
pa++;
}
else
{
*pc = *pb;
pc++;
pb++;
}
}
while (pa <= pa_last) {
*pc = *pa;
pc++;
pa++;
}
while (pb <= pb_last) {
*pc = *pb;
pc++;
pb++;
}
}
int main() {
SeqList A, B, C;
InitList(A);
InitList(B);
CreateList(A);
CreateList(B);
printf("顺序表A元素: ");
PrintList(A);
printf("顺序表B元素: ");
PrintList(B);
MergeList(A, B, C);
PrintList(C);
}
算法思路:
void MergeList(SeqList A, SeqList B, SeqList &C) {
int* pa, * pb, * pa_last, * pb_last, * pc;
pa = A.data;
pb = B.data;
pa_last = A.data + A.length - 1;
pb_last = B.data + B.length - 1;
C.length = A.length + B.length;
C.data = new int[C.length];
pc = C.data;
while (pa <= pa_last && pb <= pb_last) {
if (*pa <= *pb) {
*pc= *pa;
pc++;
pa++;
}
else
{
*pc = *pb;
pc++;
pb++;
}
}
while (pa <= pa_last) {
*pc = *pa;
pc++;
pa++;
}
while (pb <= pb_last) {
*pc = *pb;
pc++;
pb++;
}
}
与普通顺序表结构体中存储元素不同 为了方便后续的合并 我们将元素改成指向第一个元素的指针
对于顺序表A和B,当两个表的首指针没有走到末尾的时候,分别对两个表的元素进行比较
将较小的那一个放入新的C表中 并将小元素对应的指针向后移动一位 再与另一个表中比较
以此类推 直至一个表中元素全部遍历完放入C表 接下来把剩下表中元素全部加入C表末尾即可
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 15
typedef struct {
int data[MAXSIZE];
int length;
}SeqList;
void InitList(SeqList &L) {
L.length = 0;
for (int i = 0; i < MAXSIZE; i++) {
L.data[i] = 0;
}
}
void PrintList(SeqList L) {
printf("顺序表中元素:");
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
}
int length(SeqList L) {
return L.length;
}
bool Empty(SeqList L) {
if (L.length == 0)
return true;
else
return false;
}
//在位序为i的位置上插入元素e
bool ListInsert(SeqList &L, int i, int e) {
if (i > L.length+1|| i < 1)
return false;
if (L.length > MAXSIZE)
return false;
for (int j = L.length; j > i-1; j--) {
L.data[j] = L.data[j- 1];
}
L.data[i - 1] = e;
L.length++;
return true;
}
void CreateList(SeqList &L,int n) {
L.length = n;
printf("请输入顺序表中元素:\n");
for (int i = 0; i < n; i++) {
scanf_s("%d", &L.data[i]);
}
}
//获得第i个位置上的元素
int GetElem(SeqList L, int i,int &e) {
int j = 0;
if (i<1 || i>L.length)
return 0;
while (j < i-1)
j++;
e = L.data[j];
return e;
}
//查找指定元素
bool LocateElem(SeqList L, int e) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == e)
return true;
}
return false;
}
void MergeList(SeqList &LA, SeqList &LB) {
int l1, l2,e1,e;
l1 = length(LA);
l2 = length(LB);
for (int i = 1; i <= l1; i++) {
e = GetElem(LA, i, e1);
if (LocateElem(LB, e) == false) {
ListInsert(LB, l2, e);
l2++;
}
}
}
int main() {
SeqList LA, LB, LC;
int n1, n2, n3;
printf("请分别输入两个顺序表的长度:\n");
scanf_s("%d", &n1);
scanf_s("%d", &n2);
InitList(LA);
InitList(LB);
InitList(LC);
CreateList(LA, n1);
CreateList(LB, n2);
PrintList(LA);
PrintList(LB);
MergeList(LA,LB);
printf("\n合并后的顺序表为:\n");
PrintList(LB);
}
上方是线性表的合并 思路比较简单 完成LocateElem,GetElem,ListInsert即可