顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
对于表,栈,队列,树,图等等数据结构,我们都可通过选择顺序结构或者链式结构来进行实现,下面就演示一下表的顺序实现。
因为这个太过简单所以就直接上代码
数据结构
typedef struct SeqList
{
DATATYPE* data; // 指向动态开辟的数组
size_t size; // 有效数据个数
size_t capicity; // 容量空间的大小
}SeqList;
实现的接口
void SeqListInit(SeqList* list, size_t capacity);
//顺序表初始化
int CheckCapacity(SeqList* list);
//检查顺序表是否已满,如果满了进行扩容
void SeqListDestory(SeqList* list);
//销毁顺序表
void SeqListPrint(SeqList* list);
//打印顺序表
void SeqListPushBack(SeqList* list, DATATYPE x);
//尾插
void SeqListPushFront(SeqList* list, DATATYPE x);
//头插
int SeqListFind(SeqList* list, DATATYPE x);
//查找顺序表
void SeqListPopBack(SeqList* list);
//尾删
void SeqListPopFront(SeqList* list);
//尾插
void SeqListInsert(SeqList* list, size_t pos, DATATYPE x);
//指定位置插入
void SeqListErase(SeqList* list, size_t pos);
//指定位置删除
void SeqListInit(SeqList* list, size_t capacity)
{
assert(list);
DATATYPE* temp;
temp = (DATATYPE*)malloc(capacity * sizeof(DATATYPE));
if (NULL == list)
{
printf("初始化失败\n");
return;
}
list->data = temp;
list->size = 0;
list->capicity = capacity;
}
void SeqListDestory(SeqList* list)
{
assert(list);
list->size = 0;
list->capicity = 0;
free(list->data);
list->data = NULL;
}
int CheckCapacity(SeqList* list)
{
assert(list);
DATATYPE* temp;
if (list->capicity == list->size)
{
temp = realloc(list, 2 * LISTSIZE * sizeof(DATATYPE));
if (NULL == temp)
{
printf("增容失败\n");
return 0;
}
else
{
list->data = temp;
printf("增容成功\n");
}
}
return 1;
}
void SeqListPrint(SeqList* list)
{
assert(list);
size_t i = 0;
for (i = 0; i < list->size; i++)
{
printf("%d ", list->data[i]);
}
printf("\n");
}
void SeqListPushFront(SeqList* list, DATATYPE x)
{
assert(list);
int i;
if (!CheckCapacity(list))
{
printf("顺序表已满\n");
return;
}
for (i = list->size; i > 0; i--)
{
list->data[i] = list->data[i - 1];
}
list->data[0] = x;
list->size++;
}
void SeqListPushBack(SeqList* list, DATATYPE x)
{
assert(list);
if (!CheckCapacity(list))
{
printf("顺序表已满\n");
return;
}
list->data[list->size++] = x;
}
void SeqListPopFront(SeqList* list)
{
assert(list);
size_t i;
if (list->size == 0)
{
printf("表已空\n");
return;
}
for (i = 0; i < list->size - 1; i++)
{
list->data[i] = list->data[i + 1];
}
list->size--;
}
void SeqListPopBack(SeqList* list)
{
assert(list);
if (list->size == 0)
{
printf("表已空\n");
return;
}
list->size--;
}
int SeqListFind(SeqList* list, DATATYPE x)
{
assert(list);
size_t i;
for (i = 0; i < list->size; i++)
{
if (x == list->data[i])
{
printf("查找成功\n");
return i;
}
}
printf("查找失败\n");
return -1;
}
void SeqListInsert(SeqList* list, size_t pos, DATATYPE x)
{
assert(list);
int i = 0;
if (!CheckCapacity(list))
{
printf("顺序表已满\n");
return;
}
for (i = (int)list->size; i > pos -1; i--)
{
list->data[i] = list->data[i - 1];
}
list->data[pos - 1] = x;
list->size++;
}
void SeqListErase(SeqList* list, size_t pos)
{
assert(list);
size_t i;
if (list->size == 0)
{
printf("表已空\n");
return;
}
for (i = pos - 1; i < list->size - 1; i++)
{
list->data[i] = list->data[i + 1];
}
list->size--;
}