1、动态顺序表:
顺序表长度可以扩容,比静态顺序表更灵活
2、顺序表初始化
#define eleType int
typedef struct {
eleType* elements;
int length;
int capacity;
}SeqList;
//顺序表初始化,传入要初始化的顺序表和容量
void InitList(SeqList* L, int c)
{
L->elements = (eleType*)malloc(c * sizeof(eleType));
for (int i = 0; i < c; i++)
L->elements[i] = 0;
L->length = 0;
L->capacity = c;
}
3、顺序表的销毁
void DestoryList(SeqList* L)
{
free(L->elements);
L->elements = NULL;
L->capacity = 0;
L->length = 0;
}
4、顺序表的扩容
void ExpendList(SeqList* L)
{
int newCapacity = L->capacity * 2;
int* newElements = (eleType*)malloc(sizeof(eleType) * newCapacity);
for (int i = 0; i < L->length; i++)
newElements[i] = L->elements[i];
free(L->elements);
for (int i = 0; i < L->length; i++)
L->elements[i] = newElements[i];
L->capacity = newCapacity;
}
5、顺序表的元素插入
bool ListInsert(SeqList* L, int i, eleType e)
{
if (i<1 || i>L->length + 1)
return false;
//顺序表已经用尽,对顺序表进行扩容
if (L->length == L->capacity)
ExpendList(L);
for (int j = L->length; j >= i; j--)
L->elements[j] = L->elements[j - 1];
L->elements[i - 1] = e;
L->length++;
return true;
}
6、顺序表的元素删除
bool ListDelete(SeqList* L, int i,eleType* e)
{
if (L->length == 0)
return false;
if (i<1||i > L->length)
return false;
*e = L->elements[i - 1];
for (int j = i; j < L->length; j++)
L->elements[j - 1] = L->elements[j];
L->length--;
return true;
}
7、顺序表的元素修改
bool ListAlter(SeqList* L, int i, int e)
{
if (L->length == 0)
return false;
if (i<1 || i > L->length)
return false;
L->elements[i - 1] = e;
return true;
}
8、元素的按位查找
eleType GetElement(SeqList L, int i)
{
return L.elements[i - 1];
}
9、元素按值查找
//判断顺序表中是否存在该元素
bool isElementExist(SeqList L, eleType e)
{
for (int i = 0; i < L.length; i++)
{
if (L.elements[i] == e)
return true;
}
return false;
}
//元素的按值查找
int LocalElement(SeqList L, eleType e)
{
for (int i = 0; i < L.length; i++)
if (L.elements[i] == e)
return i + 1;
}
10、顺序表判空打印
//判断元素表是否为空
bool isEmpty(SeqList L)
{
if (L.length == 0)
return true;
return false;
}
//打印顺序表
void PrintList(SeqList L)
{
if (isEmpty(L))
printf("当前顺序表为空表\n");
for (int i = 0; i < L.length; i++)
{
printf("%d ",L.elements[i]);
}
printf("\n");
}
11、调试
int main()
{
SeqList L;
bool flag = true;
while (flag)
{
printf("********************\n");
printf("*****1.表初始化*****\n");
printf("*****2.插入元素*****\n");
printf("*****3.删除元素*****\n");
printf("*****4.按位查找*****\n");
printf("*****5.按值查找*****\n");
printf("*****6.元素修改*****\n");
printf("*****0.销毁退出*****\n");
printf("********************\n");
printf("请输入选项:");
int opt=0;
scanf("%d",&opt);
switch (opt)
{
case 1:
printf("请输入要初始化的容量:");
int c;
scanf("%d", &c);
InitList(&L,c);
break;
case 2:
printf("请输入插入的位置与数值:");
int i1;
eleType e1;
scanf("%d %d", &i1, &e1);
if (ListInsert(&L, i1, e1))
printf("插入成功\n");
else
printf("插入失败\n");
PrintList(L);
break;
case 3:
printf("请输入要删除的位置:");
int i2;
eleType e2;
scanf("%d", &i2);
if (ListDelete(&L, i2, &e2))
printf("删除成功,删除的值为%d\n", e2);
else
printf("删除失败\n");
PrintList(L);
break;
case 4:
printf("请输入要查找的位置:");
int i3;
scanf("%d", &i3);
if (i3<1 || i3>L.length)
printf("不存在该位置请重新输入:\n");
else
{
eleType e3 = GetElement(L, i3);
printf("该位置的值为%d\n", e3);
}
break;
case 5:
printf("请输入要查找的值:");
eleType e4;
scanf("%d", &e4);
if (isElementExist(L, e4))
{
int i4 = GetElement(L, e4);
printf("该元素所在的位置为%d\n", i4);
}
else
printf("顺序表中不存在该元素\n");
break;
case 6:
printf("请输入要修改的位置及要修改的值:");
int i5;
eleType e5;
scanf("%d %d", &i5,&e5);
if (ListAlter(&L, i5, e5))
printf("修改成功\n");
else
printf("修改失败\n");
PrintList(L);
break;
case 0:
DestoryList(&L);
flag = false;
break;
default:
printf("输入的值不合法请重新输入:");
break;
}
}
return 0;
}