C 静态链表实现逻辑

静态链表问题实质是:不使用指针,使用数组实现链表结构,所以关键是不用结构体,通过自己实现分配节点和回收节点实现类似系统的内存回收的效果;
思路:建立两个数组,一个记录数据,一个记录数组是否被占用(类似内存是否被占用):
关键是maloc2 和 fre 函数,自己想到的方式就是这样,需要分配定量的两份内存,复杂度嘛

#include <stdio.h>
#include <stdlib.h>
#include "testhead.h"

/**
 * 实现静态链表
 * 实际存储位置是在已经分配好的数组里,通过数组位置进行链表的变动,然后
 * 节点的回收,节点存储,节点删除,节点添加
 * 不能使用指针  只能使用数组
 */
static int all[1000] = { };
static int ain[1000] = { };
static int one; //the first location about the linklist which is easy to delete first
//对静态链表进行测试
void testStaLinklist() {
    //建立第一个节点的值
    int first = maloc2();
    all[first] = 1;
    all[first + 1] = -1; // use -1 to make have no next location
    one = first;
    addval(first, 2);
    addval(first, 3);
    addval(first, 4);
    addval(first, 5);
    addval(first, 6);
    addval(first, 7);
    deleValue(first,7);
//  int i=maloc2();
//  printf("%d\n",i)
    showall(one);

}
//返回一个可用下标,并将下标所代表的数组置为1 分配两个位置,一个存储数据,一个存储下一个位置
int maloc2() {
    int i;
    for (i = 0; i < 1000; i++) {
        if (ain[i] == 0) {
            ain[i] = 1;
            if (ain[i + 1] == 0)
                ain[i + 1] = 1;
            else
                return -1;
            return i;
        }
    }
    return -1;
}

//将这个下标的数组值置为0 表示未被占用,每次回收两个位置,也就是将两个位置数字置为0
int fre(int index) {
    if (ain[index] == 1 && ain[index + 1] == 1) {
        ain[index] = 0;
        ain[index + 1] = 0;
        return 1;
    } else {
        return -1;
    }
}
// add the node to the link
int addval(int first, int value) {
    int next = all[first + 1];
    if (next != -1) {
        return addval(next, value);
    }
    int nowpo = maloc2();
    all[nowpo] = value;
    all[nowpo + 1] = -1;
    all[first + 1] = nowpo;
    return 1;
}
int showall(int first) {
    printf("%d\n", all[first]);
    if (all[first + 1] != -1) {
        return showall(all[first + 1]);
    }
    return 1;
}
//delete the first ,not use this now
int  delefirst(int first) {
    int value=all[first];
    one=all[first+1];
    fre(first);
    return value;
}
//delete the last and return the delete value
int delelast(int first) {
    if (all[one + 1] == -1) {
        fre(one);
        int value = all[one];
        one = -1;
        return value;
    }
    if (all[all[first + 1] + 1] != -1) {
        return delelast(all[first + 1]);
    }
    int value = all[all[first + 1]];
    fre(all[first + 1]);
    all[first + 1] = -1;
    return value;
}
//delete by tht value,the test is right
int deleValue(int first,int val) {
   if(all[one]==val){
       return delefirst(first);
   }
   if(all[all[first+1]]!=val){
       return deleValue(all[first+1],val);
   }
   fre(all[first+1]);
   all[first+1]=all[all[first+1]+1];
   return val;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值