两种创建方式以及初始化函数都在下方代码中:
#define _CRT_SECURE_NO_WARNINGS #pragma once//防止头文件重复包含 #include<stdio.h>
#include<string.h> #include<stdlib.h> #define MAXSIZE 50 #define OK 1; #define
ERROR 0; typedef int ElemType; typedef struct { ElemType data[MAXSIZE]; int
Length; }SqList_; typedef struct { ElemType* elems; int Length; int Listsize;
}SqList; int InitList_(SqList_* L){ memset(L->data, 0, sizeof(L->data)); return
OK; } int InitList(SqList* L){ L->elems=(ElemType*)malloc(MAXSIZE *
sizeof(ElemType)); if (L->elems == NULL) { printf("内存开辟失败\n"); return ERROR; }
L->Length = 0; L->Listsize = MAXSIZE; return OK; } int Length(SqList L) {
return L.Length; }; int LocateElem(SqList L, ElemType e) { for (int i = 0; i <
L.Length; i++) { if (L.elems[i] == e) { return i; } } return -1; }; ElemType
GetElem(SqList L, int i) { return L.elems[i-1]; }; int ListInsert(SqList* L,
int i, ElemType e) { if (i<1 || i>L->Length+1) return ERROR; if (L->Length ==
MAXSIZE) return ERROR; for (int j = L->Length; j > i; j--) { L->elems[j] =
L->elems[j - 1]; } L->elems[i-1] = e; L->Length++; return OK; }; int
Append(SqList* L, ElemType e) { if (L->Length == MAXSIZE) return ERROR;
L->elems[L->Length++] = e; } int ListDelete(SqList* L, int i, ElemType* e) { if
(i<1 || i>L->Length) return ERROR; *e = L->elems[i - 1]; for (int j = i; j <
L->Length; j++) { L->elems[j-1] = L->elems[j]; } L->Length--; return OK; };
void PrintList(SqList L) { for (int i = 0; i < L.Length; i++) { printf("%d\n",
L.elems[i]); } }; int Empty(SqList L) { if (L.Length == 0) return 1; else
return 0; }; int DestroyList(SqList* L) { L->Length = 0; L->Listsize = 0;
free(L->elems); L = NULL; }; int main() { SqList List; SqList* L = &List;
InitList(L); for (int i = 0; i < 10; i++) { Append(L, i + 1); }
PrintList(List); return 0; }