※数据结构※→☆线性表结构(queue)☆============队列 链式存储结构(queue list)(九)

        队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

        在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。


队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表
        (1)允许删除的一端称为队头(Front)。
        (2)允许插入的一端称为队尾(Rear)。
        (3)当队列中没有元素时称为空队列。
        (4)队列亦称作先进先出(First In First Out)的线性表,简称为FIFO表。
       

        队列的修改是依先进先出的原则进行的。新来的成员总是加入队尾(即不允许"加塞"),每次离开的成员总是队列头上的(不允许中途离队),即当前"最老的"成员离队。

        


链式存储结构
        在计算机中用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的).
        它不要求逻辑上相邻的元素在物理位置上也相邻.因此它没有顺序存储结构所具有的弱点,但也同时失去了顺序表可随机存取的优点.


        链式存储结构特点:
                1、比顺序存储结构的存储密度小 (每个节点都由数据域和指针域组成,所以相同空间内假设全存满的话顺序比链式存储更多)。
                2、逻辑上相邻的节点物理上不必相邻。
                3、插入、删除灵活 (不必移动节点,只要改变节点中的指针)。
                4、查找结点时链式存储要比顺序存储慢。
                5、每个结点是由数据域和指针域组成。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注、支持、顶起我的博客。
你们的关注就是我的动力,本节笔记到这里就结束了。


潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家留言指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~
------------------------------------------
Country: China
Tel: +86-152******41
QQ: 451292510
E-mail: xiaoting_chen2010@163.com
------------------------------------------


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

       


AL_Node.h

/**
  @(#)$Id: AL_Node.h 44 2013-09-13 08:50:04Z xiaoting $
  @brief	store data, and be used to AL_ListSingle, AL_ListDouble, AL_ListCircular and so on

  Chain storage structure//
  The computer using a set of arbitrary linear table storage unit stores data elements (which may be a continuous plurality of memory cells, it can be discontinuous).
  It does not require the logic elements of adjacent physical location is adjacent to and therefore it is not sequential storage structure has a weakness, but also lost the sequence table can be accessed randomly advantages.

  Chain store structural features:
	1, compared with sequential storage density storage structure (each node consists of data fields and pointers domains, so the same space is full, then assume full order of more than chain stores).
	2, the logic is not required on the adjacent node is physically adjacent.
	3, insert, delete and flexible (not the mobile node, change the node as long as the pointer).
	4, find the node when stored sequentially slower than the chain stores.
	5, each node is a pointer to the data fields and domains.

  @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_Node.h $
  @Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_Node.h 44 2013-09-13 08:50:04Z xiaoting $
 */

#ifndef CXX_AL_NODE_H
#define CXX_AL_NODE_H

///
//			AL_Node
///

template<typename T> class AL_ListSingle;
template<typename T> class AL_ListDouble;
template<typename T> class AL_ListCircularSingle;
template<typename T> class AL_ListCircularDouble;
template<typename T> class AL_StackList;
template<typename T> class AL_QueueList;
template<typename T> class AL_QueuePriorityList;

template<typename T> 
class AL_Node
{
public:
	/**
	* Destruction
	*
	* @param
	* @return
	* @note
	* @attention 
	*/
	~AL_Node();

protected:
private:
	friend class AL_ListSingle<T>;
	friend class AL_ListDouble<T>;
	friend class AL_ListCircularSingle<T>;
	friend class AL_ListCircularDouble<T>;
	friend class AL_StackList<T>;
	friend class AL_QueueList<T>;
	friend class AL_QueuePriorityList<T>;
	
	/**
	* Construction
	*
	* @param
	* @return
	* @note		private the Construction, avoid the others use it
	* @attention
	*/
	AL_Node();
	
	/**
	* Construction
	*
	* @param const T& tTemplate <IN>
	* @return
	* @note
	* @attention private the Construction, avoid the others use it
	*/
	AL_Node(const T& tTemplate);

public:
protected:
private:
	T m_data;				//the friend class can use it directly
	AL_Node *m_pPre;			//previous data			AL_ListDouble will use it
	AL_Node *m_pNext;			//next data
};

///
//			AL_Node
///

/**
* Construction
*
* @param
* @return
* @note		private the Construction, avoid the others use it
* @attention
*/
template<typename T>
AL_Node<T>::AL_Node():
m_pPre(NULL),
m_pNext(NULL)
{
	//memset(&m_data, 0x00, sizeof(T));		//can not use memset, as to pointer or virtural pointer of class
}

/**
* Construction
*
* @param const T& tTemplate <IN>
* @return
* @note
* @attention private the Construction, avoid the others use it
*/
template<typename T>
AL_Node<T>::AL_Node(const T& tTemplate):
m_data(tTemplate),
m_pPre(NULL),
m_pNext(NULL)
{

}


