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

※数据结构※→☆线性表结构(list)☆============链表 顺序存储结构(list sequence)(一)

2013-08-07 11:10 645 查看
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。

将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构是顺序结构。



顺序存储结构

在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 

顺序存储结构是存储结构类型中的一种,该结构是把逻辑上相邻的节点存储在物理位置上相邻的存储单元中,结点之间的逻辑关系由存储单元的邻接关系来体现。由此得到的存储结构为顺序存储结构,通常顺序存储结构是借助于计算机程序设计语言(例如c/c++)的数组来描述的。

顺序存储结构的主要优点是节省存储空间,因为分配给数据的存储单元全用存放结点的数据(不考虑c/c++语言中数组需指定大小的情况),结点之间的逻辑关系没有占用额外的存储空间。采用这种方法时,可实现对结点的随机存取,即每一个结点对应一个序号,由该序号可以直接计算出来结点的存储地址。但顺序存储方法的主要缺点是不便于修改,对结点的插入、删除运算时,可能要移动一系列的结点。



优点:

随机存取表中元素。缺点:插入和删除操作需要移动元素。

本代码默认list可以容纳的item数目为100个,用户可以自行设置item数目。当list饱和时,会自动以2倍的长度进行递增。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注我的博客。

本节笔记到这里就结束了。

潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。

编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。

如果文章中有什么疏漏的地方,也请大家指正。也希望大家可以多留言来和我探讨编程相关的问题。

最后,谢谢你们一直的支持~~~

C++完整个代码示例(代码在VS2005下测试可运行)




AL_ListSeq.h

/**
  @(#)$Id: AL_ListSeq.h 44 2013-09-13 08:50:04Z xiaoting $
  @brief     Sequence table is in the computer memory as an array stored linear form, refers to a group of contiguous memory cells 
  storing data elements sequentially linear structure. Linear form using sequential storage mode it is called the order form. Sequence 
  table is the table node sequentially stored in the computer memory a set of contiguous memory cell.

  ////////////////////////////////Sequential storage structure//////////////////////////////////////////
  Using a set of addresses in the computer storage unit sequentially stores continuous linear form of individual data elements, called 
  the linear order of the table storage structure.

  Sequential storage structure is a type of a storage structure, the structure is the logically adjacent nodes stored in the physical 
  location of the adjacent memory cells, the logical relationship between nodes from the storage unit to reflect the adjacency. 
  Storage structure thus obtained is stored in order structure, usually by means of sequential storage structure computer programming 
  language (e.g., c / c) of the array to describe.

  The main advantage of the storage structure in order to save storage space, because the allocation to the data storage unit storing 
  all nodes with data (without regard to c / c language in the array size required for the case), the logical relationship between 
  the nodes does not take additional storage space. In this method, the node can be realized on a random access, that is, each node 
  corresponds to a number, the number can be calculated directly from the node out of the memory address. However, the main 
  disadvantage of sequential storage method is easy to modify the node insert, delete operations, may have to move a series of nodes.
							         
  Benefits:
	Random Access table elements. Disadvantages: insert and delete operations need to move elements.

  @Author $Author: xiaoting $
  @Date $Date: 2013-09-13 16:50:04 +0800 (周五, 13 九月 2013) $
  @Revision $Revision: 44 $
  @URL $URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_ListSeq.h $
  @Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_ListSeq.h 44 2013-09-13 08:50:04Z xiaoting $
 */

#ifndef CXX_AL_LISTSEQ_H
#define CXX_AL_LISTSEQ_H

///////////////////////////////////////////////////////////////////////////
//			AL_ListSeq
///////////////////////////////////////////////////////////////////////////

template<typename T> 
class AL_ListSeq
{
public:
	static const DWORD LISTSEQ_DEFAULTSIZE			= 100;
	static const DWORD LISTSEQ_MAXSIZE				= 0xffffffff;
	static const DWORD LISTSEQ_POSITION_INVALID		= 0xffffffff;
	/**
	* Construction
	*
	* @param DWORD dwSize (default value: LISTSEQ_DEFAULTSIZE)
	* @return
	* @note
	* @attention
	*/
	AL_ListSeq(DWORD dwSize = LISTSEQ_DEFAULTSIZE);

	/**
	* Destruction
	*
	* @param
	* @return
	* @note
	* @attention
	*/
	~AL_ListSeq();

	/**
	* Length
	*
	* @param VOID
	* @return DWORD
	* @note get the length of the list
	* @attention
	*/
	DWORD Length() const;
	
	/**
	* Find
	*
	* @param const T& tTemplate <IN> 
	* @return DWORD
	* @note find the position of tTemplate 
	* @attention if not find, will be return 0xffffffff
	*/
	DWORD Find(const T& tTemplate) const;

