基于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 ...
随机推荐
- Cent OS6.5——网络配置
1.已安装centos 系统,打开虚拟机,并开机进入centos系统 2.进行网络配置,必须先确认以下几个点: 2-1.网络适配器模式是否为NAT模式,点击虚拟机,选择设置 ——选择网络适配器,NAT ...
- leetcode 752. 打开转盘锁
地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...
- openjdk源码下载
http://hg.openjdk.java.net/jdk8u/jdk8u60/jdk/file/935758609767 browse>zip
- WPF 获取系统 DPI 的多种方法
原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题
A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...
- angular6 升级到 angular7+ 最新Ng-zorro
angular7 出来有一段时间了,然后我们项目一直用的是angular6, 看到一直再用的Ng-Zorro 更新版本了,然后就觉得把目前的项目也升级一下把. 目前我本地cli版本是6.0.8我要把他 ...
- JMS入门Demo
2.1点对点模式(邮箱) 点对点的模式主要建立在一个队列上面,当连接一个列队的时候,发送端不需要知道接收端是否正在接收,可以直接向ActiveMQ发送消息,发送的消息,将会先进入队列中,如果有接收端在 ...
- 知识图谱如何运用于RecomSys
将知识图谱作为辅助信息引入到推荐系统中可以有效地解决传统推荐系统存在的稀疏性和冷启动问题,近几年有很多研究人员在做相关的工作.目前,将知识图谱特征学习应用到推荐系统中主要通过三种方式——依次学习.联合 ...
- LinuxShell脚本——变量和数据类型
LinuxShell脚本——变量和数据类型 摘要:本文主要学习了Shell脚本中的变量和数据类型. 变量 定义变量的语法 定义变量时,变量名和变量值之间使用“=”分隔,并且等号两边不能有空格: 变量名 ...
- DataGridView中获取与设置当前选中行以及SelectedRows和CurrentRow注意区分
场景 DataGridView怎样实现添加.删除.上移.下移一行: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10281414 ...