静态链表 StaticList

单链表的实现严重依赖指针,数据元素中必须包含一个额外的指针域,没有指针的程序设计语言无法实现。

静态链表的定义

顺序表数组中的元素由两个数据域组成:data和next
data域用于存储数据
next域用于存储下一个元素在数组中的下标

如下图所示:
这里写图片描述

静态链表是在顺序表的基础上利用数组实现的单链表

这里写图片描述

代码编写

//静态链表头文件 StaticList.h
#ifndef _STATICLIST_H_
#define _STATICLIST_H_

typedef void StaticList;
typedef void StaticListNode;
//此处定义void,目的是数据封装。

StaticList* StaticList_Create(int capacity);
void StaticList_Destroy(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Length(StaticList* list);
int StaticList_Capacity(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
#include <stdio.h>
#include <malloc.h>
#include "StaticList.h"

#define AVAILABLE -1 
//-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) // O(n)  创建静态链表
{
    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) // O(1) 销毁静态链表
{
    free(list);
}

void StaticList_Clear(StaticList* list) // O(n) 清空数据元素
{
    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) // O(1) 获取当前链表的长度
{
    TStaticList* sList = (TStaticList*)list;
    int ret = -1;    
    if( sList != NULL )
    {
        ret = sList->header.data;
    }    
    return ret;
}

int StaticList_Capacity(StaticList* list) // O(1) 获取链表的最大长度
{
    TStaticList* sList = (TStaticList*)list;
    int ret = -1;   
    if( sList != NULL )
    {
        ret = sList->capacity;
    }   
    return ret;
}

int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)  // O(n)
// 在链表的指定位置插入一个数据元素
{
    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;//把表头数据放入数组的0位置处        
        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;       
        sList->node[0].data++;      
        sList->header = sList->node[0];
    }    
    return ret;
}

StaticListNode* StaticList_Get(StaticList* list, int pos)  // O(n) 获取某位置上的数据元素
{
    TStaticList* sList = (TStaticList*)list;
    StaticListNode* ret = NULL;
    int current = 0;
    int object = 0;
    int i = 0;    
    if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
    {
        sList->node[0] = sList->header;       
        for(i=0; i<pos; i++)
        {
            current = sList->node[current].next;
        }        
        object = sList->node[current].next;        
        ret = (StaticListNode*)(sList->node[object].data);
    }    
    return ret;
}

StaticListNode* StaticList_Delete(StaticList* list, int pos) // O(n) 删除某个数据元素
{
    TStaticList* sList = (TStaticList*)list;
    StaticListNode* ret = NULL;
    int current = 0;
    int object = 0;
    int i = 0;    
    if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
    {
        sList->node[0] = sList->header;        
        for(i=0; i<pos; i++)
        {
            current = sList->node[current].next;
        }      
        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;
}

为什么静态链表结构体中要在定义个header成员,而不直接使用node[0]?

在静态链表中 我们把所有的空闲结点都用-1来标记当插入一个元素的时候 我们需要遍历查找空闲结点 你想想这样是不是有点低效了。

所以一个想法就是把所有的空闲结点也连接成一个链表 那么也就是说在同一段空间中 部分结点是静态链表存储了实际的数据元素 部分没有用的空闲结点连接成空闲链表。

那么你想想是不是我们需要两个表头来标识两个链表呢 因此我们把0元素空出来 当把0元素赋值为静态链表头的时候 从0开始访问的就是静态链表元素 当把0元素赋值为空闲链表头的时候 从0开始访问的就是空闲链表元素。

这样子如果我们插一个新元素时 直接从空闲链表中拿一个空间出来 存储新元素即可 这个方法从空间链表取空间的时间复杂度为O(1)。

小结

静态链表其实是单链表的另一种实现方式
静态链表的实现“媒介”不是指针而是数组
静态链表主要用于不支持指针的程序设计语言中
静态链表的实现是一种内存管理的简易方法

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值