1 代码实现
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct
{
int data[MAXSIZE];
int length;
}seqList;
seqList* init_seqList()
{
seqList* L = (seqList*)malloc(sizeof(seqList));
if (L == NULL)
{
printf("内存申请失败\n");
exit(1);
}
L->length = 0;
return L;
}
int insert_seqList(seqList* L, int i, int e)
{
if (L == NULL)
{
printf("顺序表不存在\n");
return 0;
}
if (i<1 || i>L->length + 1)
{
printf("插入位置不合法\n");
return 0;
}
if (L->length >= MAXSIZE)
{
printf("顺序表已满,无法插入\n");
return 0;
}
for (int j = L->length; j >= i; j--)
{
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = e;
L->length++;
return 1;
}
int delete_seqList(seqList* L, int i) {
if (L == NULL)
{
printf("顺序表不存在\n");
return 0;
}
if (i<1 || i>L->length)
{
printf("删除位置不合法\n");
return 0;
}
for (int j = i; j < L->length; j++)
{
L->data[j - 1] = L->data[j];
}
L->length--;
return 1;
}
int locate_seqList(seqList* l, int e)
{
if (l == NULL)
{
printf("顺序表不存在\n");
return 0;
}
for (int i = 0; i < l->length; i++)
{
if (l->data[i] == e)
{
return i + 1;
}
}
return 0;
}
int getElem(seqList* l, int i)
{
if (i<1 || i>l->length)
{
printf("参数越界\n");
return 0;
}
else
{
printf("%d\n", l->data[i - 1]);
return (l->data[i - 1]);
}
}
int main() {
seqList* l = init_seqList();
insert_seqList(l, 1, 10);
insert_seqList(l, 2, 11);
insert_seqList(l, 3, 12);
getElem(l, 3);
delete_seqList(l, 2);
printf("%d\n", locate_seqList(l, 12));
free(l);
return 0;
}
2 输出
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/77dffaaf092645c7a74d59aa3edb012e.png)