数据结构线性表基本操作(C语言)
- 格式:docx
- 大小:18.15 KB
- 文档页数:7
#include
#include
#include
#define TRUE1
#define FALSE0
#define OK1
#define ERROR0
#define INFEASIBLE-1
#define OVERFLOW-2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE100
#define LISTINCREMENT10
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *L); //构造空的线性表
void DestroyList_Sq(SqList *L);//销毁一个线性表
void ClearList_Sq (SqList *L); //将L置为空表
Status ListEmpty_Sq (SqList L);//空表返回TRUE
Status ListLength_Sq (SqList L);// 返回元素个数
Status GetElem_Sq (SqList L, int i, ElemType *e);//用e返回第i个元素算法2.2中使用
Status LocateElem_Sq(SqList L, ElemType e, Status (* compare)(ElemType, ElemType));// 在L中找到一个值与e满足compare()的元素的位序
Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType *pre_e);
//用pre_e返回cur_e的前驱
Status NextElem_Sq(SqList L, ElemType cur_e, ElemType *next_e);
//用next_e返回cur_e的后继
Status ListInsert_Sq(SqList *L, int i, ElemType e);//在第i位插入新的元素e
Status ListDelete_Sq(SqList *L, int i, ElemType *e);//删除第i个元素用e返回
//算法2.3
Status InitList_Sq(SqList *L) // 构造空的线性表
{
L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (! L->elem)
{
printf("构造失败!\n");
exit(OVERFLOW);
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
printf("构造成功!\n");
return OK;
}
void DestroyList_Sq(SqList *L)// 销毁一个线性表{
if (L->elem != NULL)
{
free (L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
printf("已销毁线性表!\n");
}
}
void ClearList_Sq (SqList *L)//将L置为空表
{
if(L->elem != NULL)
{
L->length = 0;
printf("已将L置为空表!\n");
}
}
Status ListEmpty_Sq (SqList L)// 空表返回TRUE
{
if (L.elem != NULL)
{
if (L.length == 0)
{
printf("是空表\n");
return TRUE;
}
else
{
printf("不是空表\n");
return FALSE;
}
}
else
{
exit(ERROR);
}
}
Status ListLength_Sq (SqList L)// 返回元素个数
{
if (L.elem != NULL)
{
return L.length;
}
else
{
return ERROR;
}
}
Status GetElem_Sq (SqList L, int i, ElemType *e)//用e返回第i个元素算法2.2中使用
{
if (ListEmpty_Sq(L))
{
printf("为空表!\n");
return ERROR;
}
if (i < 1 || i > L.length)
{
printf("不存在地%d个位置!\n", i);
return ERROR;
}
*e = L.elem[i - 1];
return OK;
}
//算法2.6
Status LocateElem_Sq(SqList L, ElemType e, Status (* compare)(ElemType, ElemType))//