您的位置:首页 > 编程语言 > C语言/C++

C语言实现顺序表

2011-05-07 21:25 351 查看
#include <stdio.h>
#include <assert.h>
#define OVERFLOW -1
#define LIST_INIT_SIZE 100	//线性表存储空间的初始分配量
#define LISTINCREMENT 10	//线性表存储空间的分配量
typedef int ElemTye;	//定义元素类型
#define true 1
#define false 0
typedef int bool;
//定义线性表存储结构
typedef struct
{
ElemTye *elem;	//存储空间基址
int length;		//当前长度
int size;		//当前分配存储容量
}SqList;
//初始化线性表
bool InitList_Sq (SqList *L)
{
assert(L != NULL);
//构造一个空的线性表
L->elem = (ElemTye *)malloc(LIST_INIT_SIZE*sizeof(ElemTye));
if (! L->elem)
{
return false;
exit(OVERFLOW);	//存储分配失败
}
L->length = 0;					//空表长度为0
L->size = LIST_INIT_SIZE;		//初始存储容量
return true;
}
//销毁线性表L
bool DestroyList (SqList *L)
{
assert(L != NULL);
if (L->elem != NULL)
{
free(L->elem);
L->length = 0;
L->size = 0;
L->elem = NULL;
L = NULL;
return true;
}

return false;
}
//清空线性表
bool ClearList (SqList *L)
{
assert(L != NULL);
if (L->elem != NULL)
{
L->length = 0;
return true;
}
return false;
}
//返回线性表中元素的个数
int ListLength (SqList *L)
{
assert(L != NULL);
if (L->elem != NULL)
{
return L->length;
}
else
{
return -1;
}
}
//线性表中location位置插入新的数据元素
bool ListInsert(SqList *L, int location, ElemTye e)
{
SqList *newbase = NULL;
ElemTye *q, *p;
assert(L != NULL);
if ((location<1) || (location>L->length+1))
return false;
if (L->length >= L->size)
{
newbase = (ElemTye *)realloc(L->elem, (L->size+LISTINCREMENT)*sizeof(ElemTye));
if ( !newbase )
exit(OVERFLOW);
L->elem = newbase;
L->size += LISTINCREMENT;
}
//q为插入位置
q = &(L->elem[location-1]);
//插入位置之后的元素后移
for (p=&(L->elem[L->length-1]); p>=q; --p)
*(p+1) = *p;
*q = e;
++(L->length);
return true;
}
//删除线性表中的第location个元素
bool ListDelete (SqList *L, int location, ElemTye *reElem)
{
ElemTye *p = NULL, *q = NULL;
assert(L != NULL);
if (location<1 || location>L->length)
return false;
p = &(L->elem[location-1]);//删除元素的位置
*reElem = *p;//返回删除的元素
q = L->elem + L->length -1;//表尾位置
for (++p; p<=q; p++)
*(p-1) = *p;	//被删除元素之后的元素左移
-- L->length;	//表长减一
return true;
}
//从开头打印出线性表中的元素
bool SqListPrint(SqList *L)
{
int i, len;
assert(L != NULL);
len = L->length;
for (i=1; i<=len; i++)
{
printf("%d/n", L->elem[i-1]);
}
return true;
}
//返回线性表中第location个数据的元素
bool GetElem(SqList *L, int location, ElemTye *e)
{
assert(L != NULL);
if (location<1 || location>L->length)
{
return false;
}
else
{
*e = L->elem[location-1];
return true;
}
}
int main(void)
{
//构造线性表
SqList *sq = NULL;
ElemTye elem;
sq = (SqList *)malloc(sizeof(SqList)*1);
if (sq != NULL)
{
InitList_Sq(sq);
}
ListInsert(sq, 1,11);
ListInsert(sq, 2,12);
ListInsert(sq, 3,13);
ListInsert(sq, 4,14);
ListInsert(sq, 5,15);
ListInsert(sq, 1,10);
ListInsert(sq, 1,9);
//打印出线性表中的元素
SqListPrint(sq);
//删除线性表中指定元素
if (ListDelete(sq, 1, &elem))
{
printf("被删除的元素是:%d/n", elem);
}
if (ListDelete(sq, 6, &elem))
{
printf("被删除的元素是:%d/n", elem);
}

//输出元素的个数
printf("%d/n", ListLength(sq));
//获得第3个元素
GetElem(sq, 3, &elem);
printf("第3个元素是:%d/n", elem);
//清空线性表
ClearList(sq);
//输出元素的个数
printf("%d/n", ListLength(sq));
//销毁线性表
DestroyList(sq);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: