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. Peer Code Reviews Made Easy with Eclipse Plug-In

    欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...

  2. noip赛前小结3

    嗯,这是第三份小结. 连续三天的小结. 这几天状态逐渐回来了. 前天3道题rk8左右. 昨天上午3道题rk7,但是有一道题考后1minAC了. 昨天晚上2道题AK了. 今天也3道题rk1了. 这个趋势 ...

  3. 3.5 EF Code First总结

    1. 主键约定 属性名为“ID”(不区分大小写)或类名的后面跟有“ID”. 2. 关系约定 模型之间的关系,EF根据针对类型定义的导航属性来推断关系. 3. 连接字符串约定 (1)默认配置 如果连接字 ...

  4. 接入WebSocket

    闲扯 WebSocket 以前没用过,之前写过一篇博客是基于原生socket的(查看)比较复杂,慎入.今天另外一个APP需要接websocket了,然后便找到了facebook的 SocketRock ...

  5. Visual Studio 2012出现“无法访问T-SQL组件和安装了不兼容伯 DacFx版本”的解决办法

    参考:Visual Studio 2012出现“无法访问T-SQL组件和安装了不兼容伯 DacFx版本”的解决办法 Vs2012的下载地址: https://msdn.microsoft.com/en ...

  6. DOM之兄弟节点

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  7. PHP获取指定时间的上个月

    主要用strtotime()这个函数 php 获得前一个月的月份 date("Y-m-d",strtotime("last month")); php获得给定时 ...

  8. 一些Shell命令

    lsof -nP -itcp:8080 查看本机8080端口在被什么应用占用,可以查看此应用对应的pid. netstat -ant|grep 8081 查看本机8081端口的使用情况. telnet ...

  9. <context:annotation-config> 和 <context:component-scan>的差别

    <context:annotation-config> is used to activate annotations in beans already registered in the ...

  10. linux查找文件命令find

    http://blog.csdn.net/ydfok/article/details/1486451 find 路径 -name'文件名' 如:find / -name '*dhcp*'