基于C++的STL的vector实现静态链表,要求包含插入,删除,和查找功能
2024-09-06 16:15:12
//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;
}
最新文章
- T-SQL 将动态SQL的结果集赋值到变量
- x01.os.20: compile linux-0.11 on the ubuntu
- iOS:基于CoreText的排版引擎
- 给Linux装图形化界面
- VA中用文件头注释和函数头注释Suggestions
- 教你如何精通Struts:Tiles框架
- 12)Java Constructor
- 259. 3Sum Smaller
- Hadoop 配置好hive,第一次在conf能进入,第二次就不行了,怎么办?
- cf493D Vasya and Chess
- 关于局域网内IIS部署网站,本机可访问,而网内其他用户无法访问问题的解决方法
- boost库使用:vs2013下boost::container::vector编译出错解决
- Android读取JSON格式数据
- 201521123110 java课程设计
- 机器学习基石10-Logistic Regression
- MongoDB及Mongoose的记录
- Tomcat 中 jsp 中文乱码显示处理解决方案
- 20162322 朱娅霖 作业011 Hash
- css- 范围选择
- 装饰器(Decorator)模式
热门文章
- deepin系统右键刷新-解决增删改文件没有变化
- LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands
- js如何手写一个new
- 如何将Excel表批量赋值到ArcGIS属性表
- Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论
- Java类加载机制以及双亲委派模型
- #3144. 「APIO 2019」奇怪装置
- MongoDB自学------(5)MongoDB分片
- IT从业者不可不知的三条定律
- SQL server已经设置为单用户模式,还是无法做分离、属性设置等操作