【数据结构】静态链表

前端之家收集整理的这篇文章主要介绍了【数据结构】静态链表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


数据结构静态链表实现


前言

静态链表,是一种巧妙的数据结构实现方式。

静态链表:

每个节点有一个数据域(data),用来存放有用的数据信息;

还有一个下标域(cur),用来指示下一个元素的下标位置。


我们都知道链式线性表的优势在于插入和删除元素,时间复杂度都是O(1),因为不需要像顺序存储结构的线性表那样在插入和删除时需要移动大量的元素。

而静态链表的实现就弥补了这样的不足,我们以下标的方式来代替链式结构中使用的的指针,从而达到减少时间复杂度的功能


分析



实现静态链表

下面是代码,实现了最基本的几个函数包括插入元素删除元素获取和释放备用链表空间。

读者可以自行添加其他的操作函数

//======================================================================    
//    
//        Copyright (C) 2014-2015 SCOTT        
//        All rights reserved    
//    
//        filename: StaticList.c  
//        description: a demo to display StaticList
//    
//        created by SCOTT at  02/10/2015
//        http://blog.csdn.net/scottly1  
//    
//======================================================================    

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

#define TRUE		1
#define FALSE		0
#define MAX_LEN		100

typedef  int  Status;
typedef  int  ElemType;

typedef struct _tag_staticlist
{
	ElemType data;
	int cur;
}StaticList,List[MAX_LEN];


/* Init StaticList */
Status InitList(StaticList * list)
{
	int i = 0;

	for(i=0; i<MAX_LEN - 1; i++)
	{
		list[i].cur = i + 1;
		list[i].data = 0;    //此处初始化为0 是为了方便观看 实际中ElemType不一定是int型
	}

	list[MAX_LEN-1].cur = 0;
	list[MAX_LEN-1].data = 0;

	return TRUE;
}

/* Display StaticList */
void DisplayList(StaticList * list)
{
	int i = 0;

	for(i=0; i<MAX_LEN; i++)
	{
		printf("%d.cur = %d,%d.data = %d\n",i,list[i].cur,list[i].data);
	}

	return ;
}

/* Malloc StaticList,Get Free Space */
int Malloc_SL(StaticList * list)
{
	int ret = 0;

	ret = list[0].cur;

	list[0].cur = list[ret].cur;  // 指向下一个节点的cur

	return ret;
}

/* Free StaticList,Free Space */
Status Free_SL(StaticList * list,int j)
{
	list[j].cur = list[0].cur;   // 保存备用链表第一个节点下一个节点cur

	list[0].cur = j;             // j成为备用链表的首节点下标

	return TRUE;
}

/* Insert StaticList */
Status InsertList(StaticList * list,int pos,ElemType data)
{
	int i = 0;
	int j = 0;
	int k = MAX_LEN - 1;

	if(pos < 1 || NULL == list || pos >= MAX_LEN - 1)
		return FALSE;
		
	j = Malloc_SL(list);                    // Malloc cur
	list[j].data = data;			// Insert data
	for(i=1; i<pos; i++)
	{
		k = list[k].cur;
	}
		
	list[j].cur = list[k].cur;
	list[k].cur = j;

	return TRUE;
}


Status InsertList(StaticList * list,ElemType *ret)
{
	int i = 0;
	int j = 0;
	int k = MAX_LEN - 1;

	if(pos < 1 || NULL == list || pos >= MAX_LEN - 1)
		return FALSE;

	for(i=1; i<pos; i++)
	{
		k = list[k].cur;
	}

	j = list[k].cur;

	*ret = list[j].data;	

	Free_SL(list,j);

	return TRUE;
}


int main()
{
	List  L ;
	InitList(L);
	DisplayList(L);
	ElemType ret;

	printf("=============\n");
	InsertList(L,1,11);
	InsertList(L,2,22);
	InsertList(L,3,33);
	InsertList(L,4,44);
	InsertList(L,5,55);

	InsertList(L,66);
	DisplayList(L);

	printf("=============\n");
	InsertList(L,&ret);
	DisplayList(L);
	printf("%d has been removed!\n",ret);

	return 0;
}

运行结果:


原创文章,转载请著名出处:http://blog.csdn.net/scottly1/article/details/43669933

原文链接:https://www.f2er.com/datastructure/382707.html

猜你在找的数据结构相关文章