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

12. 测试

  1. int main() {
  2. StaticLinkList<int> sll;
  3. sll.Init();
  4. int e;
  5. for ( int i = 1; i < 10; i++ )
  6. sll.Insert( i, i );
  7. for (int i = 1; i < 10; i++) {
  8. sll.GetElement(i, e);
  9. cout << e << endl;
  10. }
  11. sll.Traverse();
  12. sll.Delete( 3 );
  13. sll.Traverse();
  14. cout << "List Length : " << sll.Length() << endl;
  15.  
  16. return 0;
  17. }

静态链表 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. go mod 拉取私有仓库

    前言 如果代码中依赖了本地的包, 这个包是托管在内网 Gitlab 中, 而且不是 HTTPS 服务,那么应该怎样使用 go mod 拉取代码呢? 本文会给你我的答案 正文 首先我们要知道, 如果本地 ...

  2. LeetCode226 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题  ...

  3. 【Java】标识符

    一.标识符 文章目录 一.标识符 1.标识符的命名规则 2.关键字.保留字.特殊值 3.code Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符.简单的说,凡是程序员自己命名的部分 ...

  4. 【ORA】ORA-00030: User session ID does not exist.

    今天巡检,查询锁相关的情况的时候,确认业务后,准备将锁干掉,但是干掉的时候报了一个错误,ORA-00030 发现回话不存在,我以为pmon进程已经将锁进程kill掉了,就再次查看,发现,还是存在 这个 ...

  5. [Usaco2005 Mar]Out of Hay 干草危机

    题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,000)条双向道路,所有 ...

  6. 5V充8.4V,5V升压8.4V给电池充电的芯片电路

    5V充8.4V的锂电池,需要把USB口的5V输入,升压转换成8.4V来给两串电池充电. 5V升压8.4V给锂电池充电的专门充电IC 集成了5V升压8.4V电路和充电管理电路的PL7501C 如果不需要 ...

  7. linux自定义安装位置安装jdk

    注:本文系参考网络内容及本人实践得出 1 下载jdk安装包 下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downl ...

  8. css-前端实现左中右三栏布局的常用方法:绝对定位,圣杯,双飞翼,flex,table-cell,网格布局等

    1.前言 作为一个前端开发人员,工作学习中经常会遇到快速构建网页布局的情况,这篇我整理了一下我知道的一些方法.我也是第一次总结,包括圣杯布局,双飞翼布局,table-cell布局都是第一次听说,可能会 ...

  9. centos&linux

    who am i 查看是哪一个用户 init 0关机 ifconfig用于配置网络或显示当前网络接口的状态 eth0是网卡的名字 第一行:flags后面的up指的是网卡处于运行状态,running连接 ...

  10. 列出HBASE所有表的相关信息,如表名、创建时间等。

    import java.io.IOException; import java.util.Collection; import java.util.Iterator; import org.apach ...