您的位置:首页 > 其它

线性表的顺序表示和实现

2011-12-01 14:31 453 查看
-----------------------------------------main.c-------------------------------------------------

#include <stdio.h>

#include "sqlist.h"

int main(int argc, char *argv[])

{

SqList *La = (SqList*) malloc(sizeof(SqList));

SqList *Lb = (SqList*) malloc(sizeof(SqList));

SqList *Lc = (SqList*) malloc(sizeof(SqList));

InitList_Sq(La);

InitList_Sq(Lb);

InitList_Sq(Lc);

ElemType *e;

e = (ElemType*) malloc(sizeof(ElemType));

int i;

for (i = 1; i <= LIST_INIT_SIZE; i += 2)

{

e->idx = i;

e->value = i;

ListInsert_Sq(La, La->length + 1, e);

}

printf("初始化后,顺序表La的状态为:\n");

PrintStatus_Sq(La);

for (i = 2; i <= LIST_INIT_SIZE; i += 3)

{

e->idx = i;

e->value = i;

ListInsert_Sq(Lb, Lb->length + 1, e);

}

printf("初始化后,顺序表Lb的状态为:\n");

PrintStatus_Sq(Lb);

MergeList_Sq(La, Lb, Lc);

printf("合并两表后,顺序表Lc的状态为:\n");

PrintStatus_Sq(Lc);

return OK;

}

-----------------------------------------sqlist.h-------------------------------------------------

#ifndef SQLIST_H_

#define SQLIST_H_

#include <malloc.h>

#include <stdlib.h>

#define TRUE                (1)

#define FALSE                (0)

#define OK                    (1)

#define ERROR                (0)

#define INFEAZIBLE            (-1)

#define OVERFLOW            (-2)

#define LIST_INIT_SIZE        (10)

#define LISTINCREMENT        (10)

typedef int Status;

/*

* 定义表中的元素

*/

typedef struct

{

int idx;

int value;

} ElemType;

/*

* 定义线性表

*/

typedef struct

{

ElemType *elem; //存储空间基址

int length; //当前长度

int listsize; //当前分配的存储容量,以sizeof(ElemType)为单位

} SqList;

Status InitList_Sq(SqList* const L);

Status ListInsert_Sq(SqList* const L, const int i, ElemType const *e);

Status ListDelete_Sq(SqList* const L, const int i, ElemType* const e);

int LocateElem_Sq(const SqList *L, const ElemType *e, Status(*compare)(

const ElemType*, const ElemType*));

Status equalElem_Sq(const ElemType *e1, const ElemType *e2);

int ListLength_Sq(const SqList *L);

void PrintStatus_Sq(const SqList *L);

Status MergeList_Sq(const SqList *a, const SqList *b, SqList *c);

#endif /* SQLIST_H_ */

-----------------------------------------sqlist.c-------------------------------------------------

#include "sqlist.h"

#include <stdio.h>

/*

* 线性表的初始化,初始化后,线性表可容纳LIST_INT_SIZE,即10个元素

*/

Status InitList_Sq(SqList* const L)

{

L->elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType));

if (!L->elem)

exit(OVERFLOW); //存储分配失败

L->length = 0; //空表长度为0

L->listsize = LIST_INIT_SIZE; //初始存储容量

return OK;

}//InitList_Sq()

/*

* 求表长,也即元素个数

*/

int ListLength_Sq(const SqList *L)

{

return L->length;

}//ListLength_Sq

/*

* 判断两个元素是否相同,是则返回真,否则返回假

*/

Status equalElem_Sq(const ElemType *e1, const ElemType *e2)

{

if (e1->idx == e2->idx && e1->value == e2->value)

return TRUE;

else

return FALSE;

}//equalElem_Sq

/*

* 按compare函数所表示的条件查找元素在线性表中的位置

*/

int LocateElem_Sq(const SqList *L, const ElemType *e, Status(*compare)(

const ElemType*, const ElemType*))

{

return 0;

}//LocateElem_Sq

/*

* 在顺序线性表L的第i个位置前插入新的元素e,这里i的合法值为:1 <= i <= ListLength_Sq(L) + 1

*/

Status ListInsert_Sq(SqList* const L, const int i, ElemType const *e)

{

if (i < 1 || i > ListLength_Sq(L) + 1)

return ERROR; //i值不合法

if (L->length >= L->listsize)

{

ElemType *newbase;

newbase = (ElemType*) realloc(L->elem, (L->listsize

+ LISTINCREMENT) * sizeof(ElemType));

if (!newbase)

exit(OVERFLOW); //存储分配失败

L->elem = newbase; //新基址

L->listsize += LISTINCREMENT; //增加存储容量

}

ElemType *q = &(L->elem[i - 1]); //q为插入点位置

ElemType *p;

for (p = &(L->elem[L->length - 1]); p >= q; --p)

*(p + 1) = *p; //插入位置及其以后的元素右移一位

*q = *e; //插入e

++L->length; //表长增1

return OK;

}//ListInsert_Sq

/*

* 在顺序线性表中删除第i个元素,并用e返回其值

*/

Status ListDelete_Sq(SqList* const L, const int i, ElemType* const e)

{

if (i < 1 || i > L->length)

return ERROR; //i值不合法

ElemType *p = &(L->elem[i - 1]);

*e = *p;

ElemType *last = L->elem + L->length - 1;

for (; p < last; ++p)

*p = *(p + 1);

--L->length;

return OK;

}//ListDelete_Sq

/*

* 打印线性表L中的所有元素

*/

void PrintStatus_Sq(const SqList *L)

{

printf("当前顺序线性表中共有元素%d个,分别为:\n", L->length);

int i;

for (i = 1; i <= L->length; ++i)

{

int j;

for (j = 0; j < 10; ++j)

putchar('-');

printf("%d", i);

for (j = 0; j < 10; ++j)

putchar('-');

printf("\n%d\n%d\n", L->elem[i - 1].idx, L->elem[i - 1].value);

}

}

/*

* 已知顺序线性表La和Lb的元素按值非递减排列,归并La和Lb得到新的顺序线性表Lc,Lc的元素也按值非递减排列

*/

Status MergeList_Sq(const SqList *La, const SqList *Lb, SqList *Lc)

{

ElemType *pa, *pb, *pa_last, *pb_last;

pa = La->elem, pb = Lb->elem;

pa_last = pa + La->length - 1;

pb_last = pb + Lb->length - 1;

while (pa <= pa_last && pb <= pb_last)

if (pa->value <= pb->value)

ListInsert_Sq(Lc, Lc->length + 1, pa++);

else

ListInsert_Sq(Lc, Lc->length + 1, pb++);

while (pa <= pa_last)

ListInsert_Sq(Lc, Lc->length + 1, pa++);

while (pb <= pb_last)

ListInsert_Sq(Lc, Lc->length + 1, pb++);

return OK;

}//MergeList_Sq
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  list 存储 struct c