	/**
	* IsElement
	*
	* @param const T& tTemplate <IN> 
	* @return BOOL
	* @note the tTemplate  is in the list?
	* @attention
	*/
	BOOL IsElement(const T& tTemplate) const;

	/**
	* Insert
	*
	* @param DWORD dwIndex <IN>
	* @param const T& tTemplate <IN> 
	* @return BOOL
	* @note inset the tTemplate  into the list at the position
	* @attention
	*/
	BOOL Insert(DWORD dwIndex,const T& tTemplate);

	/**
	* InsertBegin
	*
	* @param const T& tTemplate <IN> 
	* @return BOOL
	* @note inset the tTemplate  into the list at the position
	* @attention
	*/
	BOOL InsertBegin(const T& tTemplate);

	/**
	* InsertEnd
	*
	* @param const T& tTemplate <IN> 
	* @return BOOL
	* @note inset the tTemplate  into the list at the position
	* @attention
	*/
	BOOL InsertEnd(const T& tTemplate);

	/**
	* Remove
	*
	* @param const T& tTemplate <IN> 
	* @return BOOL
	* @note remove the tTemplate  into the list
	* @attention
	*/
	BOOL Remove(const T& tTemplate);

	/**
	* IsEmpty
	*
	* @param VOID
	* @return BOOL
	* @note the list has data?
	* @attention
	*/
	BOOL IsEmpty() const;

	/**
	* Get
	*
	* @param	T& tTypeOut <OUT>
	* @param	DWORD dwIndex <IN>
	* @return BOOL
	* @note get the const T& at the position
	* @attention  the dwIndex must is little than the list length
	*/
	BOOL Get(T& tTypeOut, DWORD dwIndex) const;

	/**
	* Set
	*
	* @param	T& tTypeOut <OUT>
	* @param	DWORD dwIndex <IN>
	* @param	const T& tTemplate <IN>
	* @return BOOL
	* @note Replaced with the element element element on position index, and returns the old element...
	* @attention Index must in the list
	*/
	BOOL Set(T& tTypeOut, DWORD dwIndex, const T& tTemplate);

	/**
	* Clear
	*
	* @param VOID
	* @return VOID
	* @note clear the data in the list
	* @attention all data will be clear
	*/
	VOID Clear();

protected:
private:
	/**
	* GetBuffer
	*
	* @param VOID
	* @return VOID
	* @note get the work buffer
	* @attention when the buffer is not enough, it will become to double
	*/
	VOID GetBuffer();
	
	/**
	* IsFull
	*
	* @param VOID
	* @return BOOL
	* @note the buffer is full?
	* @attention
	*/
	BOOL IsFull() const;

public:
protected:
private: 
	
	T*			m_pElements;
	DWORD		m_dwMaxSize;
	DWORD		m_dwSize;
};

///////////////////////////////////////////////////////////////////////////
//			AL_ListSeq
///////////////////////////////////////////////////////////////////////////
/**
* Construction
*
* @param DWORD dwSize (default value: LISTSEQ_DEFAULTSIZE)
* @return
* @note
* @attention
*/
template<typename T> 
AL_ListSeq<T>::AL_ListSeq(DWORD dwSize):
m_pElements(NULL),
m_dwMaxSize(dwSize),
m_dwSize(0x00)
{
	if (0x00 == m_dwMaxSize) {
		//for memory deal
		m_dwMaxSize = 1;
	}
	GetBuffer();
}

/**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
template<typename T> 
AL_ListSeq<T>::~AL_ListSeq()
{
	if (NULL != m_pElements) {
		delete[] m_pElements;
		m_pElements = NULL;
	}
}

/**
* Length
*
* @param
* @return
* @note get the length of the list
* @attention
*/
template<typename T> DWORD 
AL_ListSeq<T>::Length() const
{
	if (TRUE == IsEmpty()) {
		return 0;
	}

	return m_dwSize;
}

/**
* Find
*
* @param const T& tTemplate <IN> 
* @return DWORD
* @note find the position of tTemplate 
* @attention  if not find, will be return 0xffffffff
*/
template<typename T> DWORD 
AL_ListSeq<T>::Find(const T& tTemplate) const
{
	if (TRUE == IsEmpty()) {
		return LISTSEQ_POSITION_INVALID;
	}

	for(DWORD dwCnt=0; dwCnt<Length();dwCnt++) {
		if (m_pElements[dwCnt] == tTemplate ) {
			return dwCnt;
		}
	}
	return LISTSEQ_POSITION_INVALID;
}