/**
* Destruction
*
* @param
* @return
* @note
* @attention 
*/
template<typename T>
AL_Node<T>::~AL_Node()
{
	//it doesn't matter to clear the pointer or not.
	m_pPre = NULL;
	m_pNext = NULL;
}

#endif // CXX_AL_NODE_H
/* EOF */



AL_QueueList.h

/**
  @(#)$Id: AL_QueueList.h 44 2013-09-13 08:50:04Z xiaoting $
  @brief   A queue is a special linear form, so special is that it only allows the front end of the table (front) delete operation, 
  and the rear end of the table (rear) for insertion, and the stack, as the queue is an operating by restricted linear form. Insert 
  operation is called the tail end, the end delete operation called HOL. No element in the queue, it is called an empty queue.
  
  This data structure in the queue, the first element inserted will be the first element to be removed; otherwise the last inserted 
  element will be the last element to be removed, so the queue is also known as "first in first out" (FIFO-first in first out) linear 
  form.

  @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_QueueList.h $
  @Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_QueueList.h 44 2013-09-13 08:50:04Z xiaoting $
 */

#ifndef CXX_AL_QUEUELIST_H
#define CXX_AL_QUEUELIST_H

///
//			AL_QueueList
///

template<typename T>  
class AL_QueueList
{
public:
	/**
	* Construction
	*
	* @param
	* @return
	* @note
	* @attention
	*/
	AL_QueueList();

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

	/**
	* IsEmpty
	*
	* @param	VOID
	* @return	BOOL
	* @note		Returns true queue is empty
	* @attention
	*/
	BOOL IsEmpty() const;

	/**
	* Front
	*
	* @param	T& tTypeOut <OUT>
	* @return	BOOL
	* @note		Returns a reference to the first element at the front of the queue.
	* @attention
	*/
	BOOL Front(T& tTypeOut) const;

	/**
	* Back
	*
	* @param	T& tTypeOut <OUT>
	* @return	BOOL
	* @note		Returns a reference to the last and most recently added element at the back of the queue.
	* @attention
	*/
	BOOL Back(T& tTypeOut) const;

	/**
	* Pop
	*
	* @param	T& tTypeOut <OUT>
	* @return	BOOL
	* @note		Removes an element from the front of the queue.
	* @attention
	*/
	BOOL Pop(T& tTypeOut);

		
	/**
	* Push
	*
	* @param	const T& tTemplate
	* @return	BOOL
	* @note		Adds an element to the back of the queue.
	* @attention	
	*/
	BOOL Push(const T& tTemplate);

	/**
	* Size
	*
	* @param	VOID
	* @return	DWORD
	* @note		Returns the number of elements in the queue
	* @attention
	*/
	DWORD Size() const;

	/**
	* Clear
	*
	* @param	VOID
	* @return	VOID
	* @note		clear all data
	* @attention
	*/
	VOID Clear();
	
protected:
private:

public:
protected:
private: 
	AL_Node<T>*		m_pHeader;
	DWORD			m_dwSize;

	AL_Node<T>*		m_pFront;
	AL_Node<T>*		m_pRear;
};


/**
* Construction
*
* @param
* @return
* @note
* @attention
*/
template<typename T> 
AL_QueueList<T>::AL_QueueList():
m_pHeader(NULL),
m_dwSize(0x00),
m_pFront(NULL),
m_pRear(NULL)
{
	m_pHeader = new AL_Node<T>;

}

/**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
template<typename T> 
AL_QueueList<T>::~AL_QueueList()
{
	Clear();
	//delete the header
	if (NULL != m_pHeader) {
		delete m_pHeader;
		m_pHeader = NULL;
	}
}

/**
* IsEmpty
*
* @param	VOID
* @return	BOOL
* @note		Returns true queue is empty
* @attention
*/
template<typename T> BOOL 
AL_QueueList<T>::IsEmpty() const
{
	return (0x00 == m_pHeader->m_pNext) ? TRUE:FALSE;
}


/**
* Front
*
* @param	T& tTypeOut <OUT>
* @return	BOOL
* @note		Returns a reference to the first element at the front of the queue.
* @attention
*/
template<typename T> BOOL 
AL_QueueList<T>::Front(T& tTypeOut) const
{
	if (TRUE ==IsEmpty()) {
		return FALSE;
	}

	tTypeOut = m_pFront->m_data;
	return TRUE;
}

/**
* Back
*
* @param	T& tTypeOut <OUT>
* @return	BOOL
* @note		Returns a reference to the last and most recently added element at the back of the queue.
* @attention
*/
template<typename T> BOOL 
AL_QueueList<T>::Back(T& tTypeOut) const
{
	if (TRUE ==IsEmpty()) {
		return FALSE;
	}

	tTypeOut = m_pRear->m_data;
	return TRUE;
}

