基于C++的STL的vector实现静态链表,要求包含插入,删除,和查找功能
//main.cpp部分
#include"List.cpp"
int main()
{
StaticList<int> SL;
SL.Insert(,);
SL.Insert(,);
SL.Insert(,);
SL.Insert(,);
SL.Insert(, );
SL.Insert(, );
std::cout << "原始的静态链表如下:" << std::endl;
SL.show();
SL.Insert(, );
std::cout << "在静态链表中第2个位置插入100后结果如下:" << std::endl;
SL.show();
int m;
int delete_index = ;
SL.Delete(m, delete_index);
std::cout << "删除的对应" << delete_index << "处的值为:" << m << std::endl;
std::cout << "删除后的静态链表如下:" << std::endl;
SL.show(); int find_val = ;
int index = SL.Find(find_val);
std::cout << "查找到" << find_val << "在列表中的位置为:" << index << std::endl;
return ;
}
//List.h #pragma once #include<iostream>
#include<vector> const int MAXSIZE = ;
template<typename ElemType>
class StaticList; template<typename ElemType>
class Node
{
friend class StaticList<ElemType>;
private:
ElemType data;
int cur;
}; template<typename ElemType>
class StaticList
{
public: StaticList();
virtual ~StaticList();
bool Insert(const ElemType& v, int index = );
bool Delete(ElemType& v, int index = );
int Find(const ElemType v);
void show()const; private:
int NewSpace();
void DeleteSpace(int index);
bool Empty()const;
bool Full()const;
std::vector<Node<ElemType>*> StList;
int Length = ;
};
//List.cpp #include"List.h"
#include<iostream>
#include<algorithm>
template<typename ElemType>
StaticList<ElemType>::StaticList():Length()
{ for (int i = ; i < MAXSIZE - ; ++i)
{
Node<ElemType>* node = new Node<ElemType>();
StList.push_back(node);
} for (ElemType i = ; i < MAXSIZE - ; ++i)
(*StList[i]).cur = i + ;
Node<ElemType>* node = new Node<ElemType>();
StList.push_back(node);
(*StList[MAXSIZE - ]).cur = ; } template<typename ElemType>
StaticList<ElemType>::~StaticList()
{
std::for_each(StList.begin(), StList.end(), [](Node<ElemType>* node) { delete node; });
} template<typename ElemType>
bool StaticList<ElemType>::Insert(const ElemType& v, int index)
{
if (Full())
{
std::cout << "Can't insert element." << std::endl;
return false;
}
if (index< || index>Length + )
{
std::cout << "The invalid index" << std::endl;
return false;
}
int k = NewSpace();
int j = MAXSIZE - ;
if (k)
{
(*StList[k]).data = v;
for (int i = ; i <= index - ; ++i)
{
j = (*StList[j]).cur;
}
(*StList[k]).cur = (*StList[j]).cur;
(*StList[j]).cur = k;
++Length;
return true;
}
return false;
} template<typename ElemType>
bool StaticList<ElemType>::Delete(ElemType& v, int index)
{
if (Empty())
{
std::cout << "Can't delete element in an empty list!\n";
return false;
}
if (index < || index>Length)
{
std::cout << "The invalid index!\n";
return false;
}
int k = MAXSIZE - ;
int i = ;
for (; i <= index - ; ++i)
{
k = (*StList[k]).cur;
}
i = (*StList[k]).cur;
(*StList[k]).cur = (*StList[i]).cur;
v = (*StList[i]).data;
DeleteSpace(i);
--Length;
return true;
} template<typename ElemType>
int StaticList<ElemType>::Find(const ElemType v)
{
if (Empty())
{
std::cout << "Can't find value in an empty List!\n";
return -;
}
int i = ;
while ( != i && (*StList[(*StList[i]).cur]).data != v)
i = (*StList[i]).cur;
++i;
if ( == i)
{
std::cout << "Can't find the value " << v << " in the list" << std::endl;
return -;
}
return i; } template<typename ElemType>
void StaticList<ElemType>::show()const
{
if (Empty())
{
std::cout << "The List is empty" << std::endl;
return;
}
int k = (*StList[MAXSIZE - ]).cur;
std::cout << "The List is:" << std::endl;
for (int i = ; i <= Length; ++i)
{
std::cout << (*StList[k]).data << " ";
k = (*StList[k]).cur;
}
std::cout << std::endl;
} template<typename ElemType>
bool StaticList<ElemType>::Full()const
{
if (Length > MAXSIZE - )
return true;
return false;
} template<typename ElemType>
bool StaticList<ElemType>::Empty()const
{
return ( == Length);
} template<typename ElemType>
void StaticList<ElemType>::DeleteSpace(int index)
{
(*StList[index]).cur = (*StList[]).cur;
(*StList[]).cur = index;
} template<typename ElemType>
int StaticList<ElemType>::NewSpace()
{ int i = (*StList[]).cur;
if ((*StList[]).cur)
{
(*StList[]).cur = (*StList[i]).cur;
}
return i;
}
基于C++的STL的vector实现静态链表,要求包含插入,删除,和查找功能的更多相关文章
- 跟我一起学STL(2)——vector容器详解
一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...
- STL容器vector应用注意事项
[1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...
- STL之vector常用函数笔记
STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...
- C++的STL中vector内存分配方法的简单探索
STL中vector什么时候会自动分配内存,又是怎么分配的呢? 环境:Linux CentOS 5.2 1.代码 #include <vector> #include <stdio ...
- C++ STL中vector(向量容器)使用简单介绍
原文:http://www.seacha.com/article.php/knowledge/cbase/2013/0903/2205.html C++ vector(向量容器)是一个线性顺序结构.相 ...
- ###STL学习--vector
点击查看Evernote原文. #@author: gr #@date: 2014-08-11 #@email: forgerui@gmail.com vector的相关问题.<stl学习> ...
- STL中vector,Map,Set的实现原理
vector的数据安排以及操作方式,与array非常类似,两者唯一的区别是空间运用的灵活性,array是静态空间,一旦配置了就不能改变,如果你想要大一点的空间,就必须首先配置一块新空间,然后将原来的元 ...
- STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0 这个程序使用了vect ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
随机推荐
- Web-[RoarCTF 2019]Easy Calc
看看题目内容,一个计算器,并且过滤了非数字的值,查看源码,上了waf,并且在calc.php显示waf的规则 <?php error_reporting(0); if(!isset($_GET[ ...
- AcWing 77. 翻转单词顺序
习题地址 https://www.acwing.com/problem/content/description/73/ 题目描述输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 为简单 ...
- Java编译器的2点优化
优化1 对于byte/short/char三种类型来说,如果右侧赋值的数值没有超过范围,那么javac编译器将会自动隐含地为我们补上一个(byte)(short)(char). 如果没有超过左侧的范围 ...
- linux shell攻略学习笔记二
1.Cat命令 这么多命令,常用的 Cat –n file 显示文件以及行数 Cat - echo 'Text through stdin' | cat - file.txt Text throug ...
- 向技术领先的华为说No,就是对国家的通信前景说No!
历史已经证明了,任何一项可以加速人员.物资.能源.金钱.信息迁移的技术,都会让社会原有的生产力成倍地增长.中国在互联网.移动互联网保持令整个世界震惊的飞速发展,以BAT为首的诸多商业帝国建立,还有人们 ...
- 12306抢票带来的启示:看我如何用Go实现百万QPS的秒杀系统
本文为开源实验性工程:“github.com/GuoZhaoran/spikeSystem”的配套文章,原作者:“绘你一世倾城”,现为:猎豹移动php开发工程师,感谢原作者的技术分享. 1.引言 Go ...
- webpack入门配置步骤详解
1.初始化 1.npm install webpack webpack-cli webpack-dev-server --g 全局安装必要的第三方插件 2.mkdir config dist src ...
- 【shell脚本】自动磁盘分区,格式化,挂载===autoMount.sh
#!/bin/bash # 自动对磁盘分区.格式化.挂载 # 对虚拟机的 vdb 磁盘进行分区格式化,使用<<将需要的分区指令导入给程序 fdisk # n(新建分区),p(创建主分区), ...
- Spring Boot2 系列教程(二十二)整合 MyBatis 多数据源
关于多数据源的配置,前面和大伙介绍过 JdbcTemplate 多数据源配置,那个比较简单,本文来和大伙说说 MyBatis 多数据源的配置. 其实关于多数据源,我的态度还是和之前一样,复杂的就直接上 ...
- 使用celery执行Django串行异步任务
Django项目有一个耗时较长的update过程,希望在接到请求运行update过程的时候,Django应用仍能正常处理其他的请求,并且update过程要求不能并行,也不能漏掉任何一个请求 使用cel ...