seqlist.h文件:
#pragma once
#include "stdlib.h "
#include "stdio.h"
#include "memory.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);
//在现行变的pos位置插入新的元素node
int SeqList_Insert(SeqList *list, SeqListNode *node,int pos);
//获取线性表pos位置的元素
SeqListNode* SeqList_Get(SeqList *list,int pos);
//删除pos位置的元素,返回值为被删除的元素,若删除失败则返回NULL
SeqListNode* SeqList_Delete(SeqList *list,int pos);
seqlist.c文件:
#include "seqlist.h"
/*
线性表是零个或多个数据元素的集合,线性表中的数据元素之间是有顺序的
线性表中的数据元素的个数是有限的,线性表中的数据元素的类型必须相同
*/
typedef struct _tag_SeqList
{
int length;//长度
int capacity;//最大容量
unsigned int *node;//指针指向内存空间,为了动态分配数组大小int *node[]
}TSeqList;
SeqList* SeqList_Create(int capacity)
{
TSeqList* tem = NULL;
int rec = 0;
tem = (TSeqList*)malloc(sizeof(TSeqList));//分配内存,当函数运行完毕后我们写的底层库的内存空间仍然有效,所以进行此操作
if (tem == NULL)
{
rec = -1;
printf("SeqList_Create tem err:%d",rec);
return NULL;
}
memset(tem, 0, sizeof(TSeqList));//将tem中的数据置为0
//根据capacity的大小分配节点的空间
tem->node = (unsigned int *)malloc(sizeof(unsigned int *) * capacity);
if (tem->node == NULL)
{
rec = -2;
printf("SeqList_Create tem->node err:%d", rec);
return NULL;
}
tem->capacity = capacity;//设置链表容量大小
tem->length = 0;
return tem;
}
void SeqList_Destroy(SeqList* list)
{
int n = 0;
if (list == NULL)
{
n = -1;
printf("SeqList_Destroy err:%d", n);
return;
}
TSeqList* tem = (TSeqList*)list;
if (tem->node != NULL)
{
free(tem->node);
}
free(tem);
return;
}
void SeqList_Clear(SeqList *list)
{
int n = 0;
if (list == NULL)
{
n = -1;
printf("SeqList_Clear err:%d", n);
return;
}
TSeqList* tem = (TSeqList*)list;
tem->length = 0;
return;
}
int SeqList_Length(SeqList *list)
{
int n = 0;
if (list == NULL)
{
n = -1;
printf("SeqList_Length err:%d", n);
return -1;
}
TSeqList* tem = (TSeqList*)list;
return tem->length;
}
int SeqList_Capacity(SeqList *list)
{
int n = 0;
if (list == NULL)
{
n = -1;
printf("SeqList_Capacity err:%d", n);
return -1;
}
TSeqList* tem = (TSeqList*)list;
return tem->capacity;
}
int SeqList_Insert(SeqList *list, SeqListNode *node, int pos)
{
int i = 0,n = 0;
if (list == NULL || node == NULL || pos < 0)
{
n = -1;
printf("SeqList_Insert err:%d", n);
return -1;
}
TSeqList* tem = (TSeqList*)list;
//判断是不是满了
if (tem->length >= tem->capacity)
{
n = -2;
printf("SeqList_Insert err:%d", n);
return -1;
}
//容错修正,假如现在长度为6,容量为10,用户在8的位置插入元素,则将其修改为在7号位置插入
if (pos > tem->length)
{
pos = tem->length;
}
for (i = tem->length;i>pos;i--)
{
tem->node[i] = tem->node[i-1];
}
tem->node[i] = (unsigned int)node;
tem->length++;
return 0;
}
SeqListNode* SeqList_Get(SeqList *list, int pos)
{
SeqListNode* liNode=0;
int n = 0;
if (list == NULL)
{
n = 1;
printf("SeqList_Get err:%d", n);
return NULL;
}
TSeqList* tem = (TSeqList*)list;
liNode = (void *)tem->node[pos];
return liNode;
}
SeqListNode* SeqList_Delete(SeqList *list, int pos)
{
int n = 0,i = 0;
if (list == NULL || pos < 0)
{
n = -1;
printf("SeqList_Delete err:%d", n);
return NULL;
}
TSeqList* tem = (TSeqList*)list;
for (i = pos;i < tem->length-1;i++)
{
tem->node[i] = tem->node[i+1];
}
tem->length--;
return NULL;
}
main.c文件
#include "seqlist.h"
typedef struct Teacher
{
int age;
char name[64];
}Teacher;
int main()
{
SeqList *list = NULL;
Teacher t1, t2, t3, t4;
t1.age = 31;
t2.age = 32;
t3.age = 33;
t4.age = 34;
list = SeqList_Create(10);
SeqList_Insert(list, (SeqList*)&t1, 0);
SeqList_Insert(list, (SeqList*)&t2, 0);
SeqList_Insert(list, (SeqList*)&t3, 0);
SeqList_Insert(list, (SeqList*)&t4, 0);
for (int i=0;i<SeqList_Length(list);i++)
{
Teacher* tem = SeqList_Get(list, i);
if (tem == NULL)
{
return;
}
printf("age:%d\n", tem->age);
}
while (SeqList_Length(list)>0)
{
SeqList_Delete(list, 0);
}
system("pause");
return 0;
}