ArrayList.h
/* 线性表的顺序存储结构 */
#define MAXSIZE 10 //存储空间初始分配量
typedef int ElemType ; //ElemType类型根据实际情况而定,这里假设为int
typedef struct
{
ElemType data[MAXSIZE]; //数组存储数据元素,最大值为MAXSIZE
int length; //线性表当前长度
}ArrayList;
#define OK 1
#define ERROR 0
typedef int Status;
/* 线性表通用操作 */
Status InitList(ArrayList *L); //建立一个空的线性表L
Status ListEmpty(ArrayList L); //若线性表为空返回true,否则返回false
Status ClearList(ArrayList *L); //将线性表清空
Status GetElem(ArrayList L,int i,ElemType *e); //将线性表L中第i个位置元素值返回给e
int LocateElem(ArrayList L,ElemType e); //在线性表L中查找与给定值e相等的元素,如果查找成功,返回该元素在表中序号表示成功;否则,返回0表示失败。
Status ListInsert(ArrayList *L,int i,ElemType e); //在线性表L中的第i个位置插入新元素e
Status ListDelete(ArrayList *L,int i,ElemType *e); //删除线性表L中第i个位置元素,并用e返回值值
int ListLength(ArrayList L); //返回线性表L的元素个数
ArrayList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
int main(void)
{
ArrayList L;
InitList(&L);
printf("length==%d\n",L.length);
if(ListEmpty(L))
{
printf("是空的\n");
ListInsert(&L,0,3);
ListInsert(&L,1,13);
ListInsert(&L,2,23);
ListInsert(&L,2,33);
ListInsert(&L,4,43);
ListInsert(&L,5,53);
ListInsert(&L,6,63);
ListInsert(&L,7,73);
ListInsert(&L,8,83);
ListInsert(&L,9,93);
ListInsert(&L,10,103);
ListInsert(&L,11,113);
ListInsert(&L,12,123);
}
printf("遍历所有元素:");
int len = ListLength(L);
for(int i=0;i<len;i++)
{
printf("[%d]%d,",i,L.data[i]);
}
printf("\n");
ElemType e;
GetElem(L,2,&e);
printf("索引2位置的元素是:%d\n",e);
printf("查找53的索引位置是:%d\n",LocateElem(L,53));
printf("删除第6位后\n");
ElemType ee;
ListDelete(&L,6,&ee);
len = ListLength(L);
for(int i=0;i<len;i++)
{
printf("[%d]%d,",i,L.data[i]);
}
printf("\n");
return 0;
}
//建立一个空的线性表L
Status InitList(ArrayList *L)
{
memset(L->data,0,sizeof(ElemType)*MAXSIZE);
L->length = 0;
return OK;
}
//若线性表为空返回true,否则返回false
Status ListEmpty(ArrayList L)
{
return L.length==0;
}
//将线性表清空
Status ClearList(ArrayList *L)
{
memset(L->data,0,sizeof(ElemType)*MAXSIZE);
L->length = 0;
return OK;
}
//将线性表L中第i个位置元素值返回给e
Status GetElem(ArrayList L,int i,ElemType *e)
{
if(i<0||i>L.length)
{
printf("index 非法!\n");
return ERROR;
}
*e = L.data[i];
return OK;
}
//在线性表L中查找与给定值e相等的元素,如果查找成功,返回该元素在表中序号表示成功;否则,返回0表示失败。
int LocateElem(ArrayList L,ElemType e)
{
for(int i=0;i<L.length;i++)
{
if(e == L.data[i])
{
return i;
}
}
return ERROR;
}
//在线性表L中的第i个位置插入新元素e
Status ListInsert(ArrayList *L,int i,ElemType e)
{
if(L->length>=MAXSIZE)
{
printf("线性表已满!\n");
return ERROR;
}
if(i<0||i>L->length)
{
printf("插入位置非法\n");
return ERROR;
}
else if(i<L->length && L->length<MAXSIZE)
{
for(int j=L->length;j>i;j--)
{//如果不在表尾往后移位
L->data[j] = L->data[j-1];
}
}
L->data[i] = e;
L->length ++;
return OK;
}
//删除线性表L中第i个位置元素,并用e返回值值
Status ListDelete(ArrayList *L,int i,ElemType *e)
{
if(ListEmpty(*L))
{
printf("线性表已空!\n");
return ERROR;
}
if(i<0||i>L->length)
{
printf("删除位置非法\n");
return ERROR;
}
*e = L->data[i];
if(i<L->length)
{
for(int j=i;j<L->length-1;j++)
{//往前移位
L->data[j] = L->data[j+1];
}
}
L->length --;
return OK;
}
//返回线性表L的元素个数
int ListLength(ArrayList L)
{
return L.length;
}
测试运行:
[yanhua@localhost chap03]$ gcc --std=c99 ArrayList.c
[yanhua@localhost chap03]$ ./a.out
length==0
是空的
线性表已满!
线性表已满!
线性表已满!
遍历所有元素:[0]3,[1]13,[2]33,[3]23,[4]43,[5]53,[6]63,[7]73,[8]83,[9]93,
索引2位置的元素是:33
查找53的索引位置是:5
删除第6位后
[0]3,[1]13,[2]33,[3]23,[4]43,[5]53,[6]73,[7]83,[8]93,
结论:
线性表的顺序存储结构时间复杂度:
随机查找时间复杂度为O(1)
插入和删除由于要移动元素,时间复杂度为O(n)
优缺点:
优点:
·无须为表示表中元素之间的逻辑关系而增加额外存储空间
·可以快速地存取表中任意位置的元素
缺点:
·插入和删除操作需要移动大量元素
·当线性表长度变化较大时,难以确定存储空间的容量
·造成存储空间的“碎片”