常用數據結構之順序結構List實現

        用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), [email protected]
 *   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), [email protected]
 *   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;
}





發佈了34 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章