//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. LeetCode 1244. 力扣排行榜

    地址 https://www.acwing.com/solution/LeetCode/content/5765/ 题目描述新一轮的「力扣杯」编程大赛即将启动,为了动态显示参赛者的得分数据,需要设计一 ...

  2. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题

    C. Dominated Subarray Let's call an array

  3. MySQL多表查询综合练习答案

    目录 一.综合练习 1.1 init.sql文件内容 1.2 从init.sql文件中导入数据 1.3 基础练习 1.4 进阶练习 二.基础练习答案 三.进阶练习答案 一.综合练习 1.1 init. ...

  4. ubuntu 16.04上源码编译和安装cgal并编写CMakeLists.txt | compile and install cgal on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/39ab7ed9/,欢迎阅读最新内容! compile and install cgal on ubuntu 16.04 Guide ...

  5. selectpage选择订单的时候,订单数量和金额会动态改变

    1. 2. HTML部分: JS: PHP获取数据并return json

  6. Percona XtraDB Cluster简易入门 - 安装篇

    说明 Percona XtraDB Cluster(简称PXC),是由percona公司推出的mysql集群解决方案.特点是每个节点都能进行读写,且都保存全量的数据.也就是说在任何一个节点进行写入操作 ...

  7. Spring Cloud Gateway-自定义异常处理

    前提 我们平时在用SpringMVC的时候,只要是经过DispatcherServlet处理的请求,可以通过@ControllerAdvice和@ExceptionHandler自定义不同类型异常的处 ...

  8. SpringBoot2使用Jetty容器(替换默认Tomcat)

    https://blog.csdn.net/hanchao5272/article/details/99649252   Jetty和tomcat的比较 Tomcat和Jetty都是一种Servlet ...

  9. wpf 当DataGrid列模版是ComboBox时,显示信息

    ​ 实际工作中,有时DataGrid控件某一列显示数据是从Enum集合里面选择出来的,那这时候设置列模版为ComboBox就能满足需求.而关于显示的实际内容,直接是Enum的string()返回值可能 ...

  10. C#比较两个对象中的指定字段值是否相等

    一.创建CompareFieldAttribute标识要比较的字段 using System; namespace CompareObjField { /// <summary> /// ...