Static Link List 静态链表
参考:
 
开辟一块数组空间用来在非指针的程序中维护数组链表。
定义:
SLinkList共有MAX个元素,节点是有数据域Data和游标域Cur组成
其中第一个元素节点SLinkList[0]用来记录备用链表的头节点。最后一个节点SLinkList[maxn-1]用来记录链表的头结点。
如果链表头结点的Cur为0,则链表为空。
备用链表节点即是未使用的节点空间。
 
1.Node节点定义
/*静态链表节点元素*/
template < class ElemType >
class Node {
public :
ElemType data ;
int cur ;
};
2.静态链表定义
/*静态链表*/
template < class ElemType >
class StaticLinkList {
public :
void Init (); //初始化链表
bool Insert ( int pos, ElemType e ); //在链表的第pos个位置插入元素
bool Insert ( ElemType e ); //在链表的末尾插入元素
bool Delete ( int pos ); //删除第pos个位置的元素
int Length (); //获取链表的长度
bool Empty (); //判断链表是否为空
void Traverse (); //遍历链表
bool GetElement ( int pos, ElemType & e ); //获取第pos位置的元素
private :
//链表
Node <ElemType > SLinkList [ maxn];
int len ;
//处理备用链表用,分配和回收节点
int Malloc (); //分配备用链表中的一个空位置
void Free ( int pos ); //将位置为pos的空闲节点释放
};
3.初始化链表,链表头结点为空,其余元素连接组成备用链表。
/*初始化静态链表*/
template < class ElemType >
void StaticLinkList < ElemType>:: Init () {
cout << "----Init the Link List----" << endl; //初始化备用链表
for ( int i = 0; i < maxn - 1 ; i ++ )
SLinkList [i ]. cur = i + 1 ; SLinkList [maxn - 2 ].cur = 0 ; //作为备用链表的末尾保护,指向0空 或 可以认为指向循环备用链表的头结点
SLinkList [maxn - 1 ].cur = 0 ; //链表头结点为空
this ->len = 0 ; //链表长度为0
}
4.插入元素
4.1在pos位置插入元素
/*在第Pos个位置插入元素*/
template < class ElemType >
bool StaticLinkList < ElemType>:: Insert ( int pos , ElemType e ) {
cout << "----Insert Element in pos---- -> " << pos << " : " << e << endl; if ( pos < 1 || pos > this-> Length () + 1 )
cout << "----Failed : Input Pos Error----" << endl ; int cur_idx = maxn - 1;
int mac_idx = this ->Malloc (); if ( mac_idx != 0 ) {
SLinkList [mac_idx ]. data = e ; //寻找第pos个元素
for ( int i = 1; i <= pos - 1 ; i ++ )
cur_idx = SLinkList[ cur_idx ].cur ; SLinkList [mac_idx ]. cur = SLinkList [cur_idx ]. cur; //第pos个元素的cur指向 cur_idx之后的元素
SLinkList [cur_idx ]. cur = mac_idx ; //cur_idx指向当前插入对象
this ->len ++;
return true ;
} else
return false ;
}
4.2 插入元素到最后
/*在链表的末尾插入元素*/
template < class ElemType >
bool StaticLinkList < ElemType>:: Insert ( ElemType e ) {
cout << "----Insert Element in back---- -> " << e << endl ; int cur_idx = maxn - 1;
int mac_idx = this ->Malloc (); if ( mac_idx != 0 ) {
SLinkList [mac_idx ]. data = e ; //寻找第pos个元素
for ( int i = 1; i <= this -> Length(); i++ )
cur_idx = SLinkList[ cur_idx ].cur ; SLinkList [mac_idx ]. cur = SLinkList [cur_idx ]. cur; //第pos个元素的cur指向 cur_idx之后的元素
SLinkList [cur_idx ]. cur = mac_idx ; //cur_idx指向当前插入对象
this ->len ++;
return true ;
} else
return false ;
}
5.删除pos位置的元素
/*删除第pos个位置的元素*/
template < class ElemType >
bool StaticLinkList < ElemType>:: Delete ( int pos ) {
cout << "----Delete List from pos---- -> " << pos << endl ; if ( pos < 1 || pos > this-> Length () ) {
cout << "----Failed : Input Pos Error----" << endl ;
return false ;
} int cur_idx = maxn - 1; for ( int i = 1; i <= pos - 1 ; i ++ )
cur_idx = SLinkList[ cur_idx ].cur ; int aim_idx = SLinkList [cur_idx ]. cur;
SLinkList [cur_idx ]. cur = SLinkList [ pos]. cur ;
this ->len --;
Free ( aim_idx );
return true ;
}
6.获取链表的长度
/*获取静态链表的长度*/
template < class ElemType >
int StaticLinkList < ElemType>:: Length () {
//cout << "----Get Link List Length----" << endl;
return len ;
/*
int cur_idx = SLinkList[maxn - 1].cur;
int cnt = 0;
//统计所有元素,直到cur为0
while (cur_idx != 0) {
cur_idx = SLinkList[cur_idx].cur;
cnt++;
}
return cnt;
*/
}
7.判断链表是否为空
/*判断链表是否为空*/
template <class ElemType >
bool StaticLinkList <ElemType >:: Empty() {
return SLinkList [maxn - 1 ].cur == 0 ;
}
8.遍历链表
/*遍历静态链表*/
template < class ElemType >
void StaticLinkList < ElemType>:: Traverse () {
cout << "----Traverse Link List----" << endl;
int cur_idx = SLinkList [maxn - 1 ].cur ;
int idx = 1 ; //输出Node的数据
while ( cur_idx != 0 ) {
cout << "Node " << ( idx++ ) << " : " << SLinkList [cur_idx ]. data << endl ;
cur_idx = SLinkList[ cur_idx ].cur ;
}
}
9.获取第pos位置的元素
/*获取第pos位置的元素*/
template <class ElemType >
bool StaticLinkList <ElemType >:: GetElement( int pos , ElemType & e ) {
if ( pos < 1 || pos > this-> Length () + 1 ) {
cout << "----Failed : Input Pos Error----" << endl ;
return false ;
} int cur_idx = maxn - 1 ; for ( int i = 1; i <= pos ; i++ )
cur_idx = SLinkList [cur_idx ].cur ; e = SLinkList [cur_idx ].data ;
return true ;
}
10.分配备用链表中的一个位置用于插入元素
/*分配备用链表的一个节点*/
template <class ElemType >
int StaticLinkList <ElemType >:: Malloc() {
int cur_idx = SLinkList [0 ].cur ;
if ( cur_idx != 0 ) {
SLinkList [0 ].cur = SLinkList [cur_idx ].cur ;
return cur_idx ;
} else {
cout << "----Failed : Spare Link List is Empty----" << endl;
exit( 0);
} }
11.释放链表中的pos位置,并插入到备用链表中
/*释放pos位置空闲节点*/
template <class ElemType >
void StaticLinkList <ElemType >:: Free( int pos ) {
SLinkList [pos ].cur = SLinkList [0 ].cur ; /*把第一个元素的cur值赋给要删除的分量cur */
SLinkList [0 ].cur = pos ; /* 把要删除的分量下标赋值给第一个元素的cur */
}

