关闭

专题三-可复用静态链表

448人阅读 评论(0) 收藏 举报
分类:
//main.c文件
#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) 
{
	StaticList* list = StaticList_Create(10);
	
	int index = 0;
	
	int i = 0;
	int j = 1;
	int k = 2;
	int x = 3;
	int y = 4;
	int z = 5;
	
	//插入结点 
	StaticList_Insert(list, &i, 0);
	StaticList_Insert(list, &j, 0);
	StaticList_Insert(list, &k, 0);
	StaticList_Insert(list, &x, 0);
	StaticList_Insert(list, &y, 0);
	StaticList_Insert(list, &z, 0);
	
	//遍历静态链表 
	for (index=0; index<StaticList_Length(list); index++) {
		int* p = (int*)StaticList_Get(list, index);
		
		printf("%d\n", *p);
	}
	
	printf("\n");
	
	//删除结点
	while (StaticList_Length(list) > 0) {
		int* p = (int*)StaticList_Delete(list, 0);
		
		printf("%d\n", *p);
	} 
	
	printf("\n");
	
	//插入结点 
	StaticList_Insert(list, &x, 0);
	StaticList_Insert(list, &y, 0);
	StaticList_Insert(list, &z, 0);
	
	//输出大小 长度 
	printf("Capacity: %d Length: %d\n", StaticList_Capacity(list), StaticList_Length(list));		
	
	//遍历 
	for (index=0; index<StaticList_Length(list); index++) {
		int* p = (int*)StaticList_Get(list, index);
		
		printf("%d\n", *p);
	}	
	
	//销毁静态链表 
	StaticList_Destroy(list);
	
	return 0;
}


//StaticList.c文件
#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"

//可用标识符 
#define AVAILABLE -1

/*静态链表结点定义*/
typedef struct _tag_StaticListNode {
	unsigned int data;	//数据域 
	int next;		//下一个结点下标 
} TStaticListNode;

/*静态链表结构体定义*/
typedef struct _tag_StaticList {
	int capacity;		//容量 
	TStaticListNode header;	//头结点 
	TStaticListNode node[]; //结点 	//柔性数组 
} TStaticList;

/*初始化静态链表*/
StaticList* StaticList_Create(int capacity)
{
	TStaticList* ret = NULL;
	int i = 0;
	
	if (capacity >= 0) {
		ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity+1));	
	}
	
	if (ret != NULL) {
		ret->capacity = capacity;
		ret->header.data = 0;	//复用  作为链表长度 
		ret->header.next = 0;	//第一个结点的下标 
		
		//标识为可用状态 
		for (i=1; i<=capacity; i++) {
			ret->node[i].next = AVAILABLE;	
		}
	}
		
    	return ret;
}

/*释放静态链表*/
void StaticList_Destroy(StaticList* list)
{
	free(list);
}

/*清空静态链表*/
void StaticList_Clear(StaticList* list)
{
	TStaticList* sList = (TStaticList*)list;
	int i = 0;
	
	if (sList != NULL) {
		sList->header.data = 0;
		sList->header.next = 0;
		
		//标识为可用状态 
		for (i=1; i<=sList->capacity; i++) {
			sList->node[i].next = AVAILABLE;	
		}
	}
}

/*获取长度*/
int StaticList_Length(StaticList* list)
{
	TStaticList* sList = (TStaticList*)list;
	int ret = -1;
	
	if (sList != NULL) {
		ret = sList->header.data;
	}		
	
    	return ret;
}

/*获取容量*/
int StaticList_Capacity(StaticList* list)
{
	TStaticList* sList = (TStaticList*)list;
	int ret = -1;
	
	if (sList != NULL) {
		ret = sList->capacity;
	}		
	
    	return ret;	
}

/*插入结点*/
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)
{
	TStaticList* sList = (TStaticList*)list;
	int ret = (sList != NULL);
	int current = 0;
	int index = 0;	//标识可用位置下标 
	int i = 0;
	
	ret = ret && (sList->header.data+1 <= sList->capacity); 
	ret = ret && (pos >= 0) && (node != NULL);
	
	if (ret) {
		//查找可用位置 
		for (i=1; i<sList->capacity; i++) {
			if (sList->node[i].next == AVAILABLE) {
				index = i;
				break;	
			}
		}
		
		//插入结点
		sList->node[index].data = (unsigned int)node;
		
		sList->node[0] = sList->header;
		
		//开始移动结点 找pos位置 
		for (i=0; (i<pos) && (sList->node[current].next != 0); i++) {
			current = sList->node[current].next;	
		} 
		
		//修改指向 
		sList->node[index].next = sList->node[current].next;
		sList->node[current].next = index;
		
		//长度加1
		sList->node[0].data++;
		
		//更新表头信息 
		sList->header = sList->node[0]; 
	}
			
    	return ret;
}

/*获取结点元素*/
StaticListNode* StaticList_Get(StaticList* list, int pos)
{
	TStaticList* sList = (TStaticList*)list;
	StaticListNode* ret = NULL;
	int i = 0;
	int current = 0;
	int object = 0;
	
	if ((sList != NULL) && (0 <= pos) && (pos < sList->header.data)) {
		sList->node[0] = sList->header;
		
		//开始找pos位置的下标
		for (i=0; i<pos; i++) {
			current = sList->node[current].next;	
		}
		
		//保存pos位置下标 
		object = sList->node[current].next;
		
		//取值 
		ret = (StaticListNode*)(sList->node[object].data);
	}
		
	return ret;
}

/*删除结点*/ 
StaticListNode* StaticList_Delete(StaticList* list, int pos)
{
	TStaticList* sList = (TStaticList*)list;
	StaticListNode* ret = NULL;
	int i = 0;
	int current = 0;
	int object = 0;
	
	if ((sList != NULL) && (0 <= pos) && (pos < sList->header.data)) {
		sList->node[0] = sList->header;
		
		//开始找pos位置的下标
		for (i=0; i<pos; i++) {
			current = sList->node[current].next;	
		}
		
		//保存pos位置下标 
		object = sList->node[current].next;
		
		//删除结点 
		sList->node[current].next = sList->node[object].next;
		
		//长度减一 
		sList->node[0].data--;
		
		//更新表头信息 
		sList->header = sList->node[0];
		
		//标识为可用状态 
		sList->node[object].next = AVAILABLE;
		
		//取值 
		ret = (StaticListNode*)(sList->node[object].data);
	}
		
	return ret;
}

//StaticList.h文件
#ifndef _STATICLIST_H_
#define _STATICLIST_H_

typedef void StaticList;
typedef void StaticListNode;

StaticList* StaticList_Create(int capacity);

void StaticList_Destroy(StaticList* list);

void StaticList_Clear(StaticList* list);

int StaticList_Length(StaticList* list);

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

StaticListNode* StaticList_Get(StaticList* list, int pos);

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

#endif


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:163850次
    • 积分:2992
    • 等级:
    • 排名:第11758名
    • 原创:135篇
    • 转载:62篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论