一.理论部分不多叙述,具体可参考慕课网数据结构之队列篇(环形队列)。本篇博客住要从三个方面进行编写:
1.基础的队列,int元素作为队列的元素。
2.建立类,将类作为队列的元素。
3.建立队列模板类,该类可适应多种类型的队列元素。
(本博客主要作为自己学习以及复习所用,欢迎各位补充和指正!)
二.实现方法
1.首先最简单的队列方法:
Myqueue.h
#ifndef Myqueue_H_
#define Myqueue_H_
class Myqueue
{
public:
Myqueue(int queueCapacity);
virtual ~Myqueue();
void ClearQueue();
bool QueueEmpty() const;
int QueueLength() const;
bool EnQueue(int element);
bool DeQueue(int &element);
bool QueueFull() const;
void QueueTraverse();
private:
int *m_pQueue;
int m_iQueueLen;
int m_iQueueCapacity;
int m_iTail;
int m_iHead;
};
#endif
Myqueue.cpp文件
#include
#include"Myqueue.h"
using namespace std;
Myqueue::Myqueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new int[m_iQueueCapacity];
ClearQueue();
}
Myqueue:: ~Myqueue()
{
delete m_pQueue;
m_pQueue = NULL;
}
void Myqueue::ClearQueue()
{
m_iTail = 0;
m_iHead = 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::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_iHead = m_iHead%m_iQueueCapacity;
m_iQueueLen--;
return true;
}
}
bool Myqueue::QueueFull() const
{
if (m_iQueueLen == m_iQueueCapacity)
{
return true;
}
else
{
return false;
}
}
void Myqueue::QueueTraverse()
{
for (int i = m_iHead; i < m_iHead + m_iQueueLen; i++)
{
cout<
测试文件Queue_demo:
#include
#include"Myqueue.h"
using namespace std;
int main()
{
Myqueue *p = new Myqueue(4);
p->EnQueue(1);
p->EnQueue(2);
p->EnQueue(3);
p->EnQueue(4);
p->EnQueue(5);
p->QueueTraverse();
cout << endl;
int e = 0;
p->DeQueue(e);
cout << e << endl;
cout << endl;
p->QueueTraverse();
system("pause");
return 0;
}
2.此时使用customer类作为队列的元素,将原先的对象类型改为customer类
Myqueue.h:
#ifndef Myqueue_H_
#define Myqueue_H_
#include"Customer.h"
class Myqueue
{
public:
Myqueue(int queueCapacity);
virtual ~Myqueue();
void ClearQueue();
bool QueueEmpty() const;
int QueueLength() const;
bool EnQueue(Customer element);
bool DeQueue(Customer &element);
bool QueueFull() const;
void QueueTraverse();
private:
Customer *m_pQueue;//将其改为Customer类指针,其他不变
int m_iQueueLen;
int m_iQueueCapacity;
int m_iTail;
int m_iHead;
};
#endif
Myqueue.cpp
#include
#include"Myqueue.h"
using namespace std;
Myqueue::Myqueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new Customer[m_iQueueCapacity];
ClearQueue();
}
Myqueue:: ~Myqueue()
{
delete m_pQueue;
m_pQueue = NULL;
}
void Myqueue::ClearQueue()
{
m_iTail = 0;
m_iHead = 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::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_iHead = m_iHead%m_iQueueCapacity;
m_iQueueLen--;
return true;
}
}
bool Myqueue::QueueFull() const
{
if (m_iQueueLen == m_iQueueCapacity)
{
return true;
}
else
{
return false;
}
}
void Myqueue::QueueTraverse()
{
for (int i = m_iHead; i < m_iHead + m_iQueueLen; i++)
{
cout << m_pQueue[i] << endl;
}
}
Customer.h:
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
#include
#include
using namespace std;
class Customer
{
public:
Customer(string name="fei", int age=1);//默认构造函数,对应于m_pQueue指针内存的申请
friend ostream & operator<<(ostream &os, const Customer &customer );//重载输出符号
private:
int m_iage;
string m_strname;
};
#endif
Customer.cpp
#include
#include
#include"Customer.h"
using namespace std;
Customer::Customer(string name, int age)
{
m_strname = name;
m_iage = age;
}
ostream & operator<<(ostream &os, const Customer &customer)
{
os << "name:"<
queue_demo.cpp:
#include
#include"Myqueue.h"
#include"Customer.h"
using namespace std;
int main()
{
Myqueue *p = new Myqueue(5);
Customer customer1("fei", 21);//实例化对象
Customer customer2("yan", 21);
Customer customer3("jia", 21);
Customer customer4("good", 21);
Customer customer5("boy", 21);
p->EnQueue(customer1);
p->EnQueue(customer2);
p->EnQueue(customer3);
p->EnQueue(customer4);
p->EnQueue(customer5);
cout << "遍历" << endl;
p->QueueTraverse();
cout << endl;
cout << "出队列" << endl;
Customer e;
p->DeQueue(e);
cout << e << endl;
cout << endl;
cout << "再次遍历" << endl;
p->QueueTraverse();
system("pause");
return 0;
}
3.建立队列模板类,使得该类可适应多种类型的元素
customer类与上诉相同,在编译时将Myqueue.cpp文件放入Myqueue.h 文件中一同编译。
Myqueue.h:
#ifndef Myqueue_H_
#define Myqueue_H_
#include"Customer.h"
template
class Myqueue
{
public:
Myqueue(int queueCapacity);
virtual ~Myqueue();
void ClearQueue();
bool QueueEmpty() const;
int QueueLength() const;
bool EnQueue(T element);
bool DeQueue(T &element);
bool QueueFull() const;
void QueueTraverse();
private:
T *m_pQueue;//将其改为Customer类指针,其他不变
int m_iQueueLen;
int m_iQueueCapacity;
int m_iTail;
int m_iHead;
};
template
Myqueue::Myqueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new T[m_iQueueCapacity];
ClearQueue();
};
template
Myqueue:: ~Myqueue()
{
delete m_pQueue;
m_pQueue = NULL;
};
template
void Myqueue::ClearQueue()
{
m_iTail = 0;
m_iHead = 0;
m_iQueueLen = 0;
};
template
bool Myqueue::QueueEmpty() const
{
if (m_iQueueLen == 0)
return true;
else
return false;
};
template
int Myqueue::QueueLength() const
{
return m_iQueueLen;
};
template
bool Myqueue::EnQueue(T element)
{
if (QueueFull())
{
return false;
}
else
{
m_pQueue[m_iTail] = element;
m_iTail++;
m_iTail = m_iTail%m_iQueueCapacity;
m_iQueueLen++;
return true;
}
};
template
bool Myqueue::DeQueue(T &element)
{
if (QueueEmpty())
{
return false;
}
else
{
element = m_pQueue[m_iHead];
m_iHead++;
m_iHead = m_iHead%m_iQueueCapacity;
m_iQueueLen--;
return true;
}
};
template
bool Myqueue::QueueFull() const
{
if (m_iQueueLen == m_iQueueCapacity)
{
return true;
}
else
{
return false;
}
};
template
void Myqueue::QueueTraverse()
{
for (int i = m_iHead; i < m_iHead + m_iQueueLen; i++)
{
cout << m_pQueue[i] << endl;
}
};
#endif
queue_demo.cpp:
#include
#include"Myqueue.h"
#include"Customer.h"
using namespace std;
int main()
{
cout << "使用类实例化模板:" << endl;
Myqueue *p = new Myqueue(5);
Customer customer1("fei", 21);//实例化对象
Customer customer2("yan", 21);
Customer customer3("jia", 21);
Customer customer4("good", 21);
Customer customer5("boy", 21);
p->EnQueue(customer1);
p->EnQueue(customer2);
p->EnQueue(customer3);
p->EnQueue(customer4);
p->EnQueue(customer5);
cout << "遍历" << endl;
p->QueueTraverse();
cout << endl;
cout << "出队列" << endl;
Customer e;
p->DeQueue(e);
cout << e << endl;
cout << endl;
cout << "再次遍历" << endl;
p->QueueTraverse();
cout << "使用字符类型实例化" << endl;
p->ClearQueue();
Myqueue *pp = new Myqueue(5);
pp->EnQueue('f');
pp->EnQueue('y');
pp->EnQueue('j');
pp->EnQueue('g');
pp->EnQueue('b');
cout << "遍历" << endl;
pp->QueueTraverse();
cout << endl;
cout << "出队列" << endl;
char ee;
pp->DeQueue(ee);
cout << ee << endl;
cout << endl;
cout << "再次遍历" << endl;
pp->QueueTraverse();
system("pause");
return 0;
}
实验结果如下: