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

静态链表的部分实现

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

静态链表的部分实现

//List_SL_Head.h

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#include 
#include 
#include 
#include 

typedef int Status;
typedef int ElemType;

#define MAXSIZE 1000

typedef struct
{
    ElemType data;
    int cur;
}component, SLinkList[MAXSIZE];

//Status CreateList_SL(SLinkList, int);
void ListDisp(SLinkList, int);
//打印静态链表(调试用)

int Malloc_SL(SLinkList);
//获取一个未使用的节点

void Free_SL(SLinkList, int);
//释放一个节点

void InitSpace_SL(SLinkList);
//对静态链表初始化
//List_SL.c
#include "List_SL_Head.h"

//Yan W-M: Use (SlinkList) space to record the nodes that are didn't used.
//For example: space[0].cur points to the first node didn't used.
//S = Malloc_SL(space); to get a node to use. 
//S points to the head of list, R points to the rear of list.
//In a word: starts from space[0] to gets the nodes didn't used.
//to get the nodes that have recorded messages (List), have to start from space[S];


void ListDisp(SLinkList sl, int s)
// Display the static list.
{
    int i = sl[s].cur;
    while (i)
    {
        printf("%d ", sl[i].data);
        i = sl[i].cur;
    }
    printf("n");
}

void InitSpace_SL(SLinkList sl)
{
    for (int i = 0; i < MAXSIZE - 1; ++i)
    {
        sl[i].cur = i + 1;
    }
    sl[MAXSIZE-1].cur = 0;
}

int Malloc_SL(SLinkList sl)
// If still have memory, get a new node and return the index of new node.
{
    int i= sl[0].cur;
    if (i) sl[0].cur = sl[i].cur;
    return i;
}

void Free_SL(SLinkList sl, int k)
{
    int i = sl[0].cur;
    sl[0].cur = k;
    sl[k].cur = i;
}

静态链表创建方法:

(1) 建立一个固定大小的结构体数组,对其初始化。

即:space[0].cur = 1;space[1].cur = 2;...; space[MaxSize - 1].cur = 0;

(2) 默认从space[0]开始遍历时,获取的是未使用的节点。

(3) 对于已使用的节点(即链表),需要用整数 S 记录其下标,从S开始遍历即可遍历链表。

核心:静态链表实现;Malloc_SL() 和 Free_SL()实现;静态链表思想。

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

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

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