通用静态链表

1、头文件定义

#ifndef _STATICLIST_H
#define _STATICLIST_H

#include <stdio.h>
#include <stdlib.h>

typedef void ListNode;
typedef void StaticList;

StaticList* StaticList_Create(int capacity);

void StaticList_Clear(StaticList* list);

int StaticList_Length(StaticList* list);

int StaticList_Capacity(StaticList* list);

int StaticList_Insert(StaticList* list, ListNode* node, int pos);

ListNode* StaticList_GetNode(StaticList* list, int pos);

ListNode* StaticList_Delete(StaticList* list, int pos);

int StaticList_Destroy(StaticList* list);

#endif

2、头文件实现

#include "StaticList.h"

#define INVALID_NEXT -1

typedef struct{
	int next;
	unsigned int value;
}TListNode;

typedef struct{
	int capacity;
	TListNode header;
	TListNode node[];
}TStaticList;

StaticList* StaticList_Create(int capacity)
{
	TStaticList* list = NULL;
	int i;
	
	if(capacity <= 0)
	{
		return NULL;
	}
	
	list = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TListNode) * (capacity + 1));
	
	if(list == NULL)
	{
		return NULL;
	}
	
	list->capacity = capacity;
	list->header.next = 0;
	list->header.value = 0;
	
	for(i = 0; i < capacity; i++)
	{
		list->node[i].next = INVALID_NEXT;
	}
	
	return list;
}

void StaticList_Clear(StaticList* list)
{
	TStaticList* Tlist = (TStaticList*)list;
	int i;
	
	if(Tlist == NULL)
	{
		return;
	}
	
	Tlist->header.next = 0;
	Tlist->header.value = 0;
	
	for(i = 0; i < Tlist->capacity; i++)
	{
		Tlist->node[i].next = INVALID_NEXT;
	}
}

int StaticList_Length(StaticList* list)
{
	TStaticList* Tlist = (TStaticList*)list;
	
	if(Tlist == NULL)
	{
		return;
	}
	
	return Tlist->header.value;
}

int StaticList_Capacity(StaticList* list)
{
	TStaticList* Tlist = (TStaticList*)list;
	
	if(Tlist == NULL)
	{
		return;
	}
	
	return Tlist->capacity;
}

int StaticList_Insert(StaticList* list, ListNode* node, int pos)
{
	TStaticList* Tlist = (TStaticList*)list;
	int index;
	int Current = 0;
	int i;
	
	if(Tlist == NULL || node == NULL)
	{
		return -1;
	}
	
	if( pos < 0 || (pos + 1 > Tlist->capacity) )
	{
		return -2;
	}
	
	for(i = 1; i < Tlist->capacity; i++)
	{
		if(Tlist->node[i].next == INVALID_NEXT)
		{
			index = i;
			break;
		}
	}
	Tlist->node[index].value = (unsigned int)node;
	
	Tlist->node[0] = Tlist->header;	
	
	for(i = 0; (i < pos && Tlist->node[Current].next != 0); i++)
	{
		Current = Tlist->node[Current].next;
	}
	Tlist->node[index].next = Tlist->node[Current].next;
	Tlist->node[Current].next = index;
	
	(Tlist->node[0].value)++;
	
	Tlist->header = Tlist->node[0];	
		
	return 0;
}

ListNode* StaticList_GetNode(StaticList* list, int pos)
{
	TStaticList* Tlist = (TStaticList*)list;
	int Current = 0;
	int index;
	int i;
	
	if(Tlist == NULL)
	{
		return NULL;
	}
	
	if( pos < 0 || pos > StaticList_Length(list) - 1 )
	{
		return NULL;
	}
	
	Tlist->node[0] = Tlist->header;
	
	for(i = 0; i < pos; i++)
	{
		Current = Tlist->node[Current].next;
	}
	
	index = Tlist->node[Current].next;
	
	return (ListNode*)(Tlist->node[index].value);
}

ListNode* StaticList_Delete(StaticList* list, int pos)
{
	TStaticList* Tlist = (TStaticList*)list;
	int index;
	int Current = 0;
	int i;
	
	if(Tlist == NULL)
	{
		return NULL;
	}
	
	if( pos < 0 || pos > StaticList_Length(list) - 1 )
	{
		return NULL;
	}
	
	Tlist->node[0] = Tlist->header;
	
	for(i = 0; i < pos; i++)
	{
		Current = Tlist->node[Current].next;
	}
	
	index = Tlist->node[Current].next;
	Tlist->node[Current].next = Tlist->node[index].next;
		
	(Tlist->node[0].value)--;
	
	Tlist->header = Tlist->node[0];
	
	return (ListNode*)(Tlist->node[index].value);
}

int StaticList_Destroy(StaticList* list)
{
	if(list != NULL)
	{
		free(list);
	}
}

3、测试main

int main(int argc, char *argv[]) {
	
	StaticList* list = StaticList_Create(10);
	int i;
	int* node = NULL;
	
	int a1 = 1;
	int a2 = 2;
	int a3 = 3;
	int a4 = 4;
	int a5 = 5;
	
	StaticList_Insert(list, (ListNode*)&a1, StaticList_Length(list));
	StaticList_Insert(list, (ListNode*)&a2, StaticList_Length(list));
	StaticList_Insert(list, (ListNode*)&a3, StaticList_Length(list));
	StaticList_Insert(list, (ListNode*)&a4, StaticList_Length(list));
	StaticList_Insert(list, (ListNode*)&a5, StaticList_Length(list));
	
	for(i = 0; i < StaticList_Length(list); i++)
	{
		node = (int*)StaticList_GetNode(list, i);
		printf("%d: %d\n", i, *node);
	}
	
	while( StaticList_Length(list) != 0)
	{
		node = (int*)StaticList_Delete(list, StaticList_Length(list) - 1);
		printf("%d\n", *node);
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值