《数据结构、算法及应用》9.(C++实施订单)


9. C++实现的线性表--顺序表
- //
- // LinearList.h
- // LinearList
- //
- // Created by cc on 14-8-21.
- // Copyright (c) 2014年 cc. All rights reserved.
- // 顺序表
- #ifndef __LinearList__LinearList__
- #define __LinearList__LinearList__
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- template <class T>
- class LinearList {
- private:
- //顺序表长度
- int m_length;
- //顺序表最大的长度
- int m_maxSize;
- //元素数组
- T* m_element;
- public:
- LinearList(int maxListSize);
- virtual ~LinearList();
- /**
- * @brief 顺序表是否为空
- *
- * @return true: 空 false: 非空
- */
- bool isEmpty() const;
- /**
- * @brief 获取顺序表的长度
- *
- * @return 顺序表的长度
- */
- int length() const;
- /**
- * @brief 获取顺序表最大容量
- *
- * @return 顺序表的最大容量
- */
- int maxSize() const;
- /**
- * @brief 在顺序表中查找第k个元素。并返回第k个元素至x中
- *
- * @param k 第k个元素
- * @param x 保存第k个元素
- *
- * @return true: 找到了第k个元素 false: 没找到第k个元素
- */
- bool find(int k, T& x) const;
- /**
- * @brief 查找顺序表中的元素x, 而且返回x所在位置
- *
- * @param x 元素x
- *
- * @return 元素x的在顺序表中的位置
- */
- int search(const T& x) const;
- /**
- * @brief 删除第k个元素并将它返回至x中
- *
- * @param k 第k个元素
- * @param x 保存第k个元素
- *
- * @return 改动后的顺序表
- */
- LinearList<T>& deleteElement(int k, T& x);
- /**
- * @brief 在第k个元素之后插入x元素
- *
- * @param k 第k个元素
- * @param x 元素x
- *
- * @return 改动后的顺序表
- */
- LinearList<T>& insertElement(int k, const T& x);
- /**
- * @brief 输出信息
- *
- * @param out 输出流
- */
- void output(ostream& out) const;
- };
- template <class T>
- LinearList<T>::LinearList(int maxListSize):m_length(0), m_element(NULL) {
- this->m_maxSize = maxListSize;
- this->m_element = new T[this->m_maxSize];
- }
- template <class T>
- LinearList<T>::~LinearList() {
- delete [] this->m_element;
- this->m_element = NULL;
- }
- /**
- * @brief 顺序表是否为空
- *
- * @return true: 空 false: 非空
- */
- template <class T>
- bool LinearList<T>::isEmpty() const {
- return this->m_length == 0;
- }
- /**
- * @brief 获取顺序表的长度
- *
- * @return 顺序表的长度
- */
- template <class T>
- int LinearList<T>::length() const {
- return this->m_length;
- }
- /**
- * @brief 获取顺序表最大容量
- *
- * @return 顺序表的最大容量
- */
- template <class T>
- int LinearList<T>::maxSize() const {
- return this->m_maxSize;
- }
- /**
- * @brief 在顺序表中查找第k个元素,并返回第k个元素至x中
- *
- * @param k 第k个元素
- * @param x 保存第k个元素
- *
- * @return true: 找到了第k个元素 false: 没找到第k个元素
- */
- template <class T>
- bool LinearList<T>::find(int k, T& x) const {
- if (k > this->m_length - 1
- || k < 0) {
- cout << "在顺序表中为找到第" << k << "个位置上的元素";
- return false;
- }
- x = m_element[k];
- return true;
- }
- /**
- * @brief 查找顺序表中的元素x, 而且返回x所在位置(位置是下标+1)
- *
- * @param x 元素x
- *
- * @return 元素x的在顺序表中的位置
- */
- template <class T>
- int LinearList<T>::search(const T& x) const {
- for (int i = 0; i < this->m_length; i++) {
- if (this->m_element[i] == x) {
- return i;
- }
- }
- return 0;
- }
- /**
- * @brief 删除第k个元素并将它返回至x中
- *
- * @param k 第k个元素
- * @param x 保存第k个元素
- *
- * @return 改动后的顺序表
- */
- template <class T>
- LinearList<T>& LinearList<T>::deleteElement(int k, T& x) {
- if (find(k, x)) {
- //找到了第k个元素
- for (int i = k; i < this->m_length - 1; i++) {
- this->m_element[i] = this->m_element[i + 1];
- }
- this->m_length--;
- } else {
- // throws exception
- cout << "未找到第" << k << "个元素";
- }
- return *this;
- }
- /**
- * @brief 在第k个元素之后插入x元素
- *
- * @param k 第k个元素
- * @param x 元素x
- *
- * @return 改动后的顺序表
- */
- template <class T>
- LinearList<T>& LinearList<T>::insertElement(int k, const T& x) {
- if (k > m_maxSize - 1
- || k < 0) {
- cout << "数组下标越界!" << endl;
- // throws OutOfBoundsException
- } else if (this->m_length == this->m_maxSize) {
- cout << "已达到数组最大容量。申请内存后再加入!" << endl;
- // throws NoMemException
- } else {
- //找到了第k个元素
- for (int i = this->m_length - 1; i > k; i--) {
- this->m_element[i] = this->m_element[i - 1];
- }
- this->m_length++;
- this->m_element[k] = x;
- }
- return *this;
- }
- template<class T>
- void LinearList<T>::output(ostream& out) const {
- for (int i = 0; i < this->m_length; i++)
- out << this->m_element[i] << " ";
- }
- // overload
- template <class T>
- ostream& operator<<(ostream& out, const LinearList<T>& x){
- x.output(out); return out;
- }
- #endif /* defined(__LinearList__LinearList__) */
- //
- // main.cpp
- // LinnerList
- //
- // Created by ChengChao on 14-8-29.
- // Copyright (c) 2014年 cc. All rights reserved.
- //
- #include <iostream>
- #include <stdlib.h>
- #include "LinearList.h"
- int main(int argc, const char * argv[]) {
- LinearList<int> list(10);
- list.insertElement(0, 1000).insertElement(1, 312).insertElement(2, 134);
- cout << "The list is" << endl;
- cout << list << endl;
- //顺序表是否为空
- cout << "顺序表是否为空:" << boolalpha << list.isEmpty() << endl;
- cout << "顺序表的最大容量:" << list.maxSize() << endl;
- int res = 0;
- int res2 = 0;
- //查找99
- cout << "查找第0个元素输入到res变量中。是否找到: " << boolalpha << list.find(0, res) << "。res=" << res << endl;
- //查找312
- cout << "查找第5个元素输入到res变量中。是否找到: " << boolalpha << list.find(5, res2) << ",res=" << res2 << endl;
- //删除第1个位置上的元素元素
- list.deleteElement(1, res);
- cout << list << endl;
- //查找元素134
- cout << "搜索元素134,位置为:" << list.search(134) << endl;
- return 0;
- }
版权声明:本文博主原创文章,博客,未经同意不得转载。
《数据结构、算法及应用》9.(C++实施订单)的更多相关文章
- 初转java随感(一)程序=数据结构+算法
大学刚学编程的时候,有一句很经典的话程序=数据结构+算法 今天有了进一步认识. 场景: 1.当前局面 (1)有现成的封装好的分页组件 返回结果是page.类型为:Page.包括 page 分页信息,d ...
- 数据结构算法集---C++语言实现
//数据结构算法集---C++语言实现 //各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点) /////////////////////////// // // // 堆栈数据结构 s ...
- 数据结构+算法面试100题~~~摘自CSDN
数据结构+算法面试100题~~~摘自CSDN,作者July 1.把二元查找树转变成排序的双向链表(树) 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调 ...
- day40 数据结构-算法(二)
什么是数据结构? 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表.集合与字典等都是一种数据结构. N.Wirth: “程序=数据结构+算法” 列表 列表:在其他编程语言中称 ...
- 前端要不要学数据结构&算法
我们都知道前端开发工程师更多偏向 DOM 渲染和 DOM 交互操作,随之 Node 的推广前端工程师也可以完成服务端开发.对于服务端开发而言大家都觉得数据结构和算法是基础,非学不可.所以正在进行 No ...
- C语言版数据结构算法
C语言版数据结构算法 C语言数据结构具体算法 https://pan.baidu.com/s/19oLoEVqV1I4UxW7D7SlwnQ C语言数据结构演示软件 https://pan.baidu ...
- 【数据结构与算法】多种语言(VB、C、C#、JavaScript)系列数据结构算法经典案例教程合集目录
目录 1. 专栏简介 2. 专栏地址 3. 专栏目录 1. 专栏简介 2. 专栏地址 「 刘一哥与GIS的故事 」之<数据结构与算法> 3. 专栏目录 [经典回放]多种语言系列数据结构算法 ...
- 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值
一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...
- 数据结构算法C语言实现(六)---2.4一元多项式的表示及相加
一.简述 利用链表表示稀疏多项式,并基于之前的一些操作(编程实现上还是有所不同的)组合新的操作实现一元多项式的表示及相加. 二.ADT 抽象数据类型一元多项式的定义 ADT Polyomail{ 数据 ...
随机推荐
- 使用HtmlAgilityPack批量抓取网页数据
原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页 Htm ...
- Android中<meta-data>的使用
在AndroidManifest.xml中.<meta-data>元素能够作为子元素,被包括在<activity>.<application> .<servi ...
- hdu5179(数位dp)
传送门:beautiful number 题意:令 A=∑ni=1ai?10n?i(1≤ai≤9)(n为A的位数).若A为“漂亮的数”当且仅当对于任意1≤i<n满足a[i]≥a[i+1]且对于任 ...
- hdu2063+hdu1083(最大匹配数)
传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...
- HDU 1016 Prime Ring Problem 题解
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- 《WordPress插件开发手冊》文件夹
翻译前言:国内没有关于WordPress插件开发比較具体而且系统的资料 前言 第一章:准备一个本地开发环境 介绍 在你的电脑上安装一个站点server 下载并配置一个本地的WordPress 创建一个 ...
- A Game of Thrones(3) - Daenerys
Her brother held the gown up for her inspection. “This is beauty. Touch it. Go on. Caress(爱抚,抚抱) the ...
- cocos2d-x2.2.3和android平台环境的搭建
准备工作:1.我只是将cocos2d-x移植到android平台,所以默认为大家已经将android平台搭建完成了(eclipse和android SDK已经配置好,java环境搭建好) 2.下载an ...
- WIFI实时监控追踪小车演示视频——安卓端、小车
文章由@超人爱因斯坦出品,转载请注明出处. 文章链接: http://hpw123.net/a/qingsongyike/yingyinqu/2014/1019/59 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...