数据结构 - 静态链表

同之前的线性表一样,静态链表也是一种用线性形式来存储数据的一种容器

但是和顺序表、单链表不一样的是,静态链表这个东西……它是用数组来代替指针,来描述单链表der……

也就是说,

它是将数组元素分成两个数据域–>data和cur。

data用来存放数据元素,cur存放该元素的后继在数组中的下标。所以它有个爱称叫做单链表的<游标实现法>(不知道讲的对不对系列,应该算爱称吧)

文绉绉,讲起来怪怪的,

代码写一下就是这个样子
在代码外面加个注释
线性表特别注释
就是说,(有点奇怪的重要)包括静态链表在内的所有线性表,都可以存储包括int之内的好多数据类型,比如char……

我在说什么……

然后这篇和之前写的博客里,本着容易理解方便我打代码的原则,我全是用int代替的ElemType然后这里再讲一下= =

typedef int ElemType;
typedef struct _staticList
{
	ElemType data;
	int cur;
}StaticList[MAXSIZE];

这个形式就是静态链表

每一个结构体都要存他的数据,还有放置下一个数据的下标位置,一个数组表示一共有 maxsize个数据

看起来不太难,但是有点绕

可以百度一下大话数据结构次次,个人觉得挺好的,我们老师上课也用的那个当例子

然后基本操作+不基本但是要会的操作

//将结构体数组中所有分量链接到备用链表中
void reserveArr(component *array);
//初始化静态链表
int initArr(component *array);
//向链表中插入数据,body表示链表的头结点在数组中的位置,add表示插入元素的位置,a表示要插入的数据
void insertArr(component * array,int body,int add,int a);
//删除链表中含有字符a的结点
void deletArr(component * array,int body,int a);
//查找存储有字符elem的结点在数组的位置
int selectElem(component * array,int body,int elem);
//将链表中的字符oldElem改为newElem
void amendElem(component * array,int body,int oldElem,int newElem);
//输出函数
void displayArr(component * array,int body);
//从备用链表中摘除空闲节点的实现函数
int mallocArr(component * array);
//将摘除下来的节点链接到备用链表上
void freeArr(component * array,int k);


慢慢来

我先贴个代码去吃饭,等下就上数据结构课惹- -(这版本的是用来测试的所以很多函数是定死的,我决定等下午上课的时候改一下)


中午吃完小煎饼的我补上了我新版本的代码,又删掉了我的函数 = =

#include <stdio.h>
#define maxSize 7
typedef struct {
    int data;
    int cur;
}component;

void reserveArr(component *array);
int initArr(component *array);
void insertArr(component * array,int body,int add,int a);
void deletArr(component * array,int body,int a);
int selectElem(component * array,int body,int elem);
void amendElem(component * array,int body,int oldElem,int newElem);
void displayArr(component * array,int body);
int mallocArr(component * array);
void freeArr(component * array,int k);

int main() {
    component array[maxSize];
    int body=initArr(array);
    printf("Now list:\n");
    displayArr(array, body);
	int insert_index,insert_num;
	printf("Input the index & number tou want to insert:");
	scanf("%d %d",&insert_index,&insert_num);
	printf("Insert %d to %d:",insert_num,insert_index);
    insertArr(array,body,insert_index,insert_num);
    displayArr(array,body);
    printf("Input the number you want to delete:");
	int delete_num;
	scanf("%d",&delete_num);
    deletArr(array, body, delete_num);
    displayArr(array, body);
    
    int select_num;
    printf("Input thie num you want to find the index:");
    scanf("%d",&select_num);
    printf("The index of this num is:");
    int selectAdd=selectElem(array,body,select_num);
    printf("%d\n",selectAdd);
    printf("Input the num you want to change:");
    int change_num;
    scanf("%d",&change_num);
    amendElem(array,body,select_num,change_num);
    displayArr(array, body);
    return 0;
}

void reserveArr(component *array){
    for (int i = 0;i < maxSize;i++) {
        array[i].cur=i+1;
    }
    array[maxSize-1].cur=0;
}

int initArr(component *array,int nn){
    reserveArr(array);
    int body = mallocArr(array);
    int tempBody = body;
    for (int i = 1;i < nn;i++) {
        int j=mallocArr(array);
        array[tempBody].cur=j;
        array[j].data=i-1;
        tempBody=j;
	}
    array[tempBody].cur=0;
    return body;
}

void insertArr(component * array,int body,int add,int a){
    int tempBody=body;
    for (int i = 1;i < add;i++) {
        tempBody=array[tempBody].cur;
    }
    int insert=mallocArr(array);
    array[insert].cur=array[tempBody].cur;
    array[insert].data=a;
    array[tempBody].cur=insert; 
}

void deletArr(component * array,int body,int a){
    int tempBody = body;
    while (array[tempBody].data != a) {
        tempBody = array[tempBody].cur;
        if (tempBody == 0) {
            printf("NOT FOUND!\n");
            return;
        }
    }
	int del=tempBody;
    tempBody=body;
	while (array[tempBody].cur!=del) {
        tempBody=array[tempBody].cur;
    }
	array[tempBody].cur=array[del].cur;
  
    freeArr(array, del);
}

int selectElem(component * array,int body,int elem){
    int tempBody=body;
    while (array[tempBody].cur!=0) {
        if (array[tempBody].data==elem) {
            return tempBody;
        }
        tempBody=array[tempBody].cur;
    }
    return -1;
}

void amendElem(component * array,int body,int oldElem,int newElem){
    int add=selectElem(array, body, oldElem);
    if (add==-1) {
        printf("WITHOUT THIS NUMBER!\n");
        return;
    }
    array[add].data=newElem;
}

void displayArr(component * array,int body){
    int tempBody=array[body].cur;
    while (array[tempBody].cur) {
//      printf("%d,%d  |",array[tempBody].data,array[tempBody].cur);
		printf("%d,",array[tempBody].data);
        tempBody=array[tempBody].cur;
    }
    printf("%d\n",array[tempBody].data);
//    printf("%d,%d\n",array[tempBody].data,array[tempBody].cur);
}

int mallocArr(component * array){
    int i=array[0].cur;
    if (array[0].cur) {
        array[0].cur=array[i].cur;
    }
    return i;
}

void freeArr(component * array,int k){
    array[k].cur=array[0].cur;
    array[0].cur=k;
}

测试结果:
静态链表的测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值