ArrayList.h :结构的定义,基本函数的申明
#ifndef __ARRAYLIST_H__
#define __ARRAYLIST_H__
//一些库函数的头文件包含
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
//自定义bool
typedef int Boolean;
//定义函数返回状态
typedef int Status;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
/* 数组扩容时,申请的内存大小 */
#define LIST_INCREAMENT 10
/* 线性表结构类型的定义 */
typedef struct Arr
{
int *pBase;//数组第一个元素的地址
int len; //数组最大容量
int cnt; //当前数组有效元素的个数
}*Arrp,Arr;
/* 线性表的初始化 ,指定线性表的长度*/
Status init(Arrp *ap);
/* 线性表空间的释放 */
Status destroy(Arrp *ap);
/* 重置表 */
Status clear(Arrp p);
/* 判断空 */
Boolean isEmpty(Arr p);
/* 判断是否达到最大容量 */
Status isFull(Arr p);
/* 获取已存元素的数量 */
int listLen(Arrp ap);
/* 线性表的插入 */
Status insertElem(Arrp p, int pos, int x);
/* 线性表元素的删除 */
Status deleteElem(Arrp p, int pos);
/* 获取元素 */
Status getElem(Arrp p, int pos, int *elem);
/* 根据指定的函数关系,获取第一个满足关系的元素索引 */
int LocateElem(Arrp p, int elem, Status (*compare)(int, int));
/* 获取元素的前驱 */
Status PriorElem(Arr p, int pos, int *pre);
/* 获取元素的后继 */
Status NextElem(Arr p, int pos, int *next);
/* 根据指定的函数关系,对元素进行操作 */
void traverse(Arrp p, int pos, void (*vi)(int *));
#endif
ArrayList.c : 函数的实现
#include "ArrayList.h"
/* 线性表的初始化 ,指定线性表的长度*/
Status init(Arrp *ap)
{
//分配一个结构单元的空间,并让指针变量指向他
*p = (Arrp)malloc(sizeof(Arr));
if((*p) == NULL)
{
printf("动态内存分配失败");
exit(-1);
}
//分配单元中的base成员的空间
(*p)->pBase = (int *)malloc(sizeof(int)*LIST_INCREAMENT);
if((*p)->pBase == NULL)
{
printf("动态内存分配失败");
exit(-1);
}
(*p)->len = 0;
(*p)->cnt = 0;
return OK;
}
/* 线性表空间的释放 */
Status destroy(Arrp *ap)
{
free((*ap)->pBase);
free((*ap));
*ap = NULL;
return OK;
}
/* 重置表 */
Status clear(Arrp p)
{
p->cnt = 0;
p->len = 0;
free(p->pBase);
p->pBase = (int *)malloc(sizeof(int) * LIST_INCREAMENT);
return OK;
}
/* 判断空 */
Boolean isEmpty(Arr p)
{
if(p->cnt == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
/* 判断是否达到最大容量 */
Status isFull(Arr p)
{
if(p->len == p->cnt)
{
return TRUE;
}
else
{
return FALSE;
}
}
/* 获取已存元素的数量 */
int listLen(Arrp ap)
{
return ap->cnt;
}
/* 线性表的插入 */
Status insertElem(Arrp p, int pos, int x)
{
int i;
int *newBase;
//如果空间满了
if(isFull(p) == TRUE)
{
newBase = (int *)realloc(p->pBase, (LIST_INCREAMENT + P->len)*sizeof(int));
if(newBase == NULL)
{
printf("动态内存分配失败");
exit(-1);
}
p->pBase = newBase;
p->len += LIST_INCREAMENT;
}
//超过索引范围
if(pos < 0 || pos > p->cnt-1)
{
return ERROR;
}
for(i=p->cnt-1; i>=pos; i--)
{
p->pBase[i+1] = p->pBase[i];
}
p->pBase[pos] = x;
p->cnt++;
return OK;
}
/* 线性表元素的删除 */
Status deleteElem(Arrp p, int pos)
{
int i;
//超过索引范围
if(pos < 0 || pos > p->cnt-1)
{
return ERROR;
}
for(i=pos;i<p->cnt;i++)
{
p->pBase[i] = p->pBase[i+1];
}
p->cnt--;
return OK;
}
/* 获取元素 */
Status getElem(Arrp p, int pos, int *elem)
{
//超过索引范围
if(pos < 0 || pos > p->cnt-1)
{
return ERROR;
}
*elem = p->pBase[pos];
return OK;
}
/**
* 根据指定的函数关系,获取第一个满足关系的元素索引
* compare函数类型的返回值只能是0或1,满足条件返回1,否则返回0
**/
int LocateElem(Arrp p, int elem, Status (*compare)(int, int))
{
int i;
int * b = p-pBase;
for(i=0; i<p->cnt; i++)
{
if(compare(*b++, elem))
{
return i;
}
}
return -1;
}
/* 获取元素的前驱 */
Status PriorElem(Arr p, int pos, int *pre)
{
//超过索引范围,或者是第一个元素
if(pos <= 0 || pos > p->cnt-1)
{
return ERROR;
}
//第一个元素没有前驱
// if(pos == 0)
//{
// return INFEASIBLE;
// }
*pre = p->pBase[pos-1];
return OK;
}
/* 获取元素的后继 */
Status NextElem(Arr p, int pos, int *next)
{
//超过索引,或者是最后一个元素
if(pos < 0 || pos >= p->cnt-1)
{
return ERROR;
}
*next = p->pBase[pos+1];
return OK;
}
/* 根据指定的函数关系,对元素进行操作 */
void traverse(Arrp p, int pos, void (*vi)(int *))
{
int i=0;
while(i < p->cnt)
{
vi(p->pBase[i]);
i++;
}
return;
}