1.利用数组实现优先队列;
2.利用链表实现优先队列;
(注:默认以最大值为优先级最高,思考:如何实现用户指定优先级(最大优先或最小优先))
=====================================
1. 源代码
---------------------------------------------------------
queue.h
---------------------------------------------------------
#include <iostream>
using namespace std;
template <typename T>
class bqueue
{
public:
bqueue(const T& s);
bool empty() const;
bool full() const;
void push(const T& item);
void pop(int a);
T& front(int a) ;
int size() const;
private:
T *queueArray;
int qfront, qback; //qfront指向第一个有效元素 qback指向尾部最后一个元素的
int count;
int MAXQSIZE;
};
template <typename T>
bqueue<T>::bqueue(const T& s){
MAXQSIZE=s;
queueArray=new int[MAXQSIZE];
qfront=0;
qback=0;
count=0;
}
template <typename T>
bool bqueue<T>::empty() const
{
return (count == 0);
}
template <typename T>
bool bqueue<T>::full() const
{
return (count == MAXQSIZE);
}
template <typename T>
void bqueue<T>::push(const T& item)
{
if (count != MAXQSIZE){
queueArray[qback] = item;
}else{
cerr<<"bqueue push(): full queue";
exit(1);
}
qback = (qback+1) % MAXQSIZE;
count++;
}
template <typename T>
void bqueue<T>::pop(int a)
{
T m=queueArray[qfront];
int i,j,n;
int x=qfront;
int y;
if (count == 0){
cerr<<"bqueue pop(): empty queue";
exit(1);
}
switch (a)
{
case 0: for(i=0;i<count;i++){
if(queueArray[x]>=m){
m = queueArray[x];
n=x;
}
x=(x+1) % MAXQSIZE;
}
break;
case 1: for(i=0;i<count;i++){
if(queueArray[x]<=m){
m = queueArray[x];
n=x;
}
x=(x+1) % MAXQSIZE;
}
break;
}
if(n==qfront){
qfront = (qfront+1) % MAXQSIZE;
}else{
y=n;
while(y != qback){
queueArray[y]=queueArray[(y+1) % MAXQSIZE];
y=(y+1) % MAXQSIZE;
}
qback = (qback+MAXQSIZE-1) % MAXQSIZE;
}
count--;
}
template <typename T>
T& bqueue<T>::front(int a)
{
T m=queueArray[qfront];
int x=qfront;
if (count == 0){
cerr<<"bqueue front(): empty queue";
exit(1);
}
switch (a)
{
case 0: for(i=0;i<count;i++){
if(queueArray[x]>=m){
m = queueArray[x];
}
x=(x+1) % MAXQSIZE;
}
break;
case 1: for(i=0;i<count;i++){
if(queueArray[x]<=m){
m = queueArray[x];
}
x=(x+1) % MAXQSIZE;
}
break;
}
return m;
}
template <typename T>
int bqueue<T>::size() const
{
return count;
}
---------------------------------------------------------
queueArray.cpp
---------------------------------------------------------
#include "queue.h"
#include "random.h"
int main(int argc, char* argv[])
{
int i,x,n,y;
cout<<"input the capacity of queue: ";
cin>>n;
bqueue<int> q(n);
bqueue<int> p(n);
cout<<"最大优先:"<<endl;
for(i=0;i<n;i++){
x=random(100);
q.push(x);
}
while(!q.empty()){
y=q.front(0);
cout<<y<<" ";
p.push(y);
q.pop(0);
}
cout<<endl<<"最小优先:"<<endl;
while(!p.empty()){
cout<<p.front(1)<<" ";
p.pop(1);
}
cout<<endl;
return 0;
}
1. 源代码
---------------------------------------------------------
node.h
---------------------------------------------------------
template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list
node() : next(NULL) {};
node(const T& item, node<T> *nextNode = NULL) : nodeValue(item), next(nextNode){};
};
---------------------------------------------------------
nodeQueue.h
---------------------------------------------------------
#include "node.h"
template <typename T>
class nodeQueue
{
public:
nodeQueue();
bool empty() const;
void push(const T& item);
void pop(int a);
const T& front(int a) const;
int size() const;
private:
node<T> *qfront,*qback;
int count;
};
template <typename T>
nodeQueue<T>::nodeQueue():qfront(NULL), qback(NULL), count(0){}
template <typename T>
bool nodeQueue<T>::empty() const
{
return count==0;
}
template <typename T>
void nodeQueue<T>::push(const T& item)
{
node<T> *newNode=new node<int>(item, NULL);
if(qfront==NULL){
qfront=newNode;
qback=newNode;
}
else{
qback->next=newNode;
qback=newNode;
}
count++;
}
template <typename T>
void nodeQueue<T>::pop(int a)
{
T m=qfront->nodeValue;
if (count == 0){
cerr<<"nodeQueue pop(): empty queue"<<endl;
}
node<T> *p=new node <int>(NULL);
node<T> *q=new node <int>(NULL);
node<T> *prew=new node <int>(NULL);
node<T> *curr=new node <int>(NULL);
p=qfront;
prew=qfront;
curr=qfront;
switch (a)
{
case 0: while(p!=NULL){
if(p->nodeValue >= m){
m=p->nodeValue;
q=p;
}
p=p->next;
}
break;
case 1: while(p!=NULL){
if(p->nodeValue <= m){
m=p->nodeValue;
q=p;
}
p=p->next;
}
break;
}
if(q==qfront){
curr=qfront;
qfront=curr->next;
delete curr;
}else{
while(curr!=NULL){
if(curr==q){
prew->next=curr->next;
delete curr;
break;
}
prew=curr;
curr=curr->next;
}
}
count--;
}
template <typename T>
const T& nodeQueue<T>::front(int a) const
{
T m=qfront->nodeValue;
if (count == 0){
cerr<<"nodeQueue front(): empty queue"<<endl;
}
node<T> *p=new node <int>(NULL);
p=qfront;
switch (a)
{
case 0: while(p!=NULL){
if(p->nodeValue >= m){
m=p->nodeValue;
}
p=p->next;
}
break;
case 1: while(p!=NULL){
if(p->nodeValue <= m){
m=p->nodeValue;
}
p=p->next;
}
break;
}
return m;
}
template <typename T>
int nodeQueue<T>::size() const
{
return count;
}
---------------------------------------------------------
nodeQueue.cpp
---------------------------------------------------------
#include <iostream>
using namespace std;
#include "nodeQueue.h"
#include "random.h"
int main(int argc, char* argv[])
{
int i,x,y;
nodeQueue<int> q;
nodeQueue<int> p;
for(i=0;i<10;i++){
x=random(100);
q.push(x);
}
cout<<"最大优先:"<<endl;
while(!q.empty()){
y=q.front(0);
cout<<y<<" ";
p.push(y);
q.pop(0);
}
cout<<endl<<"最小优先:"<<endl;
while(!p.empty()){
cout<<p.front(1)<<" ";
p.pop(1);
}
cout<<endl;
return 0;
}