栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

再学C的数据结构---静态链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

再学C的数据结构---静态链表

StaticList.h

#include 
#include 
#include 

#define MAX_SIZE 20
typedef char Elemtype ;

typedef struct ListNode
{
	Elemtype data;
	int cur; //游标
}ListNode;

typedef ListNode StaticList[MAX_SIZE];
void InitSList(StaticList &space);
void Free_SL(StaticList& space, int k);
int Alloc_SL(StaticList &space);
void Insert(StaticList& space, const Elemtype x);
void Delete(StaticList& space);
void ShowList(StaticList& space);

StaticList.cpp

#include "StaticList.h"



void InitSList(StaticList &space)
{
	for (int i = 1; i < MAX_SIZE - 1; ++i)
	{
		space[i].cur = i + 1;
	}
	space[MAX_SIZE - 1].cur = 0;
	space[0].cur = -1;
}



void Free_SL(StaticList& space, int k)
{
	space[k].cur = space[1].cur;
	space[1].cur = k;
}



int Alloc_SL(StaticList &space)
{
	//获取插入结点的下标
	int i = space[1].cur;

	if (space[1].cur != 0)
	{
		//更新下一个插入结点的下标
		space[1].cur = space[i].cur;
	}
	return i;
}




void Insert(StaticList& space, const Elemtype x)
{
	int i = Alloc_SL(space);
	if (i == 0)
	{
		printf("申请结点失败!n");
		return;
	}

	//插入的第一个结点 cur为 -1
	if (space[0].cur == -1)
	{
		space[i].cur = -1;
	}
	else
	{
		space[i].cur = space[0].cur;
	}
	space[0].cur = i;
	space[i].data = x;
}



void Delete(StaticList& space)
{
	int i = space[0].cur;
	space[0].cur = space[i].cur;
	Free_SL(space, i);
}



void ShowList(StaticList& space)
{
	int i = space[0].cur;
	while (i != -1)
	{
		printf("%c--->", space[i].data);
		i = space[i].cur;
	}
	printf("NULLn");
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号