1. SeqList.h
#ifndef _LINKLIST_H
#define _LINKLIST_H
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(intcapacity);
int SeqList_Insert(SeqList*list,SeqListNode*node, int position);
SeqListNode* SeqList_Get(SeqList*list,intposition);
SeqListNode* SeqList_Delete(SeqList*list,intposition);
int SeqList_GetLength(SeqList*list);
int SeqList_GetCapacity(SeqList*list);
void SeqList_Destroy(SeqList*list);
void SeqList_Clear(SeqList*list);
#endif
2. SeqList.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct _Tag_SeqList{
int capacity;
int length;
unsigned int ** node;
} TSeqList;
SeqList* SeqList_Create(intcapacity) {
int ret = 0;
TSeqList* list = (TSeqList*)malloc(sizeof(TSeqList));
if (list == NULL) {
ret = -1;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
memset(list, 0, sizeof(list));
list->node = (unsigned int **)malloc(sizeof(unsignedint *) *capacity);
if (list->node == NULL) {
ret = -2;
printf("function SeqList_Create error: malloc error, ret = %d", ret);
}
list->capacity = capacity;
list->length = 0;
return list;
}
int SeqList_Insert(SeqList*list,SeqListNode*node, int position) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Insert error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->length >= tlist->capacity) {
ret = -3;
printf("function SeqList_Insert error: ret = %d", ret);
}
if (position >= tlist->length) {
position = tlist->length;
}
int i = 0;
for (i = tlist->length; i > position; i--) {
tlist->node[i] = tlist->node[i-1];
}
tlist->node[i] = (unsigned int *)node;
tlist->length++;
return ret;
}
SeqListNode* SeqList_Get(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Get error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Get error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Get error: ret = %d", ret);
}
return tlist->node[position];
}
SeqListNode* SeqList_Delete(SeqList*list,intposition) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Delete error: ret = %d", ret);
}
if (position < 0) {
ret = -2;
printf("function SeqList_Delete error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (position >= tlist->length) {
ret = -3;
printf("function SeqList_Delete error: ret = %d", ret);
}
SeqListNode* node = tlist->node[position];
for (int i =position; i < tlist->length-1; i++) {
tlist->node[i] = tlist->node[i+1];
}
tlist->length--;
return node;
}
int SeqList_GetLength(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetLength error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->length;
}
int SeqList_GetCapacity(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_GetCapacity error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
return tlist->capacity;
}
void SeqList_Destroy(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Destroy error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
if (tlist->node != NULL) {
free(tlist->node);
tlist->node = NULL;
}
if (tlist != NULL) {
free(tlist);
tlist = NULL;
}
return;
}
void SeqList_Clear(SeqList*list) {
int ret = 0;
if (list ==NULL) {
ret = -1;
printf("function SeqList_Clear error: ret = %d", ret);
}
TSeqList* tlist = (TSeqList*)list;
tlist->length = 0;
return;
}
3. SeqListMain.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SeqList.h"
typedef struct Teacher {
int age;
char name[64];
} Teacher;
void main() {
SeqList* list = SeqList_Create(10);
if (list == NULL) {
return;
}
Teacher t1, t2, t3;
strcpy(t1.name, "A");
strcpy(t2.name, "B");
strcpy(t3.name, "C");
SeqList_Insert(list, (SeqListNode*)&t1, 0);
SeqList_Insert(list, (SeqListNode*)&t2, 0);
SeqList_Insert(list, (SeqListNode*)&t3, 0);
int length = SeqList_GetLength(list);
for (int i = 0; i < length; i++) {
Teacher* t = (Teacher*)SeqList_Get(list, i);
printf("%s\n", t->name);
}
printf("delete\n");
while (SeqList_GetLength(list) > 0) {
Teacher* t = (Teacher*)SeqList_Delete(list, 0);
printf("%s\n", t->name);
}
printf("length:%d\n", SeqList_GetLength(list));
printf("capacity:%d\n", SeqList_GetCapacity(list));
SeqList_Clear(list);
SeqList_Destroy(list);
system("pause");
}