//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实现静态链表,要求包含插入,删除,和查找功能的更多相关文章

  1. 跟我一起学STL(2)——vector容器详解

    一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...

  2. STL容器vector应用注意事项

    [1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...

  3. STL之vector常用函数笔记

    STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...

  4. C++的STL中vector内存分配方法的简单探索

    STL中vector什么时候会自动分配内存,又是怎么分配的呢? 环境:Linux  CentOS 5.2 1.代码 #include <vector> #include <stdio ...

  5. C++ STL中vector(向量容器)使用简单介绍

    原文:http://www.seacha.com/article.php/knowledge/cbase/2013/0903/2205.html C++ vector(向量容器)是一个线性顺序结构.相 ...

  6. ###STL学习--vector

    点击查看Evernote原文. #@author: gr #@date: 2014-08-11 #@email: forgerui@gmail.com vector的相关问题.<stl学习> ...

  7. STL中vector,Map,Set的实现原理

    vector的数据安排以及操作方式,与array非常类似,两者唯一的区别是空间运用的灵活性,array是静态空间,一旦配置了就不能改变,如果你想要大一点的空间,就必须首先配置一块新空间,然后将原来的元 ...

  8. STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase

    今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0         这个程序使用了vect ...

  9. 带你深入理解STL之Vector容器

    C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...

随机推荐

  1. 求n的阶乘!

    编写一个computer类,类中含有一个求n的阶乘的方法,将该类打包, 在另一个包中引入包,在主类中定义computer类的对象,调用求n的阶乘的方法,并输出结果 结果

  2. centos7删除MariaDB怎么操作

    有时我们要在centos上安装一些组件,需要先把原来的数据库删除,比如MariaDB,不然就出现冲突错误,那么如何删除数据库呢?首先查询所安装的MariaDB组件: [root@localhost l ...

  3. <Math> 29 365

    29. Divide Two Integers class Solution { public int divide(int dividend, int divisor) { if(dividend ...

  4. 完美解决MacOS catalina 升级后Vmware黑屏的问题

    完美解决MacOS catalina 升级后VMware黑屏 1.关闭MacOS的rootless机制 #Rootless机制将成为对抗恶意程序的最后防线 1.尝试关闭Rootless,重启按住 Co ...

  5. ACR122U读卡器在win7以上系统使用过程中的设置项

    发现ACR122U这个读卡器在进行nested破解的时候总是卡死,换了N个驱动程序都不行. 后发现是windows系统因智能卡的即插即用设置导致的问题,可以通过组策略的设置搞定. gpedit.msc ...

  6. Python爬取酷狗飙升榜前十首(100)首,写入CSV文件

    酷狗飙升榜,写入CSV文件 爬取酷狗音乐飙升榜的前十首歌名.歌手.时间,是一个很好的爬取网页内容的例子,对爬虫不熟悉的读者可以根据这个例子熟悉爬虫是如何爬取网页内容的. 需要用到的库:requests ...

  7. JavaFx出现错误Caused by: java.lang.NullPointerException: Location is required的解决方法

    问题截图: "C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" "-javaagent:I:\IntelliJ IDEA ...

  8. 敏捷软件开发_设计原则<三>

    敏捷软件开发_设计原则 单一职责原则(single responsibilities principle,SRP) 原理:一个类应该只有一个变化 分离职责:如果不耦合的职责那么很简单,如果两个职责耦合 ...

  9. .NET MVC5简介(二)

    MVCApplication---Application_Statr--RegisterRoutes--给RouteCollection添加规则,请求进到网站---X----请求地址被路由按照顺序匹配 ...

  10. JavaScript HTML DOM Style flexWrap 属性

    flexWrap 属性 flexWrap属性指定flex项是否应该换行. 注意:如果元素不是flex项,则flexWrap属性不起作用. 如果必要,使flex换行: document.getEleme ...