数据结构线性表--静态链表

1.静态链表产生的原因?

因为顺序存储结构和链式存储结构两者都有各自的优点,如下表所示。

性能具体功能顺序存储链式存储
空间性能存储分配一次性分配动态分配(优)
空间性能存储密度
(下面会有解释)
=1(优)<1
时间性能查找O(n/2)O(n/2)
时间性能读运算O(1)(优)O((n+1)/2)
最好情况为1,最坏情况为n
时间性能插入运算
(性能主要指
需要移动元素个数)
O(n/2)最好
最好情况为0,最坏情况为n
O(1),更优
时间性能删除运算O((n-1)/2)
最好情况为0,最坏情况为n-1
O(1),更优

所以我们需要产生一种存储结构,可以融合顺序表和链表的优点,从而达到快速访问元素,又能快速增加或删除数据元素,所以产生了静态链表

2.静态链表

静态链表:也是线性存储结构的一种,它兼顾了顺序表和链表的优点。

特点:
1.一次性分配所以内存
2.数据全部存储在数组中,且存储位置是随机的。
3.数据之间的"一对一“的逻辑关系通过一个整形变量”游标"维持。

存储结构如图所示:
在这里插入图片描述
结构体类型:

typedef struct LNode{
	int data;//数据域:用来存储数据。
	int next;//游标:用来找到后继元素所在数组中的位置。
}LNode;

4.备用链表

静态链表中,除了数据本身通过游标组成的链表外,还需要有一条连接各个空闲位置的链表,称为备用链表。

在这里插入图片描述
备用链表的作用:可以清楚地知道数组中是否有空闲位置,以便添加数据时使用。
当静态链表中数组下标为 0 的位置上存有数据,则证明数组已满

3.创建静态链表

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

typedef struct LNode{
    int data;
    int next;
}LNode;

//创建备用链表
void reserveArr(LNode *array){
    for(int i=0;i<maxSize;i++){
        array[i].next=i+1;
        array[i].data=-1;
    }
    array[maxSize-1].next=0;
}

//提取分配空间
int mallocArr(LNode *array){
    int i=array[0].next;
    if(array[0].next){
        array[0].next=array[i].next;
    }
    return i;
}

//创建静态链表
int initArr(LNode *array){
    reserveArr(array);
    int body=mallocArr(array);
    int tempBody=body;
    int length;
    cin>>length;
    for(int i=1;i<length+1;i++){
        int j=mallocArr(array);
        array[tempBody].next=j;
        int temp;
        cin>>temp;
        array[j].data=temp;
        tempBody=j;
    }
    array[tempBody].next=0;
    return body;
}

//遍历输入
void dispalyArr(LNode *array,int body){
    int tempBody=body+1;
    while(array[tempBody].next){
        cout<<array[tempBody].data<<" ";
        tempBody=array[tempBody].next;
    }
    cout<<array[tempBody].data<<" ";
}

int main(){
    LNode array[maxSize];
    int body=initArr(array);
    dispalyArr(array,body);
    return 0;
}

结果:
在这里插入图片描述

4.添加数据


//插入数据
void insertArr(LNode * array,int body,int add,char a){
    int tempBody=body;//tempBody做遍历结构体数组使用
    //找到要插入位置的上一个结点在数组中的位置
    for (int i=1; i<add; i++) {
        tempBody=array[tempBody].next;
    }
    int insert=mallocArr(array);//申请空间,准备插入
    array[insert].data=a;
    array[insert].next=array[tempBody].next;//新插入结点的游标等于其直接前驱结点的游标
    array[tempBody].next=insert;//直接前驱结点的游标等于新插入结点所在数组中的下标
}

整体代码:

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

typedef struct LNode{
    int data;
    int next;
}LNode;

//创建备用链表
void reserveArr(LNode *array){
    for(int i=0;i<maxSize;i++){
        array[i].next=i+1;
        array[i].data=-1;
    }
    array[maxSize-1].next=0;
}

//提取分配空间
int mallocArr(LNode *array){
    int i=array[0].next;
    if(array[0].next){
        array[0].next=array[i].next;
    }
    return i;
}

//创建静态链表
int initArr(LNode *array){
    reserveArr(array);
    int body=mallocArr(array);
    int tempBody=body;
    int length;
    cin>>length;
    for(int i=1;i<length+1;i++){
        int j=mallocArr(array);
        array[tempBody].next=j;
        int temp;
        cin>>temp;
        array[j].data=temp;
        tempBody=j;
    }
    array[tempBody].next=0;
    return body;
}

//插入数据
void insertArr(LNode * array,int body,int add,char a){
    int tempBody=body;
    for (int i=1; i<add; i++) {
        tempBody=array[tempBody].next;
    }
    int insert=mallocArr(array);
    array[insert].data=a;
    array[insert].next=array[tempBody].next;
    array[tempBody].next=insert;
}

//遍历输入
void dispalyArr(LNode *array,int body){
    int tempBody=body+1;
    while(array[tempBody].next){
        cout<<array[tempBody].data<<" ";
        tempBody=array[tempBody].next;
    }
    cout<<array[tempBody].data<<" ";
}

int main(){
    LNode array[maxSize];
    int body=initArr(array);
    dispalyArr(array,body);
    cout<<endl;
    insertArr(array,body,2,10);
    dispalyArr(array,body);
    return 0;
}

结果:
在这里插入图片描述

5.删除操作

