【数据结构】静态链表

静态链表

用数组的方式实现的链表

优点

增,删操作不需要移动大量元素

缺点

不能随机存取

只能由头结点开始依次往后查找

容量固定不变

c代码实现

获取最后一个结点的下标以及获取第一个空闲节点的位置

//获取尾节点坐标
int getLastNode(StaticList list)
{
    for(int i=0; i<100; i++)
    {
        if(list[i].next==-1)
        {
            return i;
        }
    }
}

//获取第一个未被利用的空闲区域
int getLastIndex(StaticList list)
{
    if(state)
    {
        state=false;
        size=100;
        for(int i=0; i<100; i++)
        {
            if(list[i].next==-2)
            {
                size=i;
                break;
            }
        }
    }
    return size;
}

初始化静态链表

next=-2只是为了做一个标记,代表该空间未被占用

//初始化一个静态链表
void initList(StaticList *list)
{
    (*list)[0]=createStart();
    int i=1;
    //这边对空节点进行标记,代表这些节点还未被使用
    for(i; i<100; i++)
    {
        (*list)[i].next=-2;
    }
}

在最后添加一个节点

//在最后添加数据
void addLast(StaticList *list,Node * node)
{
    int insertIndex=getLastIndex(*list);
    if(insertIndex==100)
    {
        //列表已满
        return ;
    }
    else
    {
        (*list)[insertIndex]=*node;

    }
    (*list)[insertIndex]=*node;
    (*list)[insertIndex].next=-1;
    (*list)[getLastNode(*list)].next=insertIndex;
    state=true;
}

在某坐标后添加一个节点

//在一个坐标后添加节点
void addAfterIndex(StaticList *list,int index,Node *node)
{
    int insertIndex=getLastIndex(*list);
    if(index<0||index>98)
    {
        return;
    }
    if(insertIndex==100)
    {
        //列表已满
        return ;
    }
    else
    {
        (*list)[insertIndex]=*node;
    }
    (*list)[insertIndex].next=(*list)[index].next;
    (*list)[index].next=insertIndex;
    state=true;
}

遍历

//遍历
void forEach(StaticList list){
    Node node=list[0];
    do{
        node=list[node.next];
        printf("%d\n",node.data);
    }while(node.next!=-1);
}

总代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 100
bool state=true;
int size;
typedef struct
{
    int data;   //存放的数据
    int next;  //下一个节点的下标
} StaticList[MaxSize],Node;
//创建一个头结点
Node createStart()
{
    Node start;
    start.next=-1;
    return start;
}
//初始化一个静态链表
void initList(StaticList *list)
{
    (*list)[0]=createStart();
    int i=1;
    //这边对空节点进行标记,代表这些节点还未被使用
    for(i; i<100; i++)
    {
        (*list)[i].next=-2;
    }
}

//获取尾节点坐标
int getLastNode(StaticList list)
{
    for(int i=0; i<100; i++)
    {
        if(list[i].next==-1)
        {
            return i;
        }
    }
}

//获取第一个未被利用的空闲区域
int getLastIndex(StaticList list)
{
    if(state)
    {
        state=false;
        size=100;
        for(int i=0; i<100; i++)
        {
            if(list[i].next==-2)
            {
                size=i;
                break;
            }
        }
    }
    return size;
}

//在最后添加数据
void addLast(StaticList *list,Node * node)
{
    int insertIndex=getLastIndex(*list);
    if(insertIndex==100)
    {
        //列表已满
        return ;
    }
    else
    {
        (*list)[insertIndex]=*node;

    }
    (*list)[insertIndex]=*node;
    (*list)[insertIndex].next=-1;
    (*list)[getLastNode(*list)].next=insertIndex;
    state=true;
}

//遍历
void forEach(StaticList list){
    Node node=list[0];
    do{
        node=list[node.next];
        printf("%d\n",node.data);
    }while(node.next!=-1);
}

//在一个坐标后添加节点
void addAfterIndex(StaticList *list,int index,Node *node)
{
    int insertIndex=getLastIndex(*list);
    if(index<0||index>98)
    {
        return;
    }
    if(insertIndex==100)
    {
        //列表已满
        return ;
    }
    else
    {
        (*list)[insertIndex]=*node;
    }
    (*list)[insertIndex].next=(*list)[index].next;
    (*list)[index].next=insertIndex;
    state=true;
}

void main()
{

    //此时一个StaticList就是一个静态列表
    StaticList list;
    initList(&list);
    Node a;
    a.data=1;
    addLast(&list,&a);
    a.data=2;
    addLast(&list,&a);
    a.data=3;
    addAfterIndex(&list,1,&a);
    forEach(list);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王不头秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值