//线性表的顺序表示及实现
#include
#include
#include
#include
using namespace std;
//------线性表的动态分配顺序存储结构------
#define LIST_INIT_SIZE 100//线性表存储空间初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;//存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
} SqList;
Status InitList(SqList &L)
{
//构造一个空的线性表L
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
{
printf("分配失败!\n");
return ERROR;//内存分配失败
}
L.length = 0; //空表长度为0
L.listsize = LIST_INIT_SIZE;//初始存储容量
return OK;
}
Status DestroyList(SqList &L)
{
if(L.elem)
{
free(L.elem);
L.length = 0;
L.listsize = 0;
return OK;
}
return ERROR;
}
Status ClearList(SqList &L)
{
if(L.elem)
{
L.length = 0;
return OK;
}
return ERROR;
}
Status ListEmpty(SqList L)
{
if(L.elem)
{
if(L.length == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
return ERROR;
}
Status ListLength(SqList L)
{
if(L.elem)
{
return L.length;
}
return ERROR;
}
Status GetElem(SqList L, int i, int &e)
{
if(L.elem && i >= 1 && i <= L.length)
{
e = *(L.elem + i - 1);
return OK;
}
return ERROR;
}
Status LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType))
{
if(L.elem)
{
int i = 1;
ElemType *p;
p = L.elem;
while((i <= L.length) && (!(*compare)(*p++, e)))
{
++i;
}
if(i <= L.length)
{
return i;
}
else
{
return 0;
}
}
return ERROR;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
if(L.elem)
{
int i = 2;
ElemType p;
p = *(L.elem) + 1;
while(i L.length)
{
return FALSE;
}
else
{
pre_e = --p;
return OK;
}
}
return ERROR;
}
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e)
{
if(L.elem)
{
int i = 1;
ElemType p;
p = *(L.elem);
while(i= L.length)
{
return FALSE;
}
else
{
next_e = ++p;
return OK;
}
}
return ERROR;
}
Status ListInsert(SqList &L, int i, ElemType e)
{
ElemType *newbase, *q, *p;
if(i<1 || i>L.length+1)
{
return ERROR;
}
if(L.length >= L.listsize)
{
newbase = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
{
return ERROR;
}
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = L.elem + i -1;
for(p = L.elem+L.length-1; p>=q; --p)
{
*(p+1) = (*p);
}
*q = e;
++L.length;
return OK;
}
Status ListDelete(SqList &L, int i, ElemType &e)
{
ElemType *q, *p;
if(i>=1 && i<=L.length+1)
{
return ERROR;
}
p = L.elem + i -1;
e = *p;
q = L.elem + L.length - 1;
for(++p; p<=q; ++p)
{
*(p-1) = *p;
}
--L.length;
return OK;
}
Status ListTraverse(SqList L, void (*vi)(ElemType*))
{
ElemType *p;
int i;
p = L.elem;
for(i = 1; i <= L.length; i++)
{
vi(p++);
}
printf("\n");
return OK;
}
void Print(ElemType *e)
{
printf("%d ", *e);
}
Status Equal(ElemType a, ElemType b)
{
if(a == b)
{
return TRUE;
}
else
{
return FALSE;
}
}
void Union(SqList &La, SqList Lb)
{
int la_len = ListLength(La);
int lb_len = ListLength(Lb);
int i;
ElemType e;
for(i = 1; i <= lb_len; i++)
{
GetElem(Lb, i, e);
if(!LocateElem(La, e, Equal))
{
ListInsert(La, ++la_len, e);
}
}
}
int main()
{
SqList La,Lb;
int j;
if(InitList(La) == 1) /* 创建空表La成功 */
{
for(j = 1; j <= 5; j++) /* 在表La中插入5个元素 */
{
ListInsert(La, j, j);
}
}
printf("La= "); /* 输出表La的内容 */
ListTraverse(La, Print);
InitList(Lb); /* 也可不判断是否创建成功 */
for(j = 1; j <= 5; j++) /* 在表Lb中插入5个元素 */
{
ListInsert(Lb, j, 2*j);
}
printf("Lb= "); /* 输出表Lb的内容 */
ListTraverse(Lb, Print);
Union(La, Lb);
printf("new La= "); /* 输出新表La的内容 */
ListTraverse(La, Print);
return 0;
}