#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct
{
int data[N];
int last; // last代表的是数组中最后一个有效元素的下标
} seqlist_t;
// 4.判断顺序表是否为满,满返回1 未满返回0
int IsFullSeqlist(seqlist_t *p)
{
return p->last + 1 == N;
}
// 5.判断顺序表是否为空
int IsEpSeqlist(seqlist_t *p)
{
return p->last == -1;
}
// 1.创建一个空的顺序表
seqlist_t *CreateEpSeqlist() // 返回的是申请空间的首地址
{
seqlist_t *p = (seqlist_t *)malloc(sizeof(seqlist_t));
if (p == NULL)
{
perror("malloc err");
return NULL;
}
// 初始化
p->last = -1;
return p;
}
// 2.向顺序表的指定位置插入数据
int InsertIntoSeqlist(seqlist_t *p, int post, int data) // post第几个位置,data插入的数据
{
// 容错判断-->判满判空
if (IsFullSeqlist(p) || post < 0 || post > p->last + 1)
{
printf("InsertIntoSeqlist err\n");
return -1; // 非零为错误退出
}
for (int i = p->last; i >= post; i--)
p->data[i + 1] = p->data[i];
p->data[post] = data;
p->last++;
return 0;
}
// 3.遍历顺序表sequence 顺序 list 表
void ShowSeqlist(seqlist_t *p)
{
for (int i = 0; i <= p->last; i++)
printf("%d ", p->data[i]);
putchar('\n');
}
// 6.删除顺序表中指定位置的数据post删除位置
int DeletePostSeqlist(seqlist_t *p, int post)
{
for (int i = post; i < p->last; i++)
p->data[i]=p->data[i+1];
p->last--;
}
// 7.清空顺序表
void ClearSeqList(seqlist_t *p)
{
p->last = -1;
}
// 8.修改指定位置的数据
int ChangePostSeqList(seqlist_t *p, int post, int data) // post被修改的位置,data修改成的数据
{
p->data[post]=data;
}
// 9.查找指定数据出现的位置
int SearchDataSeqList(seqlist_t *p, int data) // data代表被查找的数据
{
for(int i=0;i<p->last;i++)
{
if(p->data[i]==data)
printf("%d\n",i);
}
}
int main(int argc, char const *argv[])
{
seqlist_t *p = CreateEpSeqlist();
for(int i=0;i<N;i++)
InsertIntoSeqlist(p ,i,i);
ShowSeqlist(p);
return 0;
}