#ifndef __MY_SEQLIST_H__
#define __MY_SEQLIST_H__ typedef void SeqList;
typedef void SeqListNode; //链表 创建
SeqList* SeqList_Create(int capacity); //链表 销毁
void SeqList_Destroy(SeqList* list); ////链表 清空
void SeqList_Clear(SeqList* list); //链表 长度
int SeqList_Length(SeqList* list); //链表 容量
int SeqList_Capacity(SeqList* list); //链表 在某一个位置 插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); //获取某一个位置的链表结点
SeqListNode* SeqList_Get(SeqList* list, int pos); //删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif //__MY_SEQLIST_H__
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.h" //用数组来模拟线性表
typedef struct _tag_SeqList
{
int capacity;
int length;
//int *node[100];
int **node; //指针数组
//int node[capacity] //
//int *node[capacity]; //int *node; // int node[i]===> *(node+i)
}TSeqList; //链表 创建
SeqList* SeqList_Create(int capacity) //O(1)
{
int ret;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =;
printf("func SeqList_Create() err :%d \n", ret);
return NULL;
}
memset(tmp, , sizeof(TSeqList));//置为0
tmp->capacity = capacity;
tmp->length = ;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = ;
printf("func SeqList_Create() malloc err :%d \n", ret);
return NULL;
}
memset(tmp->node, , sizeof(void *) * capacity); return tmp;
} //链表 创建
int SeqList_Create2(int capacity, SeqList**handle)
{
int ret = ;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =;
printf("func SeqList_Create2() err :%d \n", ret);
return ret;
}
memset(tmp, , sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = ;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = ;
printf("func SeqList_Create2() malloc err :%d \n", ret);
return ret;
} *handle = tmp;
return ret;
} //链表 销毁
void SeqList_Destroy(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
} tmp = (TSeqList *)list; if (tmp->node != NULL)
{
free(tmp->node);
}
free(tmp);
return ;
} ////链表 清空
void SeqList_Clear(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
} tmp = (TSeqList *)list;
tmp->length = ;
memset(tmp->node, , (tmp->capacity * sizeof(void *)) ); return ;
} //链表 长度
int SeqList_Length(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -;
}
tmp = (TSeqList *)list; return tmp->length;
} //链表 容量
int SeqList_Capacity(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -;
}
tmp = (TSeqList *)list;
return tmp->capacity;
} //链表 在某一个位置 插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //O(n)
{
TSeqList *tList = NULL;
int i = ;
if (list == NULL || node==NULL)
{
return -;
}
tList = (TSeqList *)list;
//如果满了
if (tList->length >= tList->capacity)
{
return -;
} //pos位置的容错处理
if (pos > tList->length )
{
pos = tList->length;
} for (i=tList->length; i>pos; i--) //n
{
tList->node[i] = tList->node[i-];
} tList->node[i] = (int* )node; //ok
tList->length ++; return ;
} //获取某一个位置的链表结点
SeqListNode* SeqList_Get(SeqList* list, int pos) //O(1)
{
TSeqList *tList = NULL;
SeqListNode *tmp = NULL; tList = (TSeqList *)list; if (list == NULL || pos< || pos >=tList->length )
{
return NULL;
}
tmp = tList->node[pos]; return tmp;
} //删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos) ////O(n)
{
int i = ;
TSeqList *tList = NULL;
SeqListNode *tmp = NULL; tList = (TSeqList *)list;
if (list == NULL || pos < || pos >= tList->length)
{
return NULL;
}
tmp = tList->node[pos]; // pos = 3
for (i=pos+; i<tList->length; i++)
{
tList->node[i-] = tList->node[i]; }
tList->length --;
return tmp;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h> #include "seqlist.h" typedef struct _Teacher
{
char name[];
int age ;
}Teacher; void main()
{
int ret = , i = ;
SeqList * list; Teacher t1, t2, t3;
t1.age = ;
t2.age = ;
t3.age = ; list = SeqList_Create(); //思考1: 如何实现 链表的api(链表的算法) 和 具体的数据分离
//思考2: 链表库(业务逻辑) 测试程序的业务逻辑 结点的生命周期 归谁管? ret = SeqList_Insert(list, (SeqListNode *)&t1, ); //头插法
ret = SeqList_Insert(list, (SeqListNode *)&t2, ); //头插法
ret = SeqList_Insert(list, (SeqListNode *)&t3, ); //头插法 //遍历链表
for (i=; i<SeqList_Length(list); i++ )
{
Teacher *tmp = (Teacher *)SeqList_Get(list, i); //获取链表结点
if (tmp == NULL)
{
printf("func SeqList_Get() err:%d \n ", ret);
return ;
}
printf("age:%d \n", tmp->age);
} //销毁链表
while (SeqList_Length(list) > )
{
Teacher *tmp = (Teacher *)SeqList_Delete(list, );//
if (tmp == NULL)
{
printf("func SeqList_Get() err:%d \n ", ret);
return ;
}
printf("age:%d \n", tmp->age);
} SeqList_Destroy(list); system("pause"); return ;
}