/**
* Pop
*
* @param	T& tTypeOut <OUT>
* @return	BOOL
* @note		Removes an element from the front of the queue.
* @attention
*/
template<typename T> BOOL 
AL_QueueList<T>::Pop(T& tTypeOut)
{
	if (TRUE ==IsEmpty()) {
		return FALSE;
	}
	
	AL_Node<T>*	pPop = m_pFront;
	//get the previous node of m_pRear
	m_pHeader->m_pNext = m_pFront->m_pNext;
	m_pFront = m_pHeader->m_pNext;
	tTypeOut = pPop->m_data;

	//delete the top
	delete pPop;
	pPop = NULL;

	m_dwSize--;
	return TRUE;
}

	
/**
* Push
*
* @param	const T& tTemplate
* @return	BOOL
* @note		Adds an element to the back of the queue.
* @attention	
*/
template<typename T> BOOL 
AL_QueueList<T>::Push(const T& tTemplate)
{
	AL_Node<T>*	pPush = new AL_Node<T>;
	if (NULL == pPush) {
		//new error
		return FALSE;
	}
	pPush->m_data = tTemplate;

	if (TRUE == IsEmpty()) {
		//the first time Push, not need to ++
		m_pHeader->m_pNext = pPush;
		m_pFront = m_pHeader->m_pNext;
		m_pRear = pPush;
	}
	else {
		m_pRear->m_pNext = pPush;
		m_pRear = pPush;
	}

	m_dwSize++;
	return TRUE;
}

/**
* Size
*
* @param	VOID
* @return	DWORD
* @note		Returns the number of elements in the queue
* @attention
*/
template<typename T> DWORD 
AL_QueueList<T>::Size() const
{
	return m_dwSize;
}

/**
* Clear
*
* @param	VOID
* @return	VOID
* @note		clear all data
* @attention
*/
template<typename T> VOID 
AL_QueueList<T>::Clear()
{
	AL_Node<T>* pDelete = NULL;
	while(NULL != m_pHeader->m_pNext){
		//get the node
		pDelete = m_pHeader->m_pNext;
		m_pHeader->m_pNext = pDelete->m_pNext;
		delete pDelete;
		pDelete = NULL;
	}
	m_dwSize = 0x00;
}

#endif // CXX_AL_QUEUELIST_H
/* EOF */


测试代码

#ifdef TEST_AL_QUEUELIST
	AL_QueueList<DWORD> cQueueList;
	BOOL bEmpty = cQueueList.IsEmpty();
	std::cout<<bEmpty<<std::endl;
	DWORD dwSize = cQueueList.Size();
	std::cout<<dwSize<<std::endl;
	DWORD dwFront = 0x00;
	cQueueList.Front(dwFront);
	std::cout<<dwFront<<std::endl;
	DWORD dwBack = 0x00;
	cQueueList.Back(dwBack);
	std::cout<<dwBack<<std::endl;
	DWORD dwPop = 0x00;
	cQueueList.Pop(dwPop);
	std::cout<<dwPop<<std::endl;

	cQueueList.Push(999);
	bEmpty = cQueueList.IsEmpty();
	std::cout<<bEmpty<<std::endl;
	dwSize = cQueueList.Size();
	std::cout<<dwSize<<std::endl;
	cQueueList.Front(dwFront);
	std::cout<<dwFront<<std::endl;
	cQueueList.Back(dwBack);
	std::cout<<dwBack<<std::endl;
	cQueueList.Pop(dwPop);
	std::cout<<dwPop<<std::endl;

	for (DWORD dwCnt=1; dwCnt<16; dwCnt++) {
		cQueueList.Push(dwCnt);
		cQueueList.Back(dwBack);
		std::cout<<dwBack<<std::endl;
	}

	dwSize = cQueueList.Size();
	std::cout<<dwSize<<std::endl;
	cQueueList.Front(dwFront);
	std::cout<<dwFront<<std::endl;

	while (0x00 != cQueueList.Size()) {
		cQueueList.Pop(dwPop);
		std::cout<<dwPop<<std::endl;
	}

	bEmpty = cQueueList.IsEmpty();
	std::cout<<bEmpty<<std::endl;
	dwSize = cQueueList.Size();
	std::cout<<dwSize<<std::endl;
	cQueueList.Front(dwFront);
	std::cout<<dwFront<<std::endl;
	cQueueList.Back(dwBack);
	std::cout<<dwBack<<std::endl;
	cQueueList.Pop(dwPop);
	std::cout<<dwPop<<std::endl;
#endif





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值