C++数据结构之List--线性实现
List(表)类似于队列,不同于队列的是,list可以随机读取/修改/插入某一position,通过position这一位置信息就可以直接修改相应位置的元素。实现方式和队列的类似,多了个position的参数。
注意,因为list的定义使用了模板类,所以其定义和实现需要在同一个文件下,当然,也有其他方式实现二者分离,但比较复杂,网上有方法。
代码:
List.cpp
// 特别注意:模板类不能分离编译,类实现要在同一个文件下 enum Error_code {success,underflow,overflow,range_error}; const int max_list = 10; template <class List_entry> class List { public: // methods of the List ADT List(); int size()const; bool full()const; bool empty()const; void clear(); void traverse(void(*visit)(List_entry &)); //遍历 Error_code retrieve(int position, List_entry &x)const;//察看 Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x);//移除并挂载 Error_code insert(int position, const List_entry &x);//插入 protected: // data members for a contiguous list implementation int count; List_entry entry[max_list]; }; template <class List_entry> List<List_entry>::List() { count = 0; for(int i = 0; i < max_list; i++) entry[i] = 0; } template <class List_entry> int List<List_entry>::size()const { return count; } template <class List_entry> bool List<List_entry>::full()const { return (count >= max_list); } template <class List_entry> bool List<List_entry>::empty()const { return count == 0; } template <class List_entry> void List<List_entry>::clear() { count = 0; } template <class List_entry> void List<List_entry>::traverse(void(*visit)(List_entry &x)) { //int i; for(int i = 0; i < count; i++) { (*visit)(entry[i]); } } template <class List_entry> Error_code List<List_entry>::retrieve(int position, List_entry &x)const { if(empty()) return underflow; if(position < 0 || position > count) return range_error; else { x = entry[position]; return success; } } template <class List_entry> Error_code List<List_entry>::replace(int position,const List_entry &x) { if(empty()) return underflow; if(position < 0 || position > count) return range_error; else { entry[position] = x; return success; } } template <class List_entry> Error_code List<List_entry>::remove(int position,List_entry &x) { if(empty()) return underflow; if(position < 0 || position > count) return range_error; x = entry[position]; for(int i = position; i < count-1; i++) entry[position] = entry[position+1]; count--; return success; } template <class List_entry> Error_code List<List_entry>::insert(int position,const List_entry &x) { if(full()) return overflow; if(position < 0 || position > count) return range_error; for(int i = count-1; i >= position; i--) entry[i+1] = entry[i]; entry[position] = x; count++; return success; }
main.cpp(可为其他文件名,只有代码中有main函数即可):
/* * main.cpp * * Created on: 2015年9月13日 * Author: Lv_Lang */ #include "List.cpp" #include <cstdio> void print(int &x) { printf("%d\n",x); } void test() { List<int> mylist; for(int i = 0; i < 10; i++) mylist.insert(i,i+1); int a,b; mylist.retrieve(1,a); printf("%d\n",a); mylist.replace(1,111); mylist.retrieve(1,b); printf("%d\n",b); int size = mylist.size(); printf("%d\n",size); mylist.traverse(print); } int main() { test(); return 0; }
在List的成员函数中,有一个比较有趣同时比较有用就是traverse遍历函数,这个函数的参数是一个函数的指针,而这个函数可以在后期(main函数)中自己定义,这个比较值得考究理解下其效用性。
C++数据结构之List--线性实现的更多相关文章
- Java数据结构介绍(线性结构和非线性结构)
数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...
- 【C#数据结构系列】线性表
一:线性表 1.1:定义:零个或多个数据元素的有限序列 1.2: 线性表元素个数n定义为线性表的长度,n = 0称为空表,i 为数据元素ai在线性表中的位序. 1.3:满足线性表的条件:(1):有序, ...
- 【算法与数据结构实战】线性表操作-实现A并B,结果放入A中
//数据结构与算法基础题1:线性表操作,实现A并B,结果放入A中 #include "stdafx.h" #include <iostream> #include &l ...
- PHP数据结构之二 线性表中的顺序表的PHP实现
线性表 (一)基本特点:最基本.最简单.最常用的一种数据结构 在这种结构中: 1.存在一个唯一的被称为“第一个”的数据元素: 2.存在一个唯一的被称为“最后一个”的数据元素: 3.除第一个元素外,每个 ...
- C语言数据结构——第二章 线性表
二.线性表 2.1-线性表简介 2.1.1-线性表的定义 线性表是由若干个相同特性的数据元素组成的有限序列.若该线性表不包含任何元素,则称为空表,此时长度为0,当线性表不为空时,表中的元素的个数就是线 ...
- C++ 数据结构 1:线性表
1 数据结构 1.1 数据结构中基本概念 数据:程序的操作对象,用于描述客观事物. 数据的特点: 可以输入到计算机 可以被计算机程序处理 数据是一个抽象的概念,将其进行分类后得到程序设计语言中的类型. ...
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...
- c语言数据结构学习心得——线性表
线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...
- 数据结构 - 静态顺序线性表的实行(C语言)
数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...
- 数据结构导论 四 线性表的顺序存储VS链式存储
前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...
随机推荐
- 有关Flash Player的内存管理及泄露检测
1.Flash Player分配内存的策略是少量请求大内存块, 也就是不会频繁向系统请求内存. 2.Flash Player内存的回收是根据引用计数和标记清除(比较耗cpu)这两种方法. 3.从理论上 ...
- TWaver HTML5 (2D)--基本概念
基本概念 TWaver HTML5(以下简称TWaver)使用HTML5技术和javascript语言,可在支持HTML5的浏览器上进行绘图. 使用TWaver前,需熟悉几个基本概念:图元(Eleme ...
- js中style,currentStyle和getComputedStyle的区别
1.style只能获取元素的内联样式,内部样式和外部样式是获取不到的.例子: <div id="test" style="width:100px;height:20 ...
- q和blockquote标签的区别
q用来分离文本中的引语,定义一个短的引用.该标签会对引用的文本加双引号,一般情况很少用,博客论坛系统会用得多一些: blockquote用于对长文本的引用,用来定义一段引语,标签内的内容会自动有缩进: ...
- Java学习之约瑟夫环的两中处理方法
package day_2; import java.util.Scanner; /** * @author Administrator * 约瑟夫环问题: 设编号为 1,2,3,....n的N个人围 ...
- hdu----(3065)病毒侵袭持续中(AC自动机)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- uva------Help is needed for Dexter(11384)
Problem H Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided t ...
- ARM 汇编的一些规范
A.5.1 文件格式 ARM 源程序文件(即源文件)为文件格式,可以使用任一文本编辑器编写程序代码. 在一个项目中,至少要有一个汇编源文件或C 程序文件,可以有多个汇编 ...
- Permutation Sequence [LeetCode]
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Combination Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...