//备用链表回收空间的函数,其中array为存储数据的数组,k表示未使用节点所在数组的下标
void freeArr(LNode * array,int k){
    array[k].next=array[0].next;
    array[0].next=k;
}
//删除结点函数,a 表示被删除结点中数据域存放的数据
void deletArr(LNode * array,int body,char a){
    int tempBody=body;
    //找到被删除结点的位置
    while (array[tempBody].data!=a) {
        tempBody=array[tempBody].next;
        //当tempBody为0时,表示链表遍历结束,说明链表中没有存储该数据的结点
        if (tempBody==0) {
            printf("链表中没有此数据");
            return;
        }
    }
    //运行到此,证明有该结点
    int del=tempBody;
    tempBody=body;
    //找到该结点的上一个结点,做删除操作
    while (array[tempBody].next!=del) {
        tempBody=array[tempBody].next;
    }
    //将被删除结点的游标直接给被删除结点的上一个结点
    array[tempBody].next=array[del].next;
    //回收被摘除节点的空间
    freeArr(array, del);
}

//插入数据
void insertArr(LNode * array,int body,int add,char a){
    int tempBody=body;//tempBody做遍历结构体数组使用
    //找到要插入位置的上一个结点在数组中的位置
    for (int i=1; i<add; i++) {
        tempBody=array[tempBody].next;
    }
    int insert=mallocArr(array);//申请空间,准备插入
    array[insert].data=a;
    array[insert].next=array[tempBody].next;//新插入结点的游标等于其直接前驱结点的游标
    array[tempBody].next=insert;//直接前驱结点的游标等于新插入结点所在数组中的下标
}

//总共代码

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

typedef struct LNode{
    int data;
    int next;
}LNode;

//创建备用链表
void reserveArr(LNode *array){
    for(int i=0;i<maxSize;i++){
        array[i].next=i+1;
        array[i].data=-1;
    }
    array[maxSize-1].next=0;
}

//提取分配空间
int mallocArr(LNode *array){
    int i=array[0].next;
    if(array[0].next){
        array[0].next=array[i].next;
    }
    return i;
}

//创建静态链表
int initArr(LNode *array){
    reserveArr(array);
    int body=mallocArr(array);
    int tempBody=body;
    int length;
    cin>>length;
    for(int i=1;i<length+1;i++){
        int j=mallocArr(array);
        array[tempBody].next=j;
        int temp;
        cin>>temp;
        array[j].data=temp;
        tempBody=j;
    }
    array[tempBody].next=0;
    return body;
}

//备用链表回收空间的函数,其中array为存储数据的数组,k表示未使用节点所在数组的下标
void freeArr(LNode * array,int k){
    array[k].next=array[0].next;
    array[0].next=k;
}
//删除结点函数,a 表示被删除结点中数据域存放的数据
void deletArr(LNode * array,int body,char a){
    int tempBody=body;
    //找到被删除结点的位置
    while (array[tempBody].data!=a) {
        tempBody=array[tempBody].next;
        //当tempBody为0时,表示链表遍历结束,说明链表中没有存储该数据的结点
        if (tempBody==0) {
            printf("链表中没有此数据");
            return;
        }
    }
    //运行到此,证明有该结点
    int del=tempBody;
    tempBody=body;
    //找到该结点的上一个结点,做删除操作
    while (array[tempBody].next!=del) {
        tempBody=array[tempBody].next;
    }
    //将被删除结点的游标直接给被删除结点的上一个结点
    array[tempBody].next=array[del].next;
    //回收被摘除节点的空间
    freeArr(array, del);
}

//插入数据
void insertArr(LNode * array,int body,int add,char a){
    int tempBody=body;//tempBody做遍历结构体数组使用
    //找到要插入位置的上一个结点在数组中的位置
    for (int i=1; i<add; i++) {
        tempBody=array[tempBody].next;
    }
    int insert=mallocArr(array);//申请空间,准备插入
    array[insert].data=a;
    array[insert].next=array[tempBody].next;//新插入结点的游标等于其直接前驱结点的游标
    array[tempBody].next=insert;//直接前驱结点的游标等于新插入结点所在数组中的下标
}

//遍历输入
void dispalyArr(LNode *array,int body){
    int tempBody=body+1;
    while(array[tempBody].next){
        cout<<array[tempBody].data<<" ";
        tempBody=array[tempBody].next;
    }
    cout<<array[tempBody].data<<" ";
}

int main(){
    LNode array[maxSize];
    int body=initArr(array);
    dispalyArr(array,body);
    cout<<endl;
    insertArr(array,body,2,10);
    dispalyArr(array,body);
    cout<<endl;
    deletArr(array,body,10);
    dispalyArr(array,body);
    return 0;
}

在这里插入图片描述

6.查询数据

//插入数据
void insertArr(LNode * array,int body,int add,char a){
    int tempBody=body;//tempBody做遍历结构体数组使用
    //找到要插入位置的上一个结点在数组中的位置
    for (int i=1; i<add; i++) {
        tempBody=array[tempBody].next;
    }
    int insert=mallocArr(array);//申请空间,准备插入
    array[insert].data=a;
    array[insert].next=array[tempBody].next;//新插入结点的游标等于其直接前驱结点的游标
    array[tempBody].next=insert;//直接前驱结点的游标等于新插入结点所在数组中的下标
}

在这里插入图片描述

学习参考博客地址:http://c.biancheng.net/view/3340.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值