优先队列(priority queue)

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高进先出 (largest-in,first-out)的行为特征。

例如下图:任务的优先权及执行顺序的关系
        

        优先队列是0个或多个元素的集合,每个元素都有一个优先权或值

时间复杂度

        Push时进行排序
                有序链表(即顺序存储结构),则插入时找插入点的时间复杂度为O(n)
                直接出链表表头(也就是队头元素)的时间复杂度为O(1)

Pop时进行遍历
                有序链表(即顺序存储结构),则插入时找插入点的时间复杂度为O(1)
                遍历出优先级最高的链表表头(也就是队头元素)的时间复杂度为O(n)

======================================================================================================

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(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_QueuePriorityList.h

/**
@(#)$Id: AL_QueuePriorityList.h 40 2013-09-10 09:14:44Z xiaoting $
@brief
////////////////////////////////Priority queue (priority queue)e//////////////////////////////////////////
Common queue is a FIFO data structure, the end of an additional element in the queue, and the head removed from the queue. In the
priority queue element is given priority. When accessing the element, the element with the highest priority first removed. Priority
queue with the highest first-out (largest-in, first-out) the behavioral characteristics. Priority queue is 0 or more elements of the collection, each element has a priority or value of the operations performed on the
priority queue with a) Find; 2) Insert a new element; 3) Delete in the minimum priority queue (min priorityq ueue), the lookup
operation to search for smallest element priority, delete operation to remove the element; for maximum priority queue (max priority
queue), lookup to search for the largest element of the priority, delete operation is used remove the element. priority queue
element can have the same priority, find and delete operations can be carried out according to any priority. 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-10 17:14:44 +0800 (周二, 10 九月 2013) $
@Revision $Revision: 40 $
@URL $URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_QueuePriorityList.h $
@Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_QueuePriorityList.h 40 2013-09-10 09:14:44Z xiaoting $
*/ #ifndef CXX_AL_QUEUEPRIORITYLIST_H
#define CXX_AL_QUEUEPRIORITYLIST_H ///////////////////////////////////////////////////////////////////////////
// AL_QueuePriorityList
/////////////////////////////////////////////////////////////////////////// template<typename T>
class AL_QueuePriorityList
{
public:
/**
* Construction Constructed using the default priority queue
*
* @param
* @return
* @note
* @attention The default precedence relations: big > small
*/
AL_QueuePriorityList(); /**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
~AL_QueuePriorityList(); /**
* 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 Constructed using the default priority queue
*
* @param
* @return
* @note
* @attention The default precedence relations: big > small
*/
template<typename T>
AL_QueuePriorityList<T>::AL_QueuePriorityList():
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_QueuePriorityList<T>::~AL_QueuePriorityList()
{
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_QueuePriorityList<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_QueuePriorityList<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_QueuePriorityList<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_QueuePriorityList<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_QueuePriorityList<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 {
AL_Node<T>* pMove = m_pFront;
AL_Node<T>* pMovePre = m_pFront;
while(NULL != pMove) {
if (tTemplate > pMove->m_data) {
//bigger
break;
}
pMovePre = pMove;
pMove = pMove->m_pNext;
}
if (NULL == pMove) {
//smallest in the queue
m_pRear->m_pNext = pPush;
m_pRear = pPush;
}
else {
if (m_pFront == pMovePre) {
//inset to the front
pPush->m_pNext = m_pFront;
m_pFront = pPush;
m_pHeader->m_pNext = m_pFront;
}
else {
pPush->m_pNext = pMovePre->m_pNext;
pMovePre->m_pNext = 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_QueuePriorityList<T>::Size() const
{
return m_dwSize;
} /**
* Clear
*
* @param VOID
* @return VOID
* @note clear all data
* @attention
*/
template<typename T> VOID
AL_QueuePriorityList<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_QUEUEPRIORITYLIST_H
/* EOF */

测试代码

#ifdef TEST_AL_QUEUEPRIORITYLIST
AL_QueuePriorityList<DWORD> cQueuePriorityList;
BOOL bEmpty = cQueuePriorityList.IsEmpty();
std::cout<<bEmpty<<std::endl;
DWORD dwSize = cQueuePriorityList.Size();
std::cout<<dwSize<<std::endl;
DWORD dwFront = cQueuePriorityList.Front();
std::cout<<dwFront<<std::endl;
DWORD dwBack = cQueuePriorityList.Back();
std::cout<<dwBack<<std::endl;
DWORD dwPop = cQueuePriorityList.Pop();
std::cout<<dwPop<<std::endl; cQueuePriorityList.Push(999);
bEmpty = cQueuePriorityList.IsEmpty();
std::cout<<bEmpty<<std::endl;
dwSize = cQueuePriorityList.Size();
std::cout<<dwSize<<std::endl;
dwFront = cQueuePriorityList.Front();
std::cout<<dwFront<<std::endl;
dwBack = cQueuePriorityList.Back();
std::cout<<dwBack<<std::endl;
dwPop = cQueuePriorityList.Pop();
std::cout<<dwPop<<std::endl; DWORD dwTestNum[]={14,10,56,7,83,22,36,91,3,47,72,0,91,100,7};
for (DWORD dwCnt=0; dwCnt<(sizeof(dwTestNum)/sizeof(DWORD)); dwCnt++) {
cQueuePriorityList.Push(dwTestNum[dwCnt]);
dwBack = cQueuePriorityList.Back();
std::cout<<dwTestNum[dwCnt]<<std::endl;
} dwSize = cQueuePriorityList.Size();
std::cout<<dwSize<<std::endl;
for (DWORD dwCount=0; dwCount<dwSize; dwCount++) {
dwPop = cQueuePriorityList.Pop();
std::cout<<dwPop<<std::endl;
}
struct sMySelfData {
BOOL operator > (sMySelfData sData) const{
return this->dwPriority > sData.dwPriority;
}
DWORD dwPriority;
DWORD dwValue;
};
// class sMySelfData {
// public:
// BOOL operator > (sMySelfData sData) const{
// return this->dwPriority > sData.dwPriority;
// }
// DWORD dwPriority;
// DWORD dwValue;
// };
bEmpty = cQueuePriorityList.IsEmpty();
std::cout<<bEmpty<<std::endl;
dwSize = cQueuePriorityList.Size();
std::cout<<dwSize<<std::endl;
dwFront = cQueuePriorityList.Front();
std::cout<<dwFront<<std::endl;
dwBack = cQueuePriorityList.Back();
std::cout<<dwBack<<std::endl;
dwPop = cQueuePriorityList.Pop();
std::cout<<dwPop<<std::endl; //test myself data
sMySelfData sData1;
memset(&sData1, 0x00, sizeof(sMySelfData));
sMySelfData sData2;
memset(&sData1, 0x00, sizeof(sMySelfData));
sMySelfData sData3;
memset(&sData1, 0x00, sizeof(sMySelfData));
sMySelfData sData4;
memset(&sData1, 0x00, sizeof(sMySelfData));
sData1.dwPriority = 1;
sData1.dwValue = 1;
sData2.dwPriority = 2;
sData2.dwValue = 2;
sData3.dwPriority = 3;
sData3.dwValue = 3;
sData4.dwPriority = 2;
sData4.dwValue = 4;
sMySelfData sData5; AL_QueuePriorityList<sMySelfData> cQueuePriorityListMy;
cQueuePriorityListMy.Push(sData1);
cQueuePriorityListMy.Push(sData2);
cQueuePriorityListMy.Push(sData3);
cQueuePriorityListMy.Push(sData4); dwSize = cQueuePriorityListMy.Size();
std::cout<<dwSize<<std::endl; sMySelfData sData;
memset(&sData, 0x00, sizeof(sMySelfData));
for (DWORD dwCount=0; dwCount<dwSize; dwCount++) {
memset(&sData, 0x00, sizeof(sMySelfData));
sData = cQueuePriorityListMy.Pop();
std::cout<<sData.dwPriority<<" "<<sData.dwValue<<std::endl;
}
#endif

[置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)的更多相关文章

  1. c数据结构 -- 线性表之 复杂的链式存储结构

    复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...

  2. 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)

    温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...

  3. 数据结构导论 四 线性表的顺序存储VS链式存储

    前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...

  4. 线性表的Java实现--链式存储(单向链表)

    单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始. 链式存储结构的线性表将采用一组任意的存储单元存放线性表中的数据元素.由于不需要按顺序存储,链表在 ...

  5. 线性表的顺序存储和链式存储的实现(C)

    //线性表的顺序存储 #include <stdio.h>typedef int DataType;#define MaxSize 15//定义顺序表typedef struct { Da ...

  6. 线性表的顺序存储和链式存储c语言实现

    一.线性表的顺序存储 typedef int ElemType;typedef struct List { ElemType *data;//动态分配 ,需要申请空间 int length; }Lis ...

  7. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...

  8. [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)

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

  9. [置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)

    栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表.栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据.栈 ...

随机推荐

  1. cxf调用c#的webservice

    java调用c#的webservice,如今已经測试通过.并且用到了项目中. 如今把实现方式和遇到的问题分享给大家.详细源代码例如以下: JaxWsDynamicClientFactory dcf = ...

  2. 问题在哪?动态菜单条-------Day86

    今天做了一个动态菜单条,先上图片,简单说一下我想实现的效果: 就是以下这个地方,随着鼠标指到哪,它就划到哪,并有一个惯性的幅度,并且滑动距离越远,停住的时候惯性越大,摆动幅度越大,这就是我大概想实现的 ...

  3. java--多线程之Runnable

    引读: 上一篇博文中讲了Thread的继承,存在一个问题就是,如果类本身已经继承了某个父类,又要继承Thread,导致多重继承. [但是我们知道接口是实现多重继承的重要方式].java提供了Runna ...

  4. SQL之单行函数

    单行函数语法: function name(column|expression,[arg1,arg2,...]) 参数说明: function name:函数名称 column:数据库列名 expre ...

  5. 基于二叉树和数组实现限制长度的最优Huffman编码

    具体介绍详见上篇博客:基于二叉树和双向链表实现限制长度的最优Huffman编码 基于数组和基于链表的实现方式在效率上有明显区别: 编码256个符号,符号权重为1...256,限制长度为16,循环编码1 ...

  6. (step6.1.5)hdu 1233(还是畅通工程——最小生成树)

    题目大意:输入一个整数n,表示有n个村庄,在接下来的n*(n-1)/2中,每行有3个整数beigin.end.weight,分别表示路的起始村庄,结束村庄和村庄之间的距离. 求索要修的路的最短距离 解 ...

  7. StreamWrite-StreamRead 读写文本文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Eclipse用法和技巧十八:减少不必要的输入

    写代码的时候,很多人都有一个原则,尽量上输入.依靠IDE自动生成的代码,一般可读性,排版什么的都还是不错的,最主要的一般不会有什么低级错误.今天介绍几个在eclipse环境中,常用的依靠eclipse ...

  9. javascript笔记整理(数据类型强制/隐式转换 )

    A.数据类型强制转换 1.转换为数值类型 Number(参数) 把任何的类型转换为数值类型 A.如果是布尔值,false为0,true为1 var a=false;alert(Number(a)); ...

  10. WCF技术剖析之十九:深度剖析消息编码(Encoding)实现(下篇)

    原文:WCF技术剖析之十九:深度剖析消息编码(Encoding)实现(下篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话 ...