博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线性表 - 公式化描述实现线性表
阅读量:4314 次
发布时间:2019-06-06

本文共 1801 字,大约阅读时间需要 6 分钟。

List.h

/********************************************************************	purpose:	公式化描述实现线性表	author:		xianyun1230	QQ:		836663997	e-mail:		xianyun1230@163.com	created:	2014/02/16	*********************************************************************/#include 
template
class List{ public: List(int MaxListSize = 10); ~List(); bool empty() const { return _length == 0;} int length() const { return _length;} int search(const T &x) const; bool del(int k,T &val); bool insert(int k, const T& x); void show(); T operator [](int i){return element[i];} private: int _length; T *element; int MaxSize;};template
List
::List(int MaxListSize){ MaxSize = MaxListSize; _length = 0; element = new T[MaxSize];}template
List
::~List(){ delete [] element;}template
int List
::search(const T &x) const{ for (int i = 0; i < _length && element[i] != x; ++i) continue; if (i != _length) return i-1; else return -1;}template
bool List
::del(int k, T &val){ if (k < 0 || k >= _length) return false; int n = k; val = element[n]; while(n < _length - 1) { element[n] = element[n+1]; ++n; } --_length; return true; }template
bool List
::insert(int k, const T& x){ if (k < 0 || k > _length || _length == MaxSize) return false; int n = _length - 1; while (k <= n) { element[n + 1] = element[n]; --n; } element[k] = x; ++_length; return true;}template
void List
::show(){ using namespace std; cout <<"共" <<_length <<"项:" <

main.cpp

#include "List.h"

int main()
{
List<int> link(12);
link.insert(0,21);
link.insert(0,12);
link.insert(2,34);
link.insert(1,12);
link.insert(5,13); //没有第五项,不会插入
link.show();
int a;
link.del(2,a);
link.show();
std::cout <<a;
return 0;
}

转载于:https://www.cnblogs.com/xyyh/p/3980313.html

你可能感兴趣的文章
Web测试
查看>>
模型搭建练习2_实现nn模块、optim、two_layer、dynamic_net
查看>>
使用jQuery开发datatable分页表格插件
查看>>
C语言笔记(枚举)
查看>>
coreseek安装使用
查看>>
苹果电脑提示打不开 因为它来自身份不明的开发者 不能安装下载的苹果软件解决方法...
查看>>
收发ICMP封包,实现ping
查看>>
MySql command line client 命令系列
查看>>
内置函数2
查看>>
扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
查看>>
mvc4.0添加EF4.0时发生编译时错误
查看>>
第16月第12天 CABasicAnimation 旋转加速
查看>>
Linux下查看Python安装了哪些脚本模块
查看>>
ERROR- 开发常见error
查看>>
Servlet 中文乱码问题及解决方案剖析
查看>>
OO第四次博客总结
查看>>
集合—ArrayList
查看>>
web前台设计技术
查看>>
Ubuntu14.04 在右键中添加 在终端中打开
查看>>
Eclipse代码规范工具-Checkstyle安装和使用
查看>>