12. 测试

int main() {
StaticLinkList<int> sll;
sll.Init();
int e;
for ( int i = 1; i < 10; i++ )
sll.Insert( i, i );
for (int i = 1; i < 10; i++) {
sll.GetElement(i, e);
cout << e << endl;
}
sll.Traverse();
sll.Delete( 3 );
sll.Traverse();
cout << "List Length : " << sll.Length() << endl; return 0;
}

静态链表 Static Link List的更多相关文章

  1. C#数据结构-静态链表

    对于双向链表中的节点,都包括一个向前.向后的属性器用于指向前后两个节点,对于引用类型,对象存储的是指向内存片段的内存指针,那么我们可以将其简化看作向前向后的两个指针. 现在我们将引用类型替换为值类型i ...

  2. Django基础,Day7 - 添加静态文件 static files

    添加css样式文件 1.首先在app目录下创建static文件夹,如polls/static.django会自动找到放在这里的静态文件. AppDirectoriesFinder which look ...

  3. java与数据结构(2)---java实现静态链表

    结点类 1 //结点类 2 class Node<T> { 3 private T data; 4 private int cursor; 5 6 Node(T data, int cur ...

  4. 【Java】 大话数据结构(3) 线性表之静态链表

    本文根据<大话数据结构>一书,实现了Java版的静态链表. 用数组描述的链表,称为静态链表. 数组元素由两个数据域data和cur组成:data存放数据元素:cur相当于单链表中的next ...

  5. Java数据结构-线性表之静态链表

    静态链表的定义: 节点由一个一维数组和一个指针域组成,数组用来存放数据元素,而指针域里面的指针(又称游标)用来指向下一个节点的数组下标. 这种链表称之为静态链表. 链表中的数组第一个和最后一个位置须要 ...

  6. 使用C语言描述静态链表和动态链表

    静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...

  7. 静态导入Static import

    静态导入Static import 要使用静态成员(方法和变量)我们必须给出提供这个静态成员的类. 使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名 ...

  8. 静态链表 C语言描述

    静态链表1.下标为0的游标存放最后存放数据节点的游标,即是第一个没有存放元素(备用链表)的下标2.最后一个的节点存放第一个由数值得下标3.第一个和最后一个都不存放数据 即是备用链表的第一个的下标 4. ...

  9. 03静态链表_StaticLinkList--(线性表)

    #include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...

