您的位置:首页 > 理论基础 > 数据结构算法

常用数据结构之顺序结构List实现

2012-03-10 16:36 381 查看
用C++的模板类写了个普通的List类,顺序结构存储的。留个备份。(模板类在G++中只能将声明与定义放在头文件里,C++标准也是这么说的



基于数组实现的列表,就是一数组的封装,其插入和删除的时间复杂度是O(n)。

后面还有一个简单的调用例子

/*
* =====================================================================================
*
*       Filename:  01list.h
*
*    Description:  list(use array)
*
*        Version:  1.0
*        Created:  2012年03月08日 00时19分06秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Lavey Luo (lavey), luoyi.smt@gmail.com
*   Organization:
*
* =====================================================================================
*/

namespace st
{
enum Status
{
OK = 0,
ERROR = -1
};

const int MAX_LEN = 20;
template<class VALUE_TYPE>
class list
{
public:
explicit list():_length(0){};
~list(){};
public:
/* 初始化顺序线性表 */
Status Init();

/* 初始条件:顺序线性表已存在。操作结果:若L为空表,则返回OK,否则返回ERROR*/
Status Empty();

/* 初始条件:顺序线性表已存在。操作结果:将L重置为空表 */
Status Clear();

/* 初始条件:顺序线性表已存在。操作结果:返回L中数据元素个数 */
int Length();

/* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */
/* 操作结果:用e返回第i个数据元素的值,注意i是指位置,第1个位置的数组是从0开始 */
Status GetElem(int i,VALUE_TYPE& e);

/* 初始条件:顺序线性表L已存在 */
/* 操作结果:返回第1个与e满足关系的数据元素的位序。 */
/* 若这样的数据元素不存在,则返回值为ERROR */
int Locate(const VALUE_TYPE& e);

/* 初始条件:顺序线性表已存在,1≤i≤ListLength(L), */
/* 操作结果:第i个位置之前插入新的数据元素e,长度加1 */
Status Insert(int i,const VALUE_TYPE& e);

/* 初始条件:顺序线性表L已存在 */
/* 操作结果:在顺序线性表的尾端添加一个元素,长度加1 */
Status Pushback(const VALUE_TYPE& e);

/* 初始条件:顺序线性表已存在,1≤i≤ListLength(L) */
/* 操作结果:删除的第i个数据元素,并用e返回其值,L的长度减1 */
Status Delete(int i,VALUE_TYPE *e);
private:
int _length;
VALUE_TYPE _elem[MAX_LEN];
};

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Init()
{
_length = 0;
return OK;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Empty()
{
if (_length == 0)
return OK;
else
return ERROR;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Clear()
{
return Init();
}

template<class VALUE_TYPE>
int list<VALUE_TYPE>::Length()
{
return _length;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::GetElem(int i,VALUE_TYPE& e)
{
if ( _length == 0 || i > _length || i < 1)
return ERROR;

e = _elem[i - 1];
return OK;
}

template<class VALUE_TYPE>
int list<VALUE_TYPE>::Locate(const VALUE_TYPE& e)
{
if (_length == 0)
return ERROR;

for (int i = 0; i < _length; i++)
{
if (_elem[i] == e)
return i + 1;
}
return ERROR;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Insert(int i, const VALUE_TYPE& e)
{
if (_length >= MAX_LEN)
return ERROR;
if (i < 1 )
return ERROR;

if (i <= _length)        /* 若插入数据位置不在表尾 */
{
for(int k = _length-1; k >= i-1; k--)  /* 将要插入位置之后的数据元素向后移动一位 */
_elem[k+1] = _elem[k];
}
_elem[i-1] = e;          /* 将新元素插入 */
_length++;

return OK;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Pushback(const VALUE_TYPE& e)
{
_elem[_length] = e;
_length++;
return OK;
}

template<class VALUE_TYPE>
Status list<VALUE_TYPE>::Delete(int i, VALUE_TYPE *e)
{
if (_length == 0 || i <= 0)
return ERROR;

if (i > _length)
return ERROR;

*e = _elem[i-1];
for (int k = i -1; k < _length; k++ )
e[k] = e[k+1];
_length--;
return OK;
}
}
/*
* =====================================================================================
*
*       Filename:  testlist.cpp
*
*    Description:   test examplle of the list
*
*        Version:  1.0
*        Created:  2012年03月08日 01时02分04秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Lavey Luo (lavey), luoyi.smt@gmail.com
*   Organization:
*
* =====================================================================================
*/
#include "01list.h"
#include <iostream>
using std::cout;
using std::endl;

int test_list(int argc,  char** argv)
{
st::list<int> test_1;
test_1.Init();
cout << "x test len: " << test_1.Length() << endl;;
test_1.Insert(1, 4);
test_1.Insert(2, 3);
test_1.Insert(3, 2);
test_1.Insert(4, 1);

cout << "xx test len: " << test_1.Length() << endl;
int value = 0;
test_1.GetElem(1, value);
cout << "test 1:" << value << endl;
test_1.GetElem(2, value);
cout << "test 2:" << value << endl;
test_1.GetElem(3, value);
cout << "test 3:" << value << endl;
test_1.GetElem(4, value);
cout << "test 4:" << value << endl;
cout << "test local 1: " << test_1.Locate(1) << endl;
cout << "xxx test len:" << test_1.Length() << endl;

test_1.Delete(3, &value);
cout << "delete : " << value << endl;
cout << "xxxx test len:" << test_1.Length() << "value: "<< value << endl;
test_1.Empty();
cout << "xxx test len:" << test_1.Length() << endl;
return 0;
}






                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: