数据结构--队列
队列只可以在一端入队(/出队)在另一端出队(/入队),没有插队行为。
普通队列:
存在的缺点:出队时,如果指针一直指向对头,处理起来就会效率变低,如果指针一直后移,就会造成前面的位置浪费。
环形队列:
克服普通队列的缺点。环形队列,排队有方向的。
队列用途:
自动排号机
C++实现队列设计
====================
class MyQueue{
public:
MyQueue(int queueCapacity);
virtual ~MyQueue();
void ClearQueue();
bool QueueEmpty() const;
bool QueueFull() const;
int QueueLength() const;
bool EnQueue(int element);
bool DeQueue(int &element);
void QueueTraverse();
private:
int *m_pQueue;
int m_iQueuelen;
int m_iQueueCapacity;
int m_iHead;
int m_iTail;
};
===================
#include"MyQueue.h"
#include<iostream>
using namespace std;
MyQueue:: MyQueue(int queueCapacity){
m_iQueueCapacity=queueCapacity;
m_iHead=0;
m_iTail=0;
m_pQueue=new int[m_iQueueCapacity];
m_iQueuelen=0;
}
MyQueue::~MyQueue(){
delete []m_pQueue;
m_pQueue = NULL;
}
void MyQueue::ClearQueue(){
m_iHead=0;
m_iTail=0;
m_iQueuelen=0;
}
bool MyQueue::QueueEmpty() const{
if(m_iQueuelen==0){
return true;
}else{
return false;
}
}
int MyQueue::QueueLength()const{
return m_iQueuelen;
}
bool MyQueue::QueueFull() const{
if(m_iQueuelen==m_iQueueCapacity){
return true;
}else{
return false;
}
}
bool MyQueue::EnQueue(int element){
if(QueueFull()){
return false;
}
else{
m_pQueue[m_iTail]=element;
m_iTail++;
m_iTail=m_iTail%m_iQueueCapacity;
m_iQueuelen++;
return true;
}
}
bool MyQueue::DeQueue(int &element){
if(QueueEmpty()){
return false;
}
else{
element=m_pQueue[m_iHead];
m_iHead++;
m_iTail=m_iTail%m_iQueueCapacity;
m_iQueuelen--;
return true;
}
}
void MyQueue::QueueTraverse(){
for(int i=m_iHead;i<m_iQueuelen+m_iHead;i++){
cout<<m_pQueue[i%m_iQueueCapacity]<<endl;
}
}
========================`
#include<iostream>
#include"MyQueue.h"
using namespace std;
int main(){
MyQueue *p=new MyQueue(3);
cout<<"插入"<<endl;
p->EnQueue(1);
p->EnQueue(2);
p->EnQueue(3);
cout<<"遍历"<<endl;
p->QueueTraverse();
cout<<"移除"<<endl;
int e=0;
p->DeQueue(e);
cout<<e<<endl;
cout<<"遍历"<<endl;
p->QueueTraverse();
cout<<"清除"<<endl;
p->ClearQueue();
cout<<"清除以后"<<endl;
cout<<"遍历"<<endl;
p->QueueTraverse();
cout<<"重新入队列"<<endl;
p->EnQueue(20);
cout<<"遍历"<<endl;
p->QueueTraverse();
delete p;
p=NULL;
return 0;
}
练习:
==============================
#include"Customer.h"
class MyQueue{
public:
MyQueue(int queueCapacity);
virtual ~MyQueue();
void ClearQueue();
bool QueueEmpty() const;
bool QueueFull() const;
int QueueLength() const;
bool EnQueue(Customer element);
bool DeQueue(Customer &element);
void QueueTraverse();
private:
Customer *m_pQueue;
int m_iQueuelen;
int m_iQueueCapacity;
int m_iHead;
int m_iTail;
};
================================
#include"MyQueue.h"` `#include<iostream>` `using namespace std;` `MyQueue:: MyQueue(int queueCapacity){` `m_iQueueCapacity=queueCapacity;` `m_iHead=0;` `m_iTail=0;` `m_pQueue=new Customer[m_iQueueCapacity];` `m_iQueuelen=0;` `}` `MyQueue::~MyQueue(){` `delete []m_pQueue;` `m_pQueue = NULL;` `}` `void MyQueue::ClearQueue(){` `m_iHead=0;` `m_iTail=0;` `m_iQueuelen=0;` `}` `bool MyQueue::QueueEmpty() const{` `if(m_iQueuelen==0){` `return true;` `}else{` `return false;` `}` `}` `int MyQueue::QueueLength()const{` `return m_iQueuelen;` `}` `bool MyQueue::QueueFull() const{` `if(m_iQueuelen==m_iQueueCapacity){` `return true;` `}else{` `return false;` `}` `}` `bool MyQueue::EnQueue(Customer element){` `if(QueueFull()){` `return false;` `}` `else{` `m_pQueue[m_iTail]=element;` `m_iTail++;` `m_iTail=m_iTail%m_iQueueCapacity;` `m_iQueuelen++;` `return true;` `}` `}` `bool MyQueue::DeQueue(Customer &element){` `if(QueueEmpty()){` `return false;` `}` `else{` `element=m_pQueue[m_iHead];` `m_iHead++;` `m_iTail=m_iTail%m_iQueueCapacity;` `m_iQueuelen--;` `return true;` `}` `}` `void MyQueue::QueueTraverse(){` `for(int i=m_iHead;i<m_iQueuelen+m_iHead;i++){` `m_pQueue[i%m_iQueueCapacity].printInfo();` `}` `}
=============================
#include<string>
using namespace std;
class Customer{
public:
Customer(string name="",int age=0);
void printInfo() const;
private:
string m_strName;
int m_iAge;
``
};
===============================
#include<iostream>
#include"Customer.h"
using namespace std;
Customer::Customer(string name,int age){
m_strName=name;
m_iAge=age;
}
void Customer::printInfo() const{
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
}
==========================
#include<iostream>
#include"MyQueue.h"
using namespace std;
int main(){
MyQueue *p=new MyQueue(3);
Customer c1("wangjiaer",26);
Customer c2("duanyien",27);
p->EnQueue(c1);
p->EnQueue(c2);
p->QueueTraverse();
Customer c4("",0);
p->DeQueue(c4);
p->QueueTraverse();
return 0;
}