利用数组和链表实现优先队列
来源:互联网 发布:龙岗做网络推广公司 编辑:程序博客网 时间:2024/04/26 12:49
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;
}
- 利用数组和链表实现优先队列
- 优先队列的数组实现
- 有序数组实现优先队列
- 优先队列C++数组实现
- 利用无序数组实现优先队列并排序数组——ADT实现
- 利用顺序表实现优先队列的插入和删除操作
- 队列的数组和链表实现
- 队列01:数组和链表实现
- 利用队列实现广度优先搜索
- 利用优先队列实现的dijkstra算法
- 利用堆实现堆排序&优先队列
- 利用堆实现堆排序&优先队列
- 优先队列(利用二叉堆实现)
- 利用优先队列PriorityQueue实现Prim算法
- java 实现数组队列和链表队列
- 优先队列(链表实现)
- 无序链表实现优先队列
- 优先队列的链表实现
- 递归面试题汇总
- 凸包——Andrew(安德鲁)算法
- Struts 2开发基本流程
- 天下会-Google系列之从Google搜获更多
- Spring IoC容器构建过程分析(二)(草稿,持续整理中)
- 利用数组和链表实现优先队列
- 【操作系统】进程间通信
- Cocos2d-x 3.1.1 学习日志5--cocos2d-x3.1.1打飞机的实现
- OCP 1Z0 053 138
- 黑马程序员-分类以及类的本质研究
- 多项式合并: F(x)=8x7+7x4+3x2+x+2x-2+8x-3 G(x)=9x6+2x5+5x4-x+5
- Remove Nth Node From End of List
- jpa pageutil
- LDAP基础