转载自:http://blog.csdn.net/greenhandcgl/article/details/44859637
顺序表结构的实现:
-
#include
-
#include
-
#include
-
-
#define LIST_INT_SIZE 100
-
#define LISTINCREMENT 10
-
#define OK 1
-
#define ERROR 0
-
#define TRUE 1
-
#define FALSE 0
-
#define OVERFLOW -1
-
-
typedef int ElemType;
-
typedef int Status;
-
-
typedef struct{
-
ElemType *elem;
-
int length;
-
int listsize;
-
}SqList;
-
-
Status InitList(SqList *L)
-
{
-
L->elem = (ElemType *)malloc(LIST_INT_SIZE * sizeof(ElemType));
-
if(!L->elem) exit(OVERFLOW);
-
L->length = 0;
-
L->listsize = LIST_INT_SIZE;
-
return OK;
-
}
-
-
Status DestroyList(SqList *L)
-
{
-
if(!L->elem) exit(ERROR);
-
free(L->elem);
-
L->elem = NULL;
-
return OK;
-
}
-
-
Status CreatList_Sq(SqList *L,int n)
-
{
-
int i;
-
printf("输入%d个整数:\n",n);
-
for(i=0;i
-
scanf("\n%d",&L->elem[i]);
-
return OK;
-
}
-
-
-
Status ClearList(SqList *L)
-
{
-
if(!L->elem) return ERROR;
-
L->length = 0;
-
return OK;
-
}
-
-
Status ListEmpty(SqList *L)
-
{
-
if(!L->elem || L->length < 0 || L->length > L->listsize) return ERROR;
-
if(L->length == 0)
-
return TRUE;
-
else
-
return FALSE;
-
}
-
-
Status ListLength(SqList *L)
-
{
-
if(!L->elem || L->length < 0 || L->length > L->listsize)
-
return ERROR;
-
return L->length;
-
}
-
-
Status GetElem(SqList *L, int i, ElemType *e)
-
{
-
if(!L->elem || i < 0 || i > L->length) return ERROR;
-
*e = L->elem[i-1];
-
return OK;
-
}
-
-
Status LocatElem(SqList *L, ElemType e, int compare(void *, void *))
-
{
-
if(!L->elem) return ERROR;
-
int i;
-
for(i = 0; i < L->length; i++){
-
if(compare((ElemType *)e, (ElemType *)L->elem[i]) == TRUE)
-
return i+1;
-
}
-
return FALSE;
-
}
-
-
Status PriorElem(SqList *L, ElemType cur_e, ElemType *pre_e)
-
{
-
int i;
-
for (i = 1; i < L->listsize; i++) {
-
if (L->elem[i] == cur_e) {
-
*pre_e = L->elem[i-1];
-
return OK;
-
}
-
}
-
return FALSE;
-
}
-
-
Status NextElem(SqList *L, ElemType cur_e, ElemType *next_e)
-
{
-
int i;
-
for (i=0; i < L->listsize - 1; i++) {
-
if (L->elem[i] == cur_e) {
-
*next_e = (int )L->elem[i+1];
-
return OK;
-
}
-
}
-
return FALSE;
-
}
-
-
-
Status ListInsert(SqList *L, int i, ElemType e)
-
{
-
ElemType *newbase;
-
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) exit(OVERFLOW);
-
L->elem = newbase;
-
L->listsize += LISTINCREMENT;
-
}
-
ElemType *q = &(L->elem[i-1]);
-
ElemType *p;
-
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)
-
{
-
if(i<1 || i>L->length) return ERROR;
-
ElemType *p = &(L->elem[i-1]);
-
e = *p;
-
ElemType *t = &(L->elem[L->length-1]);
-
for(++p; p <= t; ++p) *(p-1) = *p;
-
--L->length;
-
return OK;
-
}
-
-
Status ListTraverse(SqList *L, int (* visit)(void *))
-
{
-
int i;
-
for (i = 0; i < L->length; i++)
-
-
if (!visit((int *)L->elem[i]))
-
return ERROR;
-
return TRUE;
-
}
-
-
Status ListPrint(SqList *L)
-
{
-
int i;
-
if (!L->elem || L->length < 1 || L->length > L->listsize){
-
printf("empty!\n");
-
return ERROR;
-
}
-
for (i = 0; i < L->length; i++) {
-
printf("%5d ", L->elem[i]);
-
}
-
printf("\n");
-
return TRUE;
-
}
测试代码:
-
int main()
-
{
-
int i,n;
-
SqList a;
-
SqList *l = &a;
-
if(InitList(l)==-2) printf("分配失败");
-
printf("\n输入要建立的线性表l的长度n:");
-
scanf("%d",&n);
-
l->length=n;
-
printf("线性表的长度是:%d\n",l->length);
-
CreatList_Sq(l,n);
-
puts("原序:");
-
ListPrint(l);
-
puts("");
-
int ce, pe;
-
printf("请输入要查找前驱的元素:");
-
scanf("%d", &ce);
-
PriorElem(l, ce, &pe);
-
printf("%d的前驱是%d\n", ce, pe);
-
NextElem(l, ce, &pe);
-
printf("%d的后继是%d\n", ce, pe);
-
-
printf("请输入要插入的元素和插入位置:");
-
scanf("%d%d", &ce,&pe);
-
ListInsert(l, pe,ce);
-
printf("插入元素%d后的线性表为\n", ce);
-
ListPrint(l);
-
-
printf("请输入要删除元素位置:");
-
scanf("%d", &ce);
-
ListDelete(l, ce, pe);
-
printf("删除%d位置的font-family: Arial, Helvetica, sans-serif;">元素font-family: Arial, Helvetica, sans-serif;">后的线性表为\n", ce);
-
ListPrint(l);
-
DestroyList(l);
-
return 0;
运行结果:
阅读(1041) | 评论(0) | 转发(0) |