[置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(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)(十二)的更多相关文章
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 数据结构导论 四 线性表的顺序存储VS链式存储
前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...
- 线性表的Java实现--链式存储(单向链表)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始. 链式存储结构的线性表将采用一组任意的存储单元存放线性表中的数据元素.由于不需要按顺序存储,链表在 ...
- 线性表的顺序存储和链式存储的实现(C)
//线性表的顺序存储 #include <stdio.h>typedef int DataType;#define MaxSize 15//定义顺序表typedef struct { Da ...
- 线性表的顺序存储和链式存储c语言实现
一.线性表的顺序存储 typedef int ElemType;typedef struct List { ElemType *data;//动态分配 ,需要申请空间 int length; }Lis ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...
- [置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)
栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表.栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据.栈 ...
随机推荐
- UNIX环境高级编程——TCP/IP网络编程
常用网络信息检索函数 gethostname() getppername() getsockname() gethostbyname() gethostbyaddr() getprotobyname( ...
- android软键盘弹出隐藏的监听
通过网上搜索关于软键盘的隐藏弹出的监听,有几种方式,其中最有效的方式是在View的Onlayout()里面做文章 具体代码: 将布局视图自定义,重写onlayout()方法,然后在主Activity里 ...
- Android开发--CardView使用
Android5.0中向我们介绍了一个全新的控件–CardView,从本质上看,可以将CardView看做是FrameLayout在自身之上添加了圆角和阴影效果.请注意:CardView被包装为一种布 ...
- three.js 源代码凝视(十)Math/Line3.js
商域无疆 (http://blog.csdn.net/omni360/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:商域无疆 - 本博客专注于 敏捷开发 ...
- delphi高手突破学习笔记之面向对象类和对象的本质(有汇编解释 good)
知识点1:堆和栈 每个应用程序可以获得的内存空间分为两种:堆(heap)和栈(stack). 堆又称为“自由存储区”,其中的内存空间的分配与释放是必须由程序员来控制的.例如,用GetMem函数获取了一 ...
- Windows Phone 9再见了!
因为需要准备注册电气工程师考试,因此Windows Phone 8初学者教程的翻译只能就此打住了,在考完后,也许就是Windows Phone 9发布的时候还会回来! Bye bye!
- 有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M。 需要选出若干个x,使这几个x的和与 M 最接近。 请描述实现算法,并指出算法复杂度
题目:有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. 代码如下: #in ...
- 组件接口(API)设计指南[4]-通知(Notifications)
*返回文件夹阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 通知(Notifications) 通知是托付协议的还有一半 ...
- HDOJ 1800 Flying to the Mars 盲目搜索......................so easy...........
check the original problem here:http://acm.hdu.edu.cn/showproblem.php?pid=1800 the AC code: #include ...
- AngelHack China 2013 招组队成员
AngelHack China 2013 connect me