linux内核数据结构-顺序存储链表(1)

seqlist.h文件


#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__


typedef void SeqList;
typedef void SeqListNode;




SeqList* SeqList_Create(int capacity);


int  SeqList_Create01(SeqList **handle, 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__



seqlist.c文件


#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "seqlist.h"


typedef struct _tag_seqlist 
{
int length;
int capacity;
unsigned int *node;
}Tseqlist;
SeqList* SeqList_Create(int capacity)
{
Tseqlist *ret = NULL;
if (capacity < 0)
{
return NULL;
}
ret = (Tseqlist *)malloc(sizeof(Tseqlist));
if (ret == NULL)
{
return NULL;
}
memset(ret, 0, sizeof(Tseqlist));
ret->node = (unsigned int *)malloc(sizeof(unsigned int)*capacity);
if (ret->node == NULL)
{
return NULL;
}
memset(ret->node, 0, sizeof(unsigned int)*capacity);
ret->capacity = capacity;
ret->length = 0;
return ret;
}


int  SeqList_Create01(SeqList **handle, int capacity)
{
Tseqlist *ret = NULL;
if (capacity < 0)
{
return -1;
}
ret = (Tseqlist *)malloc(sizeof(Tseqlist)+sizeof(unsigned int)*capacity);
if (ret == NULL)
{
return -2;
}
memset(ret, 0, sizeof(Tseqlist)+sizeof(unsigned int)*capacity);
ret->node = (unsigned int *)(ret + 1);
if (ret->node == NULL)
{
return -3;
}
ret->capacity = capacity;
ret->length = 0;
*handle = ret;
return 0;
}


void SeqList_Destroy(SeqList* list)
{
if (list != NULL)
{
return;
}
free(list);
return;
}



void SeqList_Clear(SeqList* list)
{
Tseqlist *tem = (Tseqlist *)list;
if (list != NULL)
{
return;
}
tem->length = 0;
return;
}


int SeqList_Length(SeqList* list)
{
Tseqlist *tem = (Tseqlist *)list;
if (list == NULL)
{
return -1;
}
return tem->length;
}


int SeqList_Capacity(SeqList* list)
{
Tseqlist *tem = (Tseqlist *)list;
if (list != NULL)
{
return -1;
}
return tem->capacity;
}


int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
int i = 0;
Tseqlist *ret = (Tseqlist *)list;
if (list == NULL || node == NULL)
{
return -1;
}
//位置错误
if (pos<0 || pos>=ret->capacity)
{
return -2;
}
//满了
if (ret->length>=ret->capacity)
{
return -3;
}
//优化
if (pos>ret->length)
{
pos = ret->length;
}
for (i=ret->length; i>pos; i-- )
{
ret->node[i] = ret->node[i-1];
}
ret->node[pos] = (unsigned int)node;
ret->length++;
return 0;
}


SeqListNode* SeqList_Get(SeqList* list, int pos)
{
int i = 0;
SeqListNode *rv = NULL;
Tseqlist *ret = (Tseqlist *)list;
if (list == NULL)
{
return NULL;
}
if (pos<0 || pos>=ret->capacity)
{
return NULL;
}


rv = (SeqListNode *)ret->node[pos];
return rv;
}


SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
int i = 0;
SeqListNode *rv = NULL;
Tseqlist *ret = (Tseqlist *)list;
if (list == NULL)
{
return NULL;
}
if (pos<0 || pos>=ret->capacity)
{
return NULL;
}
rv = (SeqListNode *)ret->node[pos];
for (i=pos+1; i<ret->length; i++)
{
ret->node[i+1] = ret->node[i];
}

ret->length--;
return rv;
}


seqlinklist测试03


#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "seqlist.h"


typedef struct _Teacher
{
int name[128];
int age;
}Teacher;


void main()
{
SeqList * list = NULL;
int i = 0;
Teacher t1,t2,t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;



list = SeqList_Create(3);


SeqList_Insert(list, (SeqListNode *)&t1, 0);
SeqList_Insert(list, (SeqListNode *)&t2, 0);
SeqList_Insert(list, (SeqListNode *)&t3, 0);


for(i=0; i<SeqList_Length(list); i++)
{
Teacher * temp = NULL;
temp = (Teacher *)SeqList_Get(list, i);
if (temp != NULL)
{
printf("temp:%d", temp->age);
}
}
while(SeqList_Length(list) > 0)
{
SeqList_Delete(list, 0);
}


SeqList_Destroy(list);


.


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值