# 数据结构 线性表 顺序表合并

#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");
}

int ListInsert_Sq(SqList &L, int i, ElemType e)
{
ElemType *newbase,*p,*q;
if (i<1 || i>L.length+1)
{
return ERROR;
}
if (L.length >= L.listsize)
{
newbase = (ElemType *)realloc(L.elem ,(L.listsize + LISTLNCREMENT) * sizeof(LISTLNCREMENT));
if (!newbase)
{
exit(OVERFLOW);
}
L.elem = newbase;
L.listsize += LISTLNCREMENT;
}
q = &(L.elem [i-1]);
for (p=&(L.elem[L.length-1]);p>=q;--p)
{
*(p+1) = *p;
}
*q = e;
++L.length ;
return OK;
}

int equal(ElemType i, ElemType j)
{
return (i==j);
}

int LocateElem_Sq(SqList L, ElemType e, int (*compare)(ElemType, ElemType))
{
ElemType *p;
int i = 1;
p = L.elem;
while (i <= L.length  && !(*compare)(*p++,e))
{
++i;
}
if (i<=L.length)
{
return i;
}
else
{
return 0;
}
}

void GetElem(SqList L,int i, ElemType &e)
{
e = L.elem[i-1];
}

void Listunion(SqList &La, SqList Lb)
{
int i,e;
int la_len = La.length ;
int lb_len = Lb.length ;
for (i=1; i<=lb_len; i++)
{
GetElem(Lb,i,e);
if (!LocateElem_Sq(La,e,equal))
{
ListInsert_Sq(La,++la_len, e);
}
}
}

int main()
{
int n;
SqList La,Lb;
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);
printf("合并后的两个链表是:");
Listunion(La,Lb);
Display_Sq(La);

return 0;
}

• 本文已收录于以下专栏：

## 数据结构 顺序表及合并

include ""是先从本地目录开始寻找，然后去寻找系统路径 而Include <> 相反先从系统目录，后从本地目录
• iaiti
• 2013-07-27 12:51
• 2696

## 数据结构--合并顺序表

/** * 有序顺序表的合并: 已知顺序表LA、LB中的数据元素按值非递减有序排列， * 现要求将LA和LB归并为一个新的顺序表LC，且LC中的数据元素仍按值非递减有序排列 */ #includ...
• T_P_F
• 2017-06-30 09:24
• 50

## 线性结构：顺序表

1. 顺序表结构和操作定义 SeqList.h #include #include #include using namespace std; #define MAXSIZE 100 //定义顺序表...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)