#include<iostream>
using namespace std;
template <class T>
class SeqQueue {
protected :
int rear, front; //队尾和队头指针
T* elements;
int maxSize;
public:
SeqQueue(int sz);
~SeqQueue() { delete[]elements; }
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x);
void makeEmpty() { front = rear = -1; }
bool IsEmpty()const { return front == rear; }
bool IsFull()const {
return (rear == maxSize - 1);
}
int getSize()const
{
return rear - front;
}
};
template <class T> //构造函数
SeqQueue<T>::SeqQueue(int sz)
{
maxSize = 100;
while (sz > maxSize)
{
cout << "范围超过,请重新输入" << endl;
cin >> sz;
}
elements = new T[sz];
rear = front=-1;
}
template <class T>// 入队
bool SeqQueue<T>::EnQueue(const T& x)
{
if (rear == maxSize - 1)
{
cout << < "overflow" << endl;
}
else
{
rear++;
elements[rear] = x; //填入新元素
return true;
}
}
template <class T>// 出队
bool SeqQueue<T>::DeQueue(T& x)
{
if (front == rear)
{
cout << "underflow";
return false;
}
else
{
front++;
x = elements[front];
return true;
}
}
template <class T>// 出队
bool SeqQueue<T>::getFront(T& x)
{
if (front == rear)
{
cout << "empty" << endl;
}
else
{
x = elements[front];
return true;
}
}
循环队列
#include<iostream>
using namespace std;
//循环队列
template <class T>
class SeqQueue {
protected :
int rear, front; //队尾和队头指针
T* elements;
int maxSize;
public:
SeqQueue(int sz);
~SeqQueue() { delete[]elements; }
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x);
void makeEmpty() { front = rear = -1; }
bool IsEmpty()const { return front == rear; }
bool IsFull()const {
return ((rear+1)%maxSize== front);
}
int getSize()const
{
return (rear - front+maxSize)%maxSize;
}
};
//循环队列
template <class T> //构造函数
SeqQueue<T>::SeqQueue(int sz)
{
maxSize = 100;
while (sz > maxSize)
{
cout << "范围超过,请重新输入" << endl;
cin >> sz;
}
elements = new T[sz];
rear = front=0; //循环队列,初始位置为0,front占一个空间。
}
template <class T>// 入队
bool SeqQueue<T>::EnQueue(const T& x)
{
if ((rear+1)%maxSize ==front )
{
cout << < "overflow" << endl;
}
else
{
rear= (rear + 1) % maxSize;
elements[rear] = x; //填入新元素
return true;
}
}
template <class T>// 出队
bool SeqQueue<T>::DeQueue(T& x)
{
if (front == rear)
{
cout << "underflow"<<endl;
return false;
}
else
{
front=(front+1) % maxSize ;
x = elements[front];
return true;
}
}
template <class T>// 出队
bool SeqQueue<T>::getFront(T& x)
{
if (front == rear)
{
cout << "empty" << endl;
}
else
{
x = elements[front];
return true;
}
}
#include<iostream>
using namespace std;
template <class T>
class QueueNode
{
private:
T data;
QueueNode<T>* link;
public:
QueueNode(T d, QueueNode<T>* next = NULL) :data(d), link(next) {};
};
//链式队列
template <class T>
class LinkedQueue {
private:
QueueNode<T>* front, * rear; //队头,队尾指针
public:
LinkedQueue() :rear(NULL), front(NULL) {};
~LinkedQueue() {
QueueNode<T>* p;
while (front != NULL)
{
p = front;
front = front->link;
delete p;
}
}
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x);
void makeEmpty() { front = rear = -1; }
bool IsEmpty()const { return front == NULL; }
bool IsFull()const {
return ((rear+1)%maxSize== front);
}
int getSize()const
{
return (rear - front+maxSize)%maxSize;
}
};
//循环队列
template <class T>// 入队//就是单链表的建链,尾插法
bool LinkedQueue<T>::EnQueue(const T& x)
{
if (front == NULL)
{
front = rear = new QueueNode<T>(x);
if (front == NULL) return false;
}
else
{
rear->link = new QueueNode<T>(x);
if (rear->link == NULL) return false;
rear = rear->link;
}
return true;
}
template <class T>// 出队
bool LinkedQueue<T>::DeQueue(T& x)
{
if (IsEmpty())
{
cout << "underflow"<<endl;
return false;
}
else
{
front=(front+1) % maxSize ;
x = elements[front];
return true;
}
}
template <class T>// 取队首元素
bool LinkedQueue<T>::getFront(T& x)
{
if (IsEmpty()==true)
return false;
x = front->data;
return true;
}
练习:
此题在出队,但是循环队列的队首不能连续移动两个位置。
需要设计循环。
#include<iostream>
using namespace std;
#include "Queue.h"
int main()
{
int n = 0; // 表示队伍总人数
cin >> n;
SeqQueue<int > s(10);
int i = 1,num=0;
while (i <= n)
{
s.EnQueue(i);
i++;
}
i = 1;
int count = 1;
while (s.IsEmpty()!=true)
{
s.DeQueue(num);
if (count % 2 == 1)
{
cout << num << " ";
}
else
s.EnQueue(num);
count++;
}
return 0;
}