【数据结构】静态链表


数据结构静态链表实现


前言

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

静态链表:

        每个节点有一个数据域(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, i, 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, int pos, 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, 3, 66);
	DisplayList(L);

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

	return 0;
}

运行结果:


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


  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值