像栈一样,队列(queue)也是一种表,与栈不同的是,队列的进队(enQueue)是在尾部(rear)进行,队列的出队(deQueue)是在头部(front)进行。
队列的实现:1)队列的数组实现;2)队列的链表表实现;
循环队列:解决队列空指针和内存空置问题,提高空间利用率;
判断为空: front == rear
判断队满:front == (rear + 1) % maxSize
获取大小:(rear - front + maxSize)% maxSize
队列的应用:1)常见的排队算法;2)打印机打印文字;3)计算机网络请求等;4)图论算法
实例:使用数组和链表实现循环队列(注:用单链表表示的链式队列特别适合于数据元素变动较大的情形,而且不存在溢出的情况)
参考链接:循环队列和链式队列(C++实现)
1、seqQueue.h
//seqQueue.h
#ifndef SEQQUEUE_H_
#define SEQQUEUE_H_
#include<iostream>
template<class dataType>
class SeqQueue{
private:
dataType* data;
int front, rear;
int maxSize;
public:
SeqQueue(int max = 10);
~SeqQueue(){delete[] data;};
bool deQueue(dataType &d);
bool enQueue(const dataType &d);
void clear(){front = rear = 0;};
bool isEmpty() const{return front == rear;};
bool isFull() const{return front == (rear + 1) % maxSize;};
int getSize() const{return (rear - front + maxSize) % maxSize ;};
};
//SeqQueue constructor
template<class dataType>
SeqQueue<dataType>::SeqQueue(int max){
data = new dataType[max];
front = rear = 0;
maxSize = max;
}
//deQueue the data
template<class dataType>
bool SeqQueue<dataType>::deQueue(dataType &d){
if(isEmpty()){
return false;
}
else{
d = data[front];
front = (front + 1) % maxSize;
return true;
}
}
//enQueue the data
template<class dataType>
bool SeqQueue<dataType>::enQueue(const dataType &d){
if(isFull()){
return false;
}
else{
data[rear] = d;
rear = (rear + 1) % maxSize;
return true;
}
}
#endif
2、linkQueue.h
//linkQueue.h
#ifndef LINKQUEUE_H_
#define LINKQUEUE_H_
#include<iostream>
template<class dataType>
struct Node{
dataType data;
Node<dataType>* next;
Node(dataType &d, Node<dataType> *n = NULL):data(d), next(n){};
};
template<class dataType>
class LinkQueue{
private:
Node<dataType> *front, *rear;
public:
LinkQueue():front(NULL), rear(NULL){};
~LinkQueue(){clear();};
void clear();
int getSize();
bool isEmpty();
bool deQueue(dataType &d);
bool enQueue(dataType &d);
};
//clear the Node data
template<class dataType>
void LinkQueue<dataType>::clear(){
Node<dataType> *temp;
while(front != NULL){
temp = front;
front = front -> next;
delete front;
}
}
//get size of LinkQueue
template<class dataType>
int LinkQueue<dataType>::getSize(){
int num = 0;
Node<dataType> *temp = front;
while(temp != NULL){
num++;
temp = temp -> next;
}
return num;
}
//judge whether the LinkQueue is empty
template<class dataType>
bool LinkQueue<dataType>::isEmpty(){
return front == NULL;
}
//deQueue the data
template<class dataType>
bool LinkQueue<dataType>::deQueue(dataType &d){
if(isEmpty()){
return false;
}
Node<dataType> *temp = front;
d = front -> data;
front = front -> next;
delete temp;
return true;
}
//enQueue the data
template<class dataType>
bool LinkQueue<dataType>::enQueue(dataType &d){
if(front == NULL){
front = rear = new Node<dataType>(d);
}
else{
rear = rear -> next = new Node<dataType>(d);
}
return true;
}
#endif
3、main.cpp
//main.cpp
#include<iostream>
#include<cstdlib>
#include"seqQueue.h"
//#include"linkQueue.h"
using namespace std;
void menu(){
cout << "************ *MENU* *************" << endl;
cout << "********* *1 enQueue* ***********" << endl;
cout << "********* *2 deQueue* ***********" << endl;
cout << "********* *3 getSize* ***********" << endl;
cout << "********* *4 clear* *************" << endl;
cout << "********* *5 exit* **************" << endl;
}
//void excute(int num, LinkQueue<int> *q)
void excute(int num, SeqQueue<int> *q){
switch(num){
int temp;
case 1 :
cout << "Enter the enQueue number : ";
cin >> temp;
q -> enQueue(temp);
cout << temp << " has been enQueued " << endl;
break;
case 2 :
q -> deQueue(temp);
cout << "deQueue the number into temp : " << temp << endl;
break;
case 3 :
cout << "the queue size is : " << q -> getSize() << endl;
break;
case 4 :
cout << "I have clear the queue " << endl;
break;
case 5 :
cout << "I will exit the program !" << endl;
exit(1);
default:
cout << "only 1 ~ 5 is avaible, Wrong operation !" << endl;
exit(1);
}
}
int main()
{
//LinkQueue<int> *queue = new LinkQueue<int>
SeqQueue<int> *queue = new SeqQueue<int>;
menu();
int temp;
while(true){
cout << "choise your operation : ";
cin >> temp;
cin.get();
excute(temp, queue);
}
cout << "done . " << endl;
delete[] queue;
return 0;
}
practice makes perfect!