一、头文件:
/*文件SqList.h
*线性表的顺序表数据结构声明及方法实现
*/
#include <malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
/* 线性链表存储空间初始分配空间大小 */
#define LIST_INIT_SIZE 100
/* 线性链表存储空间的分配增量 */
#define LISTINCREMENT 10
typedef struct{
int *array; /* 存储空间起始地址 */
int length; /* 当前长度 */
int listSize; /* 当前分配的存储容量(以sizeof(int))为单位 */
}sqList;
int sqListInit(sqList *L)
{
/* 构造一个空的线性表L */
L->array = (int *)malloc(LIST_INIT_SIZE*sizeof(int)); /* 动态分配空间,L->array指向空间首地址 */
if(!L->array)exit(OVERFLOW); /* 分配空间失败 */
L->length = 0; /* 链表当期长度初始为0 */
L->listSize = LIST_INIT_SIZE; /* 初始存储容量 */
return OK;
}
/* L被插入的链表,i插入的位置,e插入的数据元素 */
int sqListInsert(sqList *L, int i, int e)
{
int j;
/* 判断L是否有效 */
if(L == NULL) return ERROR;
/* 判断i是否有效 */
if(i < 1 || i > L->length+1) return ERROR;
if(L->length >= L->listSize) /* 空间不足 */
{
int *newBase = (int *)realloc(L->array,(L->listSize+LISTINCREMENT)*sizeof(int));
if(!newBase) exit(OVERFLOW);
L->array = newBase;
L->listSize = L->listSize + LISTINCREMENT; /* 增加容量 */
}
/* i-1 及之后的数据元素,均向后移动一位 */
if(1 <= i && i <= L->length)
{
for(j = L->length - 1; j >= i-1; --j)
{
*(L->array+j+1) = *(L->array+j);
}
}
*(L->array+i-1) = e;
L->length++;
return OK;
}
/* i删除数据元素的位置,e保存被删除元素并返回 */
int sqListDelete(sqList *L, int i, int e)
{
int *p;
/* 判断L是否有效 */
if(L == NULL) return ERROR;
/* 判断i是否有效 */
if(i < 1 || i > L->length) return ERROR;
/* p初始指向被删除的数据元素 */
p = &(L->array[i-1]);
e = *p;/* 被删除的元素用e返回 */
/* 循环:依次把i-1之后的数据元素向前移动一位 */
for(++p; p <= L->array+L->length-1; ++p) *(p-1) = *p;
/* 链表长度减一 */
L->length--;
return OK;
}
int compare(int e1, int e2)
{
if(e1 == e2) return OK;
else return ERROR;
}
/* 查找e数据元素在L链表中的首位置,其中compare为指向函数指针 */
int sqListLocateItem(sqList *L, int e)
{
int i = 0;
int (*pCompare)(int e1, int e2);
pCompare = compare;
for(; i < L->length && !pCompare(*(L->array++),e);i++);
if(i < L->length) return i+1;
else
return ERROR;
}
/* 合并两个链表,通过Lc返回结果链表 */
int mergeSqList(sqList *La, sqList *Lb, sqList *Lc)
{
int *p1,*p2;
p1 = La->array;
p2 = Lb->array;
if(La == NULL && Lb == NULL)
{
Lc = NULL;
return ERROR;
}
Lc->array = (int*)malloc((La->length+La->length)*sizeof(int));
if(!Lc->array) exit(ERROR);
Lc->length = 0;
Lc->listSize = La->length+La->length;
/* 合并 */
while(p1 <= La->array+La->length-1 && p2 <= Lb->array+Lb->length-1)
{
if(*p1 < *p2)
{
*(Lc->array+Lc->length++) = *p1;
++p1;
}
else
{
*(Lc->array+Lc->length++) = *p2;
++p2;
}
}
/* 合并La或者Lb剩下的数据元素到Lc */
while(p1 <= La->array+La->length-1)
{
*(Lc->array+Lc->length++) = *p1;
++p1;
}
while(p2 <= Lb->array+Lb->length-1)
{
*(Lc->array+Lc->length++) = *p2;
++p2;
}
}
二、测试代码:
#include <stdio.h>
#include "SqList.h"
void main(void)
{
int i = 1;
int j = 0;
int locate = 0;
sqList List,ListB,ListC,ListD;
sqList *pList = &List;
sqList *pListB = &ListB;
sqList *pListC = &ListC;
sqList *pListD = &ListD;
if(sqListInit(pList))
{
for(; i <= 100&&sqListInsert(pList,i,i); i++);
}
else
exit(ERROR);
for(i = 0; i < List.length; i++)
{
printf("%d\t",*(List.array+i));
}
printf("\n");
printf("%d\n",List.length);
printf("%d\n\n",List.listSize);
getch();
sqListInsert(pList,1,101);
for(i = 0; i < List.length; i++)
{
printf("%d\t",*(List.array+i));
}
printf("\n");
printf("%d\n",List.length);
printf("%d\n\n",List.listSize);
getch();
for(i = 102; i <= 201&&sqListInsert(pList,i,i); i++);
for(i = 0; i < List.length; i++)
{
printf("%d\t",*(List.array+i));
}
printf("\n");
printf("%d\n",List.length);
printf("%d\n\n",List.listSize);
getch();
sqListDelete(pList,1,101);
for(i = 0; i < List.length; i++)
{
printf("%d\t",*(List.array+i));
}
printf("\n");
printf("%d\n",List.length);
printf("%d\n\n",List.listSize);
getch();
locate = sqListLocateItem(pList,10);
printf("%d\n\n",locate);
getch();
i = 1;
if(sqListInit(pListB))
{
for(; i <= 100&&sqListInsert(pListB,i,i); i++);
}
else
exit(ERROR);
for(i = 0; i < ListB.length; i++)
{
printf("%d\t",*(ListB.array+i));
}
printf("\n");
getch();
i = 51;
if(sqListInit(pListC))
{
for(j = 1; i <= 150&&sqListInsert(pListC,j,i); i++,j++);
}
else
exit(ERROR);
for(i = 0; i < ListC.length; i++)
{
printf("%d\t",*(ListC.array+i));
}
printf("\n");
getch();
mergeSqList(pListB,pListC,pListD);
for(i = 0; i < ListD.length; i++)
{
printf("%d\t",*(ListD.array+i));
}
printf("\n");
getch();
}