/**
* IsElement
*
* @param const T& tTemplate <IN> 
* @return BOOL
* @note the tTemplate  is in the list?
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::IsElement(const T& tTemplate) const
{
	if (TRUE == IsEmpty()) {
		return FALSE;
	}

	if (LISTSEQ_POSITION_INVALID == Find(tTemplate )) {
		return FALSE;
	}

	return TRUE;
}

/**
* Insert
*
* @param DWORD dwIndex <IN>
* @param const T& tTemplate <IN> 
* @return BOOL
* @note inset the tTemplate  into the list at the position
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::Insert(DWORD dwIndex, const T& tTemplate)
{
	if (dwIndex > Length()) {
		//can not insert to this position
		return FALSE;
	}
	if (TRUE == IsFull()) {
		// full, need to get more work buffer
		GetBuffer();
	}
	
	for(DWORD dwCnt=(Length()-1+1); dwCnt>dwIndex; dwCnt--) {
		m_pElements[dwCnt] = m_pElements[dwCnt-1];
	}
	m_pElements[dwIndex] = tTemplate ;

	m_dwSize++;
	return TRUE;
}

/**
* InsertBegin
*
* @param const T& tTemplate <IN> 
* @return BOOL
* @note inset the tTemplate  into the list at the position
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::InsertBegin(const T& tTemplate)
{
	return Insert(0, tTemplate);
}

/**
* InsertEnd
*
* @param const T& tTemplate <IN> 
* @return BOOL
* @note inset the tTemplate  into the list at the position
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::InsertEnd(const T& tTemplate)
{
	return Insert(Length(), tTemplate);
}

/**
* Remove
*
* @param const T& tTemplate <IN> 
* @return BOOL
* @note remove the tTemplate  into the list
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::Remove(const T& tTemplate)
{
	if (TRUE == IsEmpty()) {
		return FALSE;
	}

	DWORD dwIndex = Find(tTemplate );
	if (LISTSEQ_POSITION_INVALID == dwIndex) {
		return FALSE;
	}
	for(DWORD dwCnt=dwIndex; dwCnt<Length()-1; dwCnt++) {
		m_pElements[dwCnt] = m_pElements[dwCnt+1];
	}
	m_dwSize--;
	return TRUE;
}

/**
* IsEmpty
*
* @param
* @return BOOL
* @note the list has data?
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::IsEmpty() const
{
	return (0x00 == m_dwSize) ? TRUE:FALSE;
}

/**
* Get
*
* @param	T& tTypeOut <OUT>
* @param	DWORD dwIndex <IN>
* @return BOOL
* @note get the T at the position
* @attention the dwIndex must is little than the list length
*/
template<typename T> BOOL
AL_ListSeq<T>::Get(T& tTypeOut, DWORD dwIndex) const
{
	if (TRUE == IsEmpty()) {
		//error
		return FALSE;
	}

	if (Length()-1 < dwIndex) {
		//error
		return FALSE;
	}

	tTypeOut = m_pElements[dwIndex];
	return TRUE;
}

/**
* Set
*
* @param	T& tTypeOut <OUT>
* @param	DWORD dwIndex <IN>
* @param	const T& tTemplate <IN>
* @return BOOL
* @note Replaced with the element element element on position index, and returns the old element...
* @attention Index must in the list
*/
template<typename T> BOOL 
AL_ListSeq<T>::Set(T& tTypeOut, DWORD dwIndex, const T& tTemplate)
{
	if (Length()-1 < dwIndex) {
		//error
		return FALSE;
	}

	tTypeOut = m_pElements[dwIndex];
	m_pElements[dwIndex] = tTemplate ;
	return TRUE;
}

/**
* Clear
*
* @param VOID
* @return VOID
* @note clear the data in the list
* @attention all data will be clear
*/
template<typename T> VOID 
AL_ListSeq<T>::Clear()
{
	//memset(m_pElements, 0x00, sizeof(T)*Length());		//can not use memset, as to pointer or virtural pointer of class
	m_dwSize = 0x00;
}

