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--线性实现的更多相关文章

  1. Java数据结构介绍(线性结构和非线性结构)

    数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...

  2. 【C#数据结构系列】线性表

    一:线性表 1.1:定义:零个或多个数据元素的有限序列 1.2: 线性表元素个数n定义为线性表的长度,n = 0称为空表,i 为数据元素ai在线性表中的位序. 1.3:满足线性表的条件:(1):有序, ...

  3. 【算法与数据结构实战】线性表操作-实现A并B,结果放入A中

    //数据结构与算法基础题1:线性表操作,实现A并B,结果放入A中 #include "stdafx.h" #include <iostream> #include &l ...

  4. PHP数据结构之二 线性表中的顺序表的PHP实现

    线性表 (一)基本特点:最基本.最简单.最常用的一种数据结构 在这种结构中: 1.存在一个唯一的被称为“第一个”的数据元素: 2.存在一个唯一的被称为“最后一个”的数据元素: 3.除第一个元素外,每个 ...

  5. C语言数据结构——第二章 线性表

    二.线性表 2.1-线性表简介 2.1.1-线性表的定义 线性表是由若干个相同特性的数据元素组成的有限序列.若该线性表不包含任何元素,则称为空表,此时长度为0,当线性表不为空时,表中的元素的个数就是线 ...

  6. C++ 数据结构 1:线性表

    1 数据结构 1.1 数据结构中基本概念 数据:程序的操作对象,用于描述客观事物. 数据的特点: 可以输入到计算机 可以被计算机程序处理 数据是一个抽象的概念,将其进行分类后得到程序设计语言中的类型. ...

  7. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  8. c语言数据结构学习心得——线性表

    线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...

  9. 数据结构 - 静态顺序线性表的实行(C语言)

    数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...

  10. 数据结构导论 四 线性表的顺序存储VS链式存储

    前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...

随机推荐

  1. 三张图彻底了解Java中字符串的不变性

    转载: 三张图彻底了解Java中字符串的不变性 定义一个字符串 String s = "abcd"; s中保存了string对象的引用.下面的箭头可以理解为"存储他的引用 ...

  2. python 练习 3

    #!/usr/bin/python # -*- coding: utf-8 -*- def z94(): #斐波那契数列 def filie(x): a,b,t=1,1,0 if x==1 or x= ...

  3. python中2进制、10进制、16进制等之间的转换

    10转2: bin(8) # '0b1000' 2转10: int( 10转16: hex(15) # '0xf' 16转10: int( 2进制和16进制中间通过转10进制可以相互转换 from b ...

  4. 使用git上传项目

    1. 安装Git 2. 安装TortoiseGit 3.任意文件夹选择「TortoiseGit」>「settings」,打开如下界面. 3. 生成SSH公钥 3.1运行Git Bash,如下命令 ...

  5. C++的vector学习abc

    开始学习和使用vector了,用到之后再去学似乎神迹的感觉啊,就像跑一下就能给个糖吃哈哈 百度上的六种初始化的方法就不再说了,那些方法都很对. 如果没有值初始化,系统会自行初始化,目前我遇到的是用脚标 ...

  6. mustache模板技术

    一.简介Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档.当然不同的开发语言有不同模板引擎,如 Javascript 下的 Hog ...

  7. [转]z-order引出的问题

    在窗口与窗口之间毫无重叠的情况下,根本不需要关心z-order.然而,当窗口之间出现重叠时,系统就需要通过一个标准来确定窗口的显示顺序.这个标准就是z-order.存在多个因素影响一个窗口的z-ord ...

  8. SQL 修改数据库架构名

    SQl 修改数据库架构名 declare @name sysname declare csr1 cursor for select TABLE_NAME from INFORMATION_SCHEMA ...

  9. jQuery中的quickExpr

    jQuery 源码中的 quickExpr 是用来检测参数 selector 是否是复杂的 HTML 代码(如“abc<div>”)或 #id,匹配结果存放在数组 match 中 // A ...

  10. 二模 (7) day2

    第一题: 题目大意:多重背包. 解题过程: 1.二进制拆分.最慢的点0.5s. 2.单调队列优化会更快,不过我不会.. 第二题: 题目描述:给定一个n×m的矩阵,记录左上角为(1,1),右下角为(n, ...