• [转]特殊线性表(queue)


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

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

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

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

            

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


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



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

           

    AL_QueueList.h

    /**
      @(#)$Id: AL_QueueList.h 36 2013-09-09 02:20:57Z 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-09 10:20:57 +0800 (周一, 09 九月 2013) $
      @Revision $Revision: 36 $
      @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 36 2013-09-09 02:20:57Z 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	VOID
    	* @return	T
    	* @note		Returns a reference to the first element at the front of the queue.
    	* @attention
    	*/
    	T Front() const;
    
    	/**
    	* Back
    	*
    	* @param	VOID
    	* @return	T
    	* @note		Returns a reference to the last and most recently added element at the back of the queue.
    	* @attention
    	*/
    	T Back() const;
    
    	/**
    	* Pop
    	*
    	* @param	VOID
    	* @return	T
    	* @note		Removes an element from the front of the queue.
    	* @attention
    	*/
    	T Pop();
    
    		
    	/**
    	* 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	VOID
    * @return	T
    * @note		Returns a reference to the first element at the front of the queue.
    * @attention
    */
    template<typename T> T 
    AL_QueueList<T>::Front() const
    {
    	T tTypeTemp;
    	memset(&tTypeTemp, 0x00, sizeof(T));
    
    	if (TRUE ==IsEmpty()) {
    		return tTypeTemp;
    	}
    
    	return m_pFront->m_data;
    }
    
    /**
    * Back
    *
    * @param	VOID
    * @return	T
    * @note		Returns a reference to the last and most recently added element at the back of the queue.
    * @attention
    */
    template<typename T> T 
    AL_QueueList<T>::Back() const
    {
    	T tTypeTemp;
    	memset(&tTypeTemp, 0x00, sizeof(T));
    
    	if (TRUE ==IsEmpty()) {
    		return tTypeTemp;
    	}
    
    	return m_pRear->m_data;
    }
    
    /**
    * Pop
    *
    * @param	VOID
    * @return	T
    * @note		Removes an element from the front of the queue.
    * @attention
    */
    template<typename T> T 
    AL_QueueList<T>::Pop()
    {
    	T tTypeTemp;
    	memset(&tTypeTemp, 0x00, sizeof(T));
    
    	if (TRUE ==IsEmpty()) {
    		return tTypeTemp;
    	}
    	
    	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;
    	tTypeTemp = pPop->m_data;
    
    	//delete the top
    	delete pPop;
    	pPop = NULL;
    
    	m_dwSize--;
    	return tTypeTemp;
    }
    
    	
    /**
    * 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 = cQueueList.Front();
    	std::cout<<dwFront<<std::endl;
    	DWORD dwBack = cQueueList.Back();
    	std::cout<<dwBack<<std::endl;
    	DWORD dwPop = cQueueList.Pop();
    	std::cout<<dwPop<<std::endl;
    
    	cQueueList.Push(999);
    	bEmpty = cQueueList.IsEmpty();
    	std::cout<<bEmpty<<std::endl;
    	dwSize = cQueueList.Size();
    	std::cout<<dwSize<<std::endl;
    	dwFront = cQueueList.Front();
    	std::cout<<dwFront<<std::endl;
    	dwBack = cQueueList.Back();
    	std::cout<<dwBack<<std::endl;
    	dwPop = cQueueList.Pop();
    	std::cout<<dwPop<<std::endl;
    
    	for (DWORD dwCnt=1; dwCnt<16; dwCnt++) {
    		cQueueList.Push(dwCnt);
    		dwBack = cQueueList.Back();
    		std::cout<<dwBack<<std::endl;
    	}
    
    	dwSize = cQueueList.Size();
    	std::cout<<dwSize<<std::endl;
    	dwFront = cQueueList.Front();
    	std::cout<<dwFront<<std::endl;
    
    	while (0x00 != cQueueList.Size()) {
    		dwPop = cQueueList.Pop();
    		std::cout<<dwPop<<std::endl;
    	}
    
    	bEmpty = cQueueList.IsEmpty();
    	std::cout<<bEmpty<<std::endl;
    	dwSize = cQueueList.Size();
    	std::cout<<dwSize<<std::endl;
    	dwFront = cQueueList.Front();
    	std::cout<<dwFront<<std::endl;
    	dwBack = cQueueList.Back();
    	std::cout<<dwBack<<std::endl;
    	dwPop = cQueueList.Pop();
    	std::cout<<dwPop<<std::endl;
    #endif
  • 相关阅读:
    Matlab脚本和函数
    C# vb .net图像合成-合成富文本
    C# vb .net图像合成-合成星形
    C# vb .net图像合成-多图片叠加合成
    C# vb .net图像合成-合成艺术字 照片合成艺术字
    C# vb .net图像合成-合成文字
    C# vb .net实现锐化效果滤镜
    C# vb .net实现像素化效果滤镜打马赛克
    C# vb .net实现过度曝光效果滤镜
    C# vb .net实现淡色效果滤镜
  • 原文地址:https://www.cnblogs.com/tangtang-123/p/4436755.html
Copyright © 2020-2023  润新知