随机推荐

  1. collection常用功能:

    collection常用功能: Collection是所有单列集合的父接口,因此在collection中定义了单列集合(List)和(Set)通用的一些方法.这些方法可用于操作所有的单列集合,方法如下 ...

  2. 用js实现打印九九乘法表

    用js在打印九九乘法表 思考 在学习了流程控制和条件判断后,我们可以利用js打印各式各样的九九乘法表 不管是打印什么样三角形九九乘法表,我们都应该找到有规律的地方,比如第一列的数字是什么规律,第一行的 ...

  3. SQL中的主键,候选键,外键,主码,外码

    1.码=超键:能够唯一标识一条记录的属性或属性集. 标识性:一个数据表的所有记录都具有不同的超键 非空性:不能为空 有些时候也把码称作"键" 2.候选键=候选码:能够唯一标识一条记 ...

  4. kali中安装漏洞靶场Vulhub

    一.什么是vulhub? Vulhub是一个基于docker和docker-compose的漏洞环境集合,进入对应目录并执行一条语句即可启动一个全新的漏洞环境,让漏洞复现变得更加简单,让安全研究者更加 ...

  5. 技术实践丨React Native 项目 Web 端同构

    摘要:尽管 React Native 已经进入开源的第 6 个年头,距离发布 1.0 版本依旧是遥遥无期."Learn once, write anywhere",完全不影响 Re ...

  6. +load和+initialize方法调用时机

    一.+load方法什么时候调用 +load方法会在runtime加载类.分类时调用(程序运行起来会先去加载调用+load 跟你引用没有引用其头文件没有关系).每个类.分类的+load,在程序运行过程中 ...

  7. SAP内表类型及其数据读取效率评估

    内表大概分3种: 1.标准表standard tables:如果不指定BINARY SEARCH附加选项,则默认为线性查找(linear search),既一条一条的查找. 2.排序表(sorted ...

  8. 【Android】关于连续多次点击控件的控制方案(新建监听类)

    参考:防止Android过快点击造成多次事件的三种方法_胖胖的博客-CSDN博客 实现逻辑很简单: 设置限定时间 在用户点击时开始计时 若计时未超过限定时间,则不允许触发点击事件 因还未学习过Rxja ...

  9. cisco思科交换机终端远程ssh另一端报错:% ssh connections not permitted from this terminal

    故障现象: XSJ-GH10-C3750->ssh 58.64.xx.xx% ssh connections not permitted from this terminal 解决办法: 原因: ...

  10. zabbix指定版本自动化安装脚本shell

    安装服务端zabbix 有时候要部署一个zabbix各种配置啊贼烦. #!/bin/sh #sleep 10 zabbix_version=4.2.5 ###这里你自定义版本,我要的是4.2.5 za ...