/**
* GetBuffer
*
* @param VOID
* @return VOID
* @note get the work buffer
* @attention when the buffer is not enough, it will become to double
*/
template<typename T> VOID 
AL_ListSeq<T>::GetBuffer()
{

	if ( (FALSE == IsFull()) && (NULL != m_pElements) ) {
		//we do not need to get more buffer
		return;
	}

	if (NULL == m_pElements) {
		if(0 < m_dwMaxSize){
			//get the new work buffer
			m_pElements = new T[m_dwMaxSize];
			//memset(m_pElements, 0x00, sizeof(T)*m_dwMaxSize);		//can not use memset, as to pointer or virtural pointer of class
		}
		return;
	}

	//we need to get more buffer, store the previous pointer
	T* pLastTpye = NULL;

	// it will become to double
	pLastTpye = m_pElements;
	if (LISTSEQ_MAXSIZE == m_dwMaxSize) {
		//can not get more buffer, please check the application
		return;
	}
	else if (LISTSEQ_MAXSIZE/2 < m_dwMaxSize) {
		m_dwMaxSize = LISTSEQ_MAXSIZE;
	}
	else {
		m_dwMaxSize *= 2;
	}
	if(0 < m_dwMaxSize){
		//get the new work buffer
		m_pElements = new T[m_dwMaxSize];
		//memset(m_pElements, 0x00, sizeof(T)*m_dwMaxSize);		//can not use memset, as to pointer or virtural pointer of class
	}
	//need to copy the last to the current
	for (DWORD dwCpy=0; dwCpy<Length(); dwCpy++) {
		m_pElements[dwCpy] = pLastTpye[dwCpy];
	}
//	memcpy(m_pElements, pLastTpye, sizeof(T)*Length());				//can not use memcopy, as to pointer
	
	//free the last work buffer
	delete[] pLastTpye;
	pLastTpye = NULL;
}

/**
* IsFull
*
* @param
* @return BOOL
* @note the buffer is full?
* @attention
*/
template<typename T> BOOL 
AL_ListSeq<T>::IsFull() const
{
	return (m_dwMaxSize <= Length()) ? TRUE:FALSE;
}

#endif // CXX_AL_LISTSEQ_H
/* EOF */


测试代码

#ifdef TEST_AL_LISTSEQ

	AL_ListSeq<DWORD> cListSeq(1);
	BOOL bEmpty = cListSeq.IsEmpty();
	std::cout<<bEmpty<<std::endl;

	int array[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	for(int i=0;i<15;i++)
		cListSeq.Insert(cListSeq.Length(), array[i]);
	bEmpty = cListSeq.IsEmpty();
	std::cout<<bEmpty<<std::endl;

	//test the interface
	DWORD dwListSeqLen = cListSeq.Length();
	std::cout<<dwListSeqLen<<std::endl;

	DWORD dwFind = cListSeq.Find(16);
	std::cout<<dwFind<<std::endl;
	dwFind = cListSeq.Find(12);
	std::cout<<dwFind<<std::endl;

	BOOL bElement = cListSeq.IsElement(16);
	std::cout<<bElement<<std::endl;
	bElement = cListSeq.IsElement(14);
	std::cout<<bElement<<std::endl;

	BOOL bInsert = cListSeq.Insert(0, 0);
	std::cout<<bInsert<<std::endl;
	bInsert = cListSeq.Insert(16, 16);
	std::cout<<bInsert<<std::endl;
	bInsert = cListSeq.Insert(16, 999);
	std::cout<<bInsert<<std::endl;

	BOOL bRemove = cListSeq.Remove(9846354);
	std::cout<<bRemove<<std::endl;
	bRemove = cListSeq.Remove(999);
	std::cout<<bRemove<<std::endl;

	bRemove = cListSeq.Remove(10);
	std::cout<<bRemove<<std::endl;

	DWORD it = 0x00;
	for (DWORD i=0; i<cListSeq.Length(); i++) {
		cListSeq.Get(it, i);
		std::cout<<it<<std::endl;
	}

	DWORD dwSet = 0x00;
	cListSeq.Set(dwSet, 16, 999);
	std::cout<<dwSet<<std::endl;
	cListSeq.Set(dwSet, 0, 888);
	std::cout<<dwSet<<std::endl;
	cListSeq.Set(dwSet, 11, 777);
	std::cout<<dwSet<<std::endl;

	for (DWORD i=0; i<cListSeq.Length(); i++) {
		cListSeq.Get(it, i);
		std::cout<<it<<std::endl;
	}

	cListSeq.Clear();
	bEmpty = cListSeq.IsEmpty();
	std::cout<<bEmpty<<std::endl;
	dwListSeqLen = cListSeq.Length();
	std::cout<<dwListSeqLen<<std::endl;

	bInsert = cListSeq.Insert(1, 999);
	std::cout<<bInsert<<std::endl;

	bInsert = cListSeq.Insert(0, 666);
	std::cout<<bInsert<<std::endl;
	bRemove = cListSeq.Remove(666);
	std::cout<<bRemove<<std::endl;
	bEmpty = cListSeq.IsEmpty();
	std::cout<<bEmpty<<std::endl;
	dwListSeqLen = cListSeq.Length();
	std::cout<<dwListSeqLen<<std::endl;
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