使用一层指针
# include "stdio.h"
# include <stdlib.h>
# define LIST_INIT_SIZE 10
# define LISTINCREMENT 10
# define ELEM int
typedef struct {
ELEM* head;
int length;
int listsize;
}SqList;
SqList* InitList_Sq(SqList* L) {
L = (SqList*)malloc(sizeof(SqList));
L->head = (ELEM*)malloc(LIST_INIT_SIZE * sizeof(ELEM));
if (!L->head) {
printf("failed------");
exit(0);
};
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return L;
}
int ListLength(SqList* L) {
return L->length;
}
void ListTraverse(SqList* L) {
int len = ListLength(L);
for (int k = 0;k < len; k++) {
printf("%d ", L->head[k]);
}
}
ELEM getElem(SqList* L, int i) {
return L->head[i];
}
void ListInsert(SqList* L, int index, ELEM e) {
if (index > L->length || index < 0) {
printf("there is an error -----");
return;
}
else {
if (L->length >= L->listsize) {
L->head = (ELEM*)realloc(L->head, (L->length + LISTINCREMENT) * sizeof(ELEM));
if (!L->head) {
printf("realloc failed\n");
return;
}
L->listsize += LISTINCREMENT;
}
for (int i = L->length; i > index; i--) {
L->head[i] = L->head[i - 1];
}
L->head[index] = e;
L->length++;
return;
}
}
SqList* ClearList(SqList* L) {
L->length = 0;
L->head = (ELEM*)realloc(L->head, LIST_INIT_SIZE * sizeof(ELEM));
L->listsize = LIST_INIT_SIZE;
return L;
}
void DestoryList(SqList* l)