静态链表 Static Link List

/*静态链表节点元素*/
template < class ElemType >
class Node {
public :
ElemType data ;
int cur ;
};
/*静态链表*/
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的空闲节点释放
};
/*初始化静态链表*/
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
}
/*在第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 ;
}
/*在链表的末尾插入元素*/
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 ;
}
/*删除第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 ;
}
/*获取静态链表的长度*/
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;
*/
}
/*判断链表是否为空*/
template <class ElemType >
bool StaticLinkList <ElemType >:: Empty() {
return SLinkList [maxn - 1 ].cur == 0 ;
}
/*遍历静态链表*/
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 ;
}
}
/*获取第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 ;
}
/*分配备用链表的一个节点*/
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);
} }
/*释放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的更多相关文章
- C#数据结构-静态链表
对于双向链表中的节点,都包括一个向前.向后的属性器用于指向前后两个节点,对于引用类型,对象存储的是指向内存片段的内存指针,那么我们可以将其简化看作向前向后的两个指针. 现在我们将引用类型替换为值类型i ...
- Django基础,Day7 - 添加静态文件 static files
添加css样式文件 1.首先在app目录下创建static文件夹,如polls/static.django会自动找到放在这里的静态文件. AppDirectoriesFinder which look ...
- java与数据结构(2)---java实现静态链表
结点类 1 //结点类 2 class Node<T> { 3 private T data; 4 private int cursor; 5 6 Node(T data, int cur ...
- 【Java】 大话数据结构(3) 线性表之静态链表
本文根据<大话数据结构>一书,实现了Java版的静态链表. 用数组描述的链表,称为静态链表. 数组元素由两个数据域data和cur组成:data存放数据元素:cur相当于单链表中的next ...
- Java数据结构-线性表之静态链表
静态链表的定义: 节点由一个一维数组和一个指针域组成,数组用来存放数据元素,而指针域里面的指针(又称游标)用来指向下一个节点的数组下标. 这种链表称之为静态链表. 链表中的数组第一个和最后一个位置须要 ...
- 使用C语言描述静态链表和动态链表
静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...
- 静态导入Static import
静态导入Static import 要使用静态成员(方法和变量)我们必须给出提供这个静态成员的类. 使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名 ...
- 静态链表 C语言描述
静态链表1.下标为0的游标存放最后存放数据节点的游标,即是第一个没有存放元素(备用链表)的下标2.最后一个的节点存放第一个由数值得下标3.第一个和最后一个都不存放数据 即是备用链表的第一个的下标 4. ...
- 03静态链表_StaticLinkList--(线性表)
#include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...
随机推荐
- WPF学习笔记02_布局
布局原则 WPF窗口只能包含单个元素.如果要放置多个元素,需要放置一个容器,然后在容器中添加元素. 不应显示的设定元素的尺寸 不应该使用屏幕坐标指定元素的位置 布局容器的子元素"共享&quo ...
- 初识 D3.js :打造专属可视化
一.前言 随着现在自定义可视化的需求日益增长,Highcharts.echarts等高度封装的可视化框架已经无法满足用户各种强定制性的可视化需求了,这个时候D3的无限定制的能力就脱颖而出. 如果想要通 ...
- navicat for mysql 破解版
Navicat for MySQL下载地址:Navicat for MySQL 软件和破解程序 第1步.安装Navicat软件,最后点击完成 第2步.安装成功之后还要进行破解.点击patchNavic ...
- Tomcat 配置Vue history模式
Tomcat 配置Vue history模式 近日 , 在使用 Tomcat 部署Vue项目时 , 刷新项目出现404的异常 . 原因是 Vue使用了history模式 , 而tomcat没有相关配 ...
- 在Windows中安装MongoDB--图文并茂
在Windows环境下安装MongoDB的方法 (1)下载MongoDB Windows版: 进入MongoDB官网 (2)设置数据文件和日志文件的存放目录: 打开刚刚安装MongoDB的目录咋bin ...
- 【Spring】Spring中的Bean - 4、Bean的生命周期
Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...
- 映泰主板H100系列安装win7的各种坑
自100系列主板发布以来,windows7好像就被遗弃一样,原因就在于安装win7的时候,会出现USB设备无法使用导致无法安装的问题.主要在于Win7系统没有整合USB的XHCI驱动,而100系列芯片 ...
- 查询数据库v$session时报部分多维元组字元
在查询v$session视图时,出现如下图报错,基本原因是用plsql dev时使用汉字打开新标签,导致v$session action栏位出现乱码 解决方法: select SID,SERIAL#, ...
- SAPLink 非常好用的工具
对于SAP LINK,如果你想将一个程序完整的保存到本地,包括程序的自定义屏幕.菜单等等,那么请使用这个工具,它能够将一个程序完整的保存下来,并且移植到另一个SAP系统中,用来左程序的迁移和本地保存备 ...
- IDEA安装codota插件和使用,开发人员的知心伙伴
打开IDEA 点击左上角的File之后,如下图 成功后如图所示