【数据结构】线性链表(3)静态链表

静态链表

(1)构建一个结构体数组当成备用链表,如list[100]。

(2)初始化链表:备用链表从list[0]开始初始化,到list[97],list[98],list[99]

置为0,静态链表将list[99]当成头结点不存储任何数据。

(3)插入时,需要从备用链表中Malloc一个区域存放插入的数据。

#define MAX_SIZE 10
typedef int ElemType;
struct List
{
    ElemType data;
    int cur;
};

//静态链表基本操作(13个)
//1从备用链表中分配一个区域,返回分配的结点下标
int Malloc(struct List list[MAX_SIZE])
{
    int i = list[0].cur;
    if (i)
    {
        list[0].cur = list[i].cur;
    }
    return i;
}

//2空闲结点回收到备用链表
void Free(struct List list[MAX_SIZE], int k)
{
    list[k].cur = list[0].cur;
    list[0].cur = k;
}

//3初始化静态链表(关键)
void init_static(struct List list[MAX_SIZE])
{
    int i;
    list[MAX_SIZE - 1].cur = 0;
    for (i = 0; i < MAX_SIZE - 2; i++)
    {
        list[i].cur = i + 1;
    }
    list[MAX_SIZE - 2].cur = 0;
}

//4将静态链表置为空
void clear_list(struct List list[MAX_SIZE])
{
    int j;
    int i = list[0].cur;
    while (i)
    {
        j = i;
        i = list[i].cur;
    }
    list[j].cur = list[MAX_SIZE - 1].cur;
    list[MAX_SIZE - 1].cur = 0;
}

//5判断静态链表是否为空
bool is_empty(struct List list[MAX_SIZE])
{
    if (list[MAX_SIZE-1].cur == 0)
        return true;
    else
        return false;
}

//6返回静态链表中元素个数
int list_length(struct List * list)
{
    int j = 0;
    int i = list[MAX_SIZE - 1].cur;
    while (i)
    {
        i = list[i].cur;
        j++;
    }
    return j;
}

//7用val返回第pos个元素
bool getlist_value(struct List list[MAX_SIZE], int pos, ElemType * val)
{
    if (pos < 1 || pos > list_length(list))
        return false;
    int i;
    int k = MAX_SIZE - 1;
    for (i = 1; i <= pos; i++)
    {
        k = list[k].cur;
    }
    *val = list[k].data;

    return true;
}

//8查找元素并返回位序
int locate_list(struct List list[MAX_SIZE], ElemType val)
{
    int i = list[MAX_SIZE - 1].cur;
    while (i && list[i].data != val)
    {
        i = list[i].cur;
    }
    return i;
}

//9将元素插入第pos个位置
bool insert_list(struct List list[MAX_SIZE], int pos, ElemType val)
{
    int m, j, k = MAX_SIZE - 1;
    if (pos < 1 || pos > list_length(list) + 1)
        return false;
    j = Malloc(list);
    if (j)
    {
       list[j].data = val;
        for (m = 1; m < pos; m++)
        {
            k = list[k].cur;
        }
        list[j].cur = list[k].cur;  //改变pos前一个位置的cur
        list[k].cur = j;

        return true;
    }

    return false;
}

//10用pre_e返回某个元素的前一个元素
bool prior_list(struct List list[MAX_SIZE], ElemType cur_e, ElemType * pre_e)
{
    int j;
    int i = list[MAX_SIZE - 1].cur;
    i = list[i].cur;
    while (i && cur_e != list[i].data)
    {
        j = i;
        i = list[i].cur;
    }
    if (i)
    {
        *pre_e = list[j].data;
        return true;
    }
    return false;
}
//11用next_e返回某个元素的后一个元素
bool next_list(struct List list[MAX_SIZE], ElemType cur_e, ElemType * next_e)
{
    int j;
    int i = locate_list(list, cur_e);
    if (i)
    {
        j = list[i].cur;
        if (j)
        {
            *next_e = list[j].data;
            return true;
        }
    }
    return false;
}

//12删除pos位置上的元素并用val接收值
bool delete_list(struct List list[MAX_SIZE], int pos, ElemType * val)
{
    int j;
    int k = MAX_SIZE - 1;
    if (pos < 1 || pos > list_length(list))
        return false;
    for (j = 1; j < pos; j++)
    {
        k = list[k].cur;
    }
    j = list[k].cur;
    list[k].cur = list[j].cur;  //将待删除节点的下一个位置赋给上一个位置
    *val = list[j].data;
    Free(list, j);
    return true;
}

//13遍历
void traverse_list(struct List * list)
{
    int i = list[MAX_SIZE - 1].cur;
    while (i)
    {
        printf("%d ", list[i].data);
        i = list[i].cur;
    }
    printf("\n");
}

测试

#include <stdio.h>
#include <stdbool.h>

int main() {
    struct List list[MAX_SIZE];
    init_static(list);
    insert_list(list, 1, 2);
    insert_list(list, 2, 3);
    insert_list(list, 3, 5);
    insert_list(list, 4, 7);
    insert_list(list, 5, 8);
    traverse_list(list);
    ElemType val;
    getlist_value(list, 2, &val);
    printf("val = %d\n", val);
    int len = list_length(list);
    printf("len = %d\n", len);
    ElemType delete_val;
    delete_list(list, 3, &delete_val);
    printf("delete_val = %d\n", delete_val);
    traverse_list(list);
    int cnt;
    cnt = locate_list(list, 3);
    printf("cnt = %d\n", cnt);

    ElemType pre_e;
    prior_list(list, 7, &pre_e);
    printf("pre_e = %d\n", pre_e);

    ElemType next_e;
    next_list(list, 7, &next_e);
    printf("next_e = %d\n", next_e);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值