静态链表
静态链表是将线性表和链表糅合在一起的数据结构,相比线性表,它插入速度更快,相比链表,它的数据密度更大,因为它的节点是存储在数组中的。
虽然我不知道静态链表什么时候用,但学了总有好处
前言:
1.老师仅仅布置了静态链表的初始化、插入和删除操作,因此我只做这几个。绝对不是因为懒!
2.本文使用cpp完成。
思路:
首先定义节点结构体Component,然后在main函数中定义一个Component数组,它的长度为1000;
Component中有两个元素,一个是DataType,这里我使用了string,用来表示存储的元素,另一个是cursor,它的作用与链表节点中的指针一样,用来指出下一个元素的位置
不同的是,cursor指出的不再是地址,而是下一个元素在数组中的下标。
由此可以看出,静态链表本质还是链表,我们仍需用出来链表的方式处理静态链表,不能被数组所迷惑。
下面开始实现。
实现:
静态链表,归根结底还是链表,因此首先我们要定义节点。
#define MAXSIZE 1000 //数组最大长度
struct Component
{
string data = "" ; //数据
int cursor = 0; //游标
};
在main函数中,我们定义静态链表
int main()
{
Component StaticLinkedList[MAXSIZE]; //静态链表
}
接下来,我们需要进行初始化
void initLinkedList(Component* list)
{
int cnt = 1;
for ( int i = 0; i < MAXSIZE - 1; i++)
{
list[i].cursor = i++; //初始化时每个节点的cursor指出
} //下一个节点的位置,而最后一个
list[MAXSIZE - 1].cursor = 0; //节点的cursor为0,指向头节点
}
无论插入还是删除,我们都需要找到元素。
Component& find(string input, Component* list)
{ //返回component的引用,这样可以修改数组中的元素,而无需使用指针
int curEle = list[0].cursor;
while (list[curEle].cursor != 0 && list[curEle].data != "" ) //初始化时每个元素都为空,因此以此为条件
{
if (list[curEle].data == input)
{
return list[curEle];
}
else
{
curEle = list[curEle].cursor;
}
}
return list[curEle]; //当找不到时返回的元素
}
当我们插入元素时,我们需要将最后一个非空元素后面的那一个元素使用,将数据保存在那个元素里。
那么怎么知道最后一个元素呢?为此我定义了一个静态变量currentLength;
表示链表非空元素的长度,在插入之前先让其加一,即可获得需要的那个元素。
这里是将元素出入指定元素之后
void insertAfter(string input, string query, Component* list)
{ //与链表插入几乎一样的手法,无需多言
currenLength++;
Component& target = find(query, list);
list[currenLength].data = input;
list[currenLength].cursor = target.cursor;
target.cursor = currenLength;
}
删除元素
void deleteElement(string query, Component* list)
{
if (query == "" )
{
cout << "you didn't input anything." ;
return ;
}
Component& target = find(query, list);
if (target.data != query)
{
cout << "the element you input does not exist in the list." ;
return ;
}
int curEle = 0;
int pre;
while (1) //需要寻找指定元素之前的元素
{
if (list[curEle].data == query)
{
list[pre].cursor = list[curEle].cursor;
list[curEle].cursor = -1;
list[curEle].data = "" ;
break ;
}
else
{
pre = curEle;
curEle = list[curEle].cursor;
}
}
}
完整代码:
#include
#include
using namespace std;
#define MAXSIZE 1000
struct Component
{
string data = "" ;
int cursor = 0;
};
static int currenLength = 0;
Component& find(string input, Component* list)
{
int curEle = list[0].cursor;
while (list[curEle].cursor != 0 && list[curEle].data != "" )
{
if (list[curEle].data == input)
{
return list[curEle];
}
else
{
curEle = list[curEle].cursor;
}
}
return list[curEle];
}
void insertAfter(string input, string query, Component* list)
{
currenLength++;
Component& target = find(query, list);
list[currenLength].data = input;
list[currenLength].cursor = target.cursor;
target.cursor = currenLength;
}
void deleteElement(string query, Component* list)
{
if (query == "" )
{
cout << "you didn't input anything." ;
return ;
}
Component& target = find(query, list);
if (target.data != query)
{
cout << "the element you input does not exist in the list." ;
return ;
}
int curEle = 0;
int pre;
while (1)
{
if (list[curEle].data == query)
{
list[pre].cursor = list[curEle].cursor;
list[curEle].cursor = -1;
list[curEle].data = "" ;
break ;
}
else
{
pre = curEle;
curEle = list[curEle].cursor;
}
}
}
void initLinkedList(Component* list)
{
int cnt = 1;
for ( int i = 0; i < MAXSIZE - 1; i++)
{
list[i].cursor = i++;
}
list[MAXSIZE - 1].cursor = 0;
}
int main()
{
Component StaticLinkedList[MAXSIZE];
initLinkedList(StaticLinkedList);
string input, query, deleteMent;
while (1) {
cin >> input >> query >> deleteMent;
insertAfter(input, query, StaticLinkedList);
deleteElement(deleteMent, StaticLinkedList);
}
}
结语:
这就是静态链表这几个操作了,我经过VS调试后并未出现异常
如有看不懂,或错误的地方,欢迎讨论。

浙公网安备 33010602011771号