直接上干货:
.c部分
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define InitSize 8 /* 初始分配的存储空间 */
#define IncrSize 4 /* 每次增加的存储空间 */
typedef int ElemType;
typedef struct SeqList
{
ElemType *data; /* 存储地址 */
int length; /* 当前长度 */
int capacity; /* 当前分配的存储容量 */
} SeqList;
void InitSeqList(SeqList *list)
{
assert(list != NULL);
list->data = (ElemType *)malloc(InitSize * sizeof(ElemType));
assert(list->data != NULL);
list->length = 0;
list->capacity = InitSize;
}
bool IncSeqList(SeqList *list)
{
assert(list != NULL);
ElemType *newData = (ElemType *)realloc(list->data, (list->capacity + IncrSize) * sizeof(ElemType));
if (newData == NULL)
return false;
list->data = newData;
list->capacity += IncrSize;
return true;
}
void push_front(SeqList *list, ElemType x)
{
assert(list != NULL);
if (list->length == list->capacity)
IncSeqList(list);
for (int i = list->len