数据结构:线性表之静态链表

本文介绍了一种通过结构体数组模拟单链表的方法——静态链表,并提供了完整的代码实现。重点展示了插入、删除等操作的优势及局限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

静态链表,也是线性表的一种表现形式之一,本篇文章中仅仅简单展示如何简单实现其基本功能及简单的测试调试。
静态链表:利用结构体数组模拟单链表,其中,结构体中的next为int型,指向数组的某一个下标,从而实现模拟单链表
特点:设置空闲链(模拟单链表从内存中申请空间)使得未被使用的数组下标可以被寻址,写入新数据,有数据释放时,空间归还到空闲链。
亮点:相对于顺序表来说,插入和删除时无需移动大量元素
缺点 :同样没有解决跟顺序表一样的可拓展性不强的特点(不确定表长时容易不知何时会表满,溢出)
上代码:

结构体实现:
#pragma once
#include<iostream>
using namespace std;

const int MaxSize = 100;     //自定义表长
template<typename T>
class Node {
public:
    T data;
    int next;      //存储指向下一个节点的数组的下标
};
//单链表类模板的实现:
#pragma once
#include"Node.h"


template<typename T>
class staticLink {
public:
    staticLink(T a[],int n);
    ~staticLink();
    int Length();         //返回单链表的长度
    T Get(int i);           //按位查找,查找第i个节点的元素
    int Locate(T x);        //按值查找,查找链表中第一个值为x的元素,并返回序号
    bool Insert(int i, T x);   //插入元素,在第i个位置插入值x
    bool Delete(int i);       //删除节点,删除第i个节点
    bool InsertHead(T x);    //头插法插入节点
    bool InsertTail(T x);    //尾插法插入节点
    void ListTraverse();      //遍历节点
private:
    int first;
    int avail;
    int m_Length;
    Node<T> SList[MaxSize];
};
template<typename T>
staticLink<T>::staticLink(T a[],int n) {
    for (int i = 0; i < MaxSize; i++)
    {
        SList[i].next = i + 1;
    }
    m_Length = 0;
    SList[MaxSize - 1].next = -1;
    avail = 2;
    first = 1;
    SList[first].next = -1;
    //利用头插法插入元素
    for (int j = 0; j < n;j++) {
        //判断链中是否已满
        if (avail==-1) {   
            break;
        }
        int s = avail;
        //空链后移
        avail = SList[avail].next;
        //新链上值
        SList[s].data = a[j];
        //上链
        SList[s].next = SList[first].next;
        SList[first].next = s;
        m_Length++;
    }
}
template<typename T>
staticLink<T>::~staticLink() {

}
template<typename T>
int staticLink<T>::Length() {
    return m_Length;
}
template<typename T>
T staticLink<T>::Get(int i) {
    if (i <= 0 || i > m_Length) {
        throw"";
    }
    int s = first;
    for (int j = 0; j < i; j++) {
        s = SList[s].next;
    }
    return SList[s].data;
}
template<typename T>
int staticLink<T>::Locate(T x) {
    int count = 0;
    int s = first;
    while (count<m_Length&&SList[s].next!=-1)
    {
        if (SList[s].data == x) {
            return count;
        }
        count++;
        s = SList[s].next;
    }
    return -1;
}
template<typename T>
bool staticLink<T>::Insert(int i, T x) {
    if (SList[avail].next==-1) {
        return false;
    }
    int s = first;
    int temp = avail;
    SList[temp].data = x;
    //空链头针后移
    avail = SList[avail].next;
    int count=0;
    while (count < i-1 &&count < m_Length) {
        s = Slist[s].next;
        count++;
    }
    SList[temp].next = SList[s].next;
    Slist[s].next = temp;
    m_Length++;
    return true;
}
template<typename T>
bool staticLink<T>::Delete(int i) {
    if (i <= 0 || i > m_Length) {
        return false;
    }
    int count = 0;
    int s = first;
    while (count < i-1&&count < m_Length) {
        s = SList[s].next;
        count++;
    }
    int q = SList[s].next;
    SList[s].next = SList[q].next;
    SList[q].next = avail;
    avail = q;
    m_Length--;
    return true;
}
template<typename T>
bool staticLink<T>::InsertHead(T x) {
    int s = avail;
    SList[s].data = x;
    avail = SList[avail].next;
    SList[s].next = SList[first].next;
    SList[first].next = s;
    m_Length++;
    return true;
}
template<typename T>
bool staticLink<T>::InsertTail(T x) {
    int s = first;
    int temp = avail;
    SList[temp].data = x;
    avail = SList[avail].next;
    while (SList[s].next != -1) {
        s = SList[s].next;
    }
    SList[temp].next = SList[s].next;
    SList[s].next = temp;
    m_Length++;
    return true;
}
template<typename T>
void staticLink<T>::ListTraverse() {
    int s = SList[first].next;
    for (int i = 1; i <= m_Length; i++) {
        cout << SList[s].data << ",";
        s = SList[s].next;
    }
}

注意:实现代码中没有详细的注释,但是其实现方法中的思想及处理方法都与前面的顺序表及单链表相似,可以类比

//实例调用:
#include<iostream>
#include"staticLink.h"
using namespace std;

int main() {
    int a[5] = { 1,2,3,4,5 };
    staticLink<int>  MyList(a, 5);
    MyList.ListTraverse();
    cout<<endl<<"第 1个节点的元素是:"<<MyList.Get(1);
    MyList.Delete(1);
    cout << endl;
    MyList.ListTraverse();
    cout << endl;
    MyList.InsertHead(6);
    MyList.InsertTail(5);
    MyList.ListTraverse();
    cout <<endl<< "链表的长度为:" << MyList.Length();
    cout << endl << "元素6所在的位置为:" << MyList.Locate(6);
    return 0;
}

实例调用结果
这里是基本实现,具体的使用技巧应该到具体的问题中体现出来。
以上。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值