c++ 简单实现优先级队列

优先级队列:

 

 

优先级队列 是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。

 

 

#include<iostream>

using namespace std;

 

template<class T>

struct Node{

       T     date;    //元素

       Node<T>* next;

       int   Index;    //优先数值,由该值来决定优先级,值大的优先级高

       Node():date(T()),next(NULL),Index(0){}

 

};  //节点实现队列

 

 

template<class T>

class Priority_Queue

{

public:

       Priority_Queue()     //初始化队列,初始化一个节点,但不用来存值

       {

              front=rear=newNode<T>;

              front->date=T();

              rear->next=NULL;

              front->Index=0;

              count=0;

       }

 

       booladd(const T &e ,int index);  //添加元素

       booldel(T & e);   //出队

       int  size();

       boolempty();

       boolclear();

       voiddisplay();

       ~Priority_Queue()

       {

              this->clear();

       }

 

private:

       Node<T>* front;   //头指针

       Node<T>* rear;    //尾指针

       intcount;          //元素的个数

};

 

/*

 入队

 constT &e :为要入队的元素值

 intindex: 为优先级数值,先给一个优先级数值来决定在队列中的位置

 入队时判断元素的优先级并确定元素在队列中的位置

*/

template<class T>

bool Priority_Queue<T>::add(const T&e ,int index)

{

       Node<T>* p=new Node<T>;

       p->date=e;

       p->Index=index;

       if(index>front->Index)

       {

              p->next=front;

              front=p;

              count++;

      

              returntrue;

       }

       else

       {

              if(front->next==rear)

              {

                     front->next=p;

                     p->next=rear;

             

              }

              else

              {

 

             

                  Node<T>*q=front;

                  while(index<=q->next->Index)

                     {

                        q=q->next;

                    

                     }

                     p->next=q->next;

                     q->next=p;

 

              }

              count++;

      

              returntrue;

 

       }

}

/*

出队

T &e:出队的元素

出队时不用判断元素的优先级,直接从front出队,因为入队时已按优先级入队

*/

template<class T>

bool Priority_Queue<T>::del(T & e)

{

       if(front==rear)

       {

              cout<<"队列为空!,del失败!"<<endl;

       }

       e=front->date;

       Node<T>* p=front;

       front=p->next;

       deletep;

       count--;

       returntrue;

 

}

 

template<class T>

int Priority_Queue<T>::size()

{

       returncount;

}

 

template<class T>

bool Priority_Queue<T>::empty()

{

       if(front==rear)

              returntrue;

       else

              returnfalse;

}

 

template<class T>

bool Priority_Queue<T>::clear()

{

       Node<T>*p=front;

       Node<T>*q=NULL;

       while(p)

       {

              q=p;

              deleteq;

              p=p->next;

             

 

       }

 

       returntrue;

}

 

template<class T>

void Priority_Queue<T>::display()

{

       if(empty())

       {

              cout<<"队列为空,display失败!"<<endl;

              return;

       }

       else

       {

              Node<T>* p=front;

              cout<<"优先顺序:"<<endl<<"  ";

              Node<T>*q=NULL;

              while(p->next)

              {

                     q=p;

                     p=p->next;

                     cout<<q->date<<",";

              }

              cout<<endl;

 

       }

      

}

 

 

int main()

{

       Priority_Queue<int>q;

       q.add(10,4);

       q.add(8,6);

       q.add(7,5);

       q.display();

 

       inte;

       q.del(e);

       cout<<"出队:"<<e<<endl;

       q.display();

       cout<<"队列长度:"<<q.size()<<endl;

//     q.clear();

 

      

       return0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值