头文件
#ifndef _SEQUENCE_H_
#define _SEQUENCE_H_
#define MAXSIZE 20
#define FAILURE 1000000
#define SUCCESS 1000001
#define TRUE 1000002
#define FALSE 1000003
typedef int ElemType;
struct sequence
{
ElemType *data;
int size;
int length;
};
typedef struct sequence SeList;
int ListInit(SeList *s);
int ListInsert(SeList *s, int i, ElemType e);
int ListLength(SeList s);
int EmptyList(SeList s);
int GetElem(SeList s, int p, ElemType *e);
int ListTraverse(SeList s, void (*look)(ElemType));
int LocateElem(SeList s, ElemType e, int (*compare)(ElemType, ElemType));
#endif
接口函数
#include <stdio.h>
#include "SequenceList.h"
#include <stdlib.h>
int ListInit(SeList *s) //初始化
{
s->data = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE);
if (NULL == s->data)
{
return FAILURE;
}
s->size = MAXSIZE;
s->length = 0;
return SUCCESS;
}
int ListInsert(SeList *s, int i, ElemType e) //插入
{
int k;
if (i < 0 || i >= MAXSIZE)
{
return FAILURE;
}
if (s->length >= MAXSIZE)
{
return FAILURE;
}
for (k = 0; k < s->length - i; k++)
{
s->data[s->length - k] = s->data[s->length - k - 1];
}
s->data[i] = e;
s->length++;
return SUCCESS;
}
int ListLength(SeList s) //求长度
{
return s.length;
}
int EmptyList(SeList s) //判断是否为空表
{
return (s.length == 0) ? TRUE : FALSE;
}
int GetElem(SeList s, int p, ElemType *e) //获取表中元素
{
if (p < 0 || p >= MAXSIZE)
{
return FAILURE;
}
*e = s.data[p];
return SUCCESS;
}
int ListTraverse(SeList s, void (*look)(ElemType))//遍历
{
int i;
for (i = 0; i < s.length; i++)
{
look(s.data[i]);
}
return SUCCESS;
}
int LocateElem(SeList s, ElemType e, int (*compare)(ElemType, ElemType))
{
int i;
for (i = 0; i < s.length; i++)
{
if (compare(e, s.data[i]) == TRUE)
{
return i;
}
}
return FAILURE;
}
主函数
#include <stdio.h>
#include "SequenceList.h"
int equal(ElemType a, ElemType b)
{
return (a == b) ? TRUE : FALSE;
}
int large(ElemType a, ElemType b)
{
return (a <= b) ? TRUE : FALSE;
}
void visit(ElemType e)
{
printf("%d ", e);
}
void add(ElemType e)
{
printf("%d ", e + 1);
}
int main()
{
int ret, i;
SeList SList;
int place;
ElemType e;
ret = ListInit(&SList);
if (SUCCESS == ret)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
ret = EmptyList(SList);
if (TRUE == ret)
{
printf("List is Empty!\n");
}
else if (FALSE == ret)
{
printf("List is't Empty!\n");
}
for (i = 0; i < 6; i++)
{
ret = ListInsert(&SList, i, i + 1);
if (SUCCESS == ret)
{
printf("Insert %d Success!\n");
}
else
{
printf("Insert %d Failure!\n");
}
}
ret = EmptyList(SList);
if (TRUE == ret)
{
printf("List is Empty!\n");
}
else if (FALSE == ret)
{
printf("List is't Empty!\n");
}
printf("Length : %d\n", ListLength(SList));
place = 10;
ret = GetElem(SList, place, &e);
if(SUCCESS == ret)
{
printf("%d element is %d\n", place, e);
}
else
{
printf("Get %d Place Failure!\n", place);
}
ret = ListTraverse(SList, visit);
if (SUCCESS == ret)
{
printf("Traverse Success!\n");
}
else
{
printf("Traverse Failure!\n");
}
e = 4;
//ret = LocateElem(SList, e, equal);
ret = LocateElem(SList, e, large);
if (FAILURE == ret)
{
printf("Not Found!\n");
}
else
{
printf("%d is %dth element!\n", e, ret);
}
return 0;
}