您的位置:首页 > 理论基础 > 数据结构算法

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

2017-10-01 16:03 501 查看
静态链表,也是线性表的一种表现形式之一,本篇文章中仅仅简单展示如何简单实现其基本功能及简单的测试调试。

静态链表:利用结构体数组模拟单链表,其中,结构体中的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;
}




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

以上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: