SeqList.h 的内容
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
typedef void SeqList;
typedef void SeqListNode;
/*顺序表创建*/
SeqList* SeqList_Creat(int capacity);
/*顺序表删除*/
void SeqList_Destroy(SeqList* list);
/*顺序表清空*/
void SeqList_Clear(SeqList* list);
/*顺序表求长度*/
int SeqList_Length(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
SeqList.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "SeqList.h"
typedef struct TSeqList
{
int capacity,length;
SeqListNode **ArrayNode;
}TSeqList;
/*顺序表创建*/
SeqList* SeqList_Creat(int capacity)
{
if (capacity < 0)
{
printf("func SeqList_Creat() err ");
return NULL;
}
TSeqList *tmp = NULL;
tmp = (TSeqList*)malloc(sizeof(TSeqList));
memset(tmp, 0, sizeof(TSeqList));
if (tmp == NULL)
{
printf("func SeqList_Creat() err ");
return NULL;
}
tmp->length = 0;
tmp->capacity = capacity;
tmp->ArrayNode = (SeqListNode**)malloc(capacity*sizeof(SeqListNode*));
memset(tmp->ArrayNode, 0, 10 * sizeof(SeqListNode*));
return tmp;
}
/*顺序表删除*/
void SeqList_Destroy(SeqList* list)
{
int ret;
if (list == NULL)
{
ret = -1;
printf("func SeqList_Destroy() err %d !\n",ret);
return;
}
TSeqList* tlist = list;
if (tlist->ArrayNode != NULL)
{
free(tlist->ArrayNode);
}
if (tlist != NULL)
{
free(tlist);
}
}
/*顺序表清空*/
void SeqList_Clear(SeqList* list)
{
int ret;
if (list == NULL)
{
ret = -1;
printf("func SeqList_Clear() err %d !\n", ret);
return;
}
TSeqList *tlist = list;
tlist->length = 0;
memset(tlist->ArrayNode, 0, tlist->capacity*sizeof(SeqListNode*));
}
/*顺序表求长度*/
int SeqList_Length(SeqList* list)
{
int ret = 0;
if (list == NULL)
{
ret = -1;
printf("func SeqList_Length() err %d !\n", ret);
return;
}
TSeqList *tlist = list;
return tlist->length;
}
/*顺序表插入*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
int ret = 0;
if (list == NULL || node == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Length()at '(list == NULL || node == NULL || pos < 0)' err %d !\n", ret);
return ret;
}
TSeqList *tlist = list;
int *tmp = tlist->ArrayNode;
/*如果pos在正常范围(0-length)则执行后移插入*/
if (0 < pos<= tlist->length)
{
for (int i = tlist->length; i > pos; i--)
{
tmp[i] = tmp[i - 1];
}
tmp[pos] = node;
tlist->length++;
}
/*如果pos大于length,如何数组满了这替换最后,没满则是最加到最后*/
if (tlist->length < pos )
{
if (tlist->length == tlist->capacity)
{
tmp[tlist->length-1] = node;
}
if (tlist->length < tlist->capacity)
{
tmp[tlist->length] = node;
tlist->length++;
printf("");
}
}
}
/*顺序表获取位置上的元素*/
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
int ret = 0;
if (list == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Get() err %d", ret);
return NULL;
}
TSeqList* tlist = (TSeqList*)list;
int *tmp = tlist->ArrayNode;
/*如果pos在正常范围(0-length)这返回pos 的node*/
if (0 < pos && pos<= tlist->length)
{
return tmp[pos];
}
/*如果pos大于length*/
if (tlist->length < pos)
{
/*
if (tlist->length == tlist->capacity)
{
return tmp[tlist->length - 1];
}
if (tlist->length < tlist->capacity)
{
return tmp[tlist->length];
}
*/
return tmp[tlist->length - 1];
}
}
/*删除顺序表的某位置的元素*/
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
int ret = 0;
if (list == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Get() err %d", ret);
return ;
}
}
main.c
#include <stdio.h>
#include "SeqList.h"
int main()
{
SeqList *list = SeqList_Creat(10);
SeqList_Destroy(list);
list = SeqList_Creat(10);
SeqList_Clear(list);
char *a = "hello";
SeqList_Insert(list, a, 4);
int list_len = SeqList_Length(list);
char *a1 = SeqList_Get(list, 4);
return 0;
}