1.队列的定义
队列是一种特殊的线性表。在这种线性表中,插入和删除都限定在表的一端。允许插入的一端为队尾,允许进行删除的一端为队头,位于队头的元素为队头元素,位于队尾的元素为队尾元素。队列中的元素个数为队列的长度,若队列中没有元素,则为空队列。在队列中插入一个元素为入队,删除一个元素为出队。 队列满足 先进先出。
队列的主要操作:
1.创建一个队列create()
2.入队enQueue(x)
3.出队deQueue()
4.获得队头元素getHead()
5.判断队列是否为空isEmpty()
队列的抽象类:
template<class elemType>
class queue{
public:
virtual bool isEmpty() const = 0;
virtual void enQueue(const elemType&x) = 0;
virtual elemType deQueue() = 0;
virtual elemType getHead()const = 0;
virtual ~queue(){}
};
2.队列的顺序实现
template<class elemType>
class queue{
public:
virtual bool isEmpty() const = 0;
virtual void enQueue(const elemType&x) = 0;
virtual elemType deQueue() = 0;
virtual elemType getHead()const = 0;
virtual ~queue(){}
};
template <class elemType>
class seqQueue : public queue<elemType>
{
private:
elemType *elem;
int maxSize;
int front, rear;
void doubleSpace();
public:
seqQueue(int initSize = 10);
~seqQueue();
bool isEmpty() const;
void enQueue(const elemType &x);
elemType deQueue();
elemType getHead() const;
};
/**
* 构造函数
*/
template <class elemType>
seqQueue<elemType>::seqQueue(int initSize)
{
elem = new elemType[initSize];
maxSize = initSize;
front = rear = 0;
}
/**
* 析构函数
*/
template <class elemType>
seqQueue<elemType>::~seqQueue()
{
delete[] elem;
}
/**
* isEmpty 函数 判断队列是否为空
*/
template <class elemType>
bool seqQueue<elemType>::isEmpty() const
{
return front == rear;
}
/**
* deQueue 函数 出队
*/
template <class elemType>
elemType seqQueue<elemType>::deQueue()
{
front = (front + 1) % maxSize;
return elem[front];
}
/**
* getHead 函数 获取头元素
*/
template <class elemType>
elemType seqQueue<elemType>::getHead() const
{
return elem[(front + 1) % maxSize];
}
/**
* enQueue 函数 入队
*/
template <class elemType>
void seqQueue<elemType>::enQueue(const elemType &x)
{
if ((rear + 1) % maxSize == front)
doubleSpace();
else
rear = (rear + 1) % maxSize;
elem[rear] = x;
}
/**
* doubleSpace 函数 扩展空间
*/
template <class elemType>
void seqQueue<elemType>::doubleSpace()
{
elemType *tmp = elem;
elem = new elemType[maxSize * 2];
for (int i = 1; i <= maxSize; i++)
elem[i] = tmp[(front + i) % maxSize];
front = 0;
rear = maxSize;
maxSize *= 2;
delete tmp;
}
3.队列的链接实现
template <class elemType>
class queue
{
public:
virtual bool isEmpty() const = 0;
virtual void enQueue(const elemType &x) = 0;
virtual elemType deQueue() = 0;
virtual elemType getHead() const = 0;
virtual ~queue() {}
};
template <class elemType>
class linkQueue : public queue<elemType>
{
private:
struct node
{
elemType data;
node *next;
node(const elemType &x, node *n = nullptr)
{
data = x;
next = n;
}
node() : next(nullptr) {}
~node() {}
};
node *front, *rear;
public:
linkQueue();
~linkQueue();
bool isEmpty() const;
void enQueue(const elemType &x);
elemType deQueue();
elemType getHead() const;
};
/**
* 构造函数
*/
template <class elemType>
linkQueue<elemType>::linkQueue()
{
front = rear = nullptr;
}
/**
* 析构函数
*/
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node *tmp;
while (front != nullptr)
{
tmp = front;
front = front->next;
delete tmp;
}
}
/**
* isEmpty 函数 判断是否为空
*/
template <class elemType>
bool linkQueue<elemType>::isEmpty() const
{
return front == nullptr;
}
/**
* getHead 函数 获取头元素
*/
template <class elemType>
elemType linkQueue<elemType>::getHead() const
{
return front->data;
}
/**
* enQueue 函数 进队
*/
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType &x)
{
if (rear == nullptr)
front = rear = new node(x);
else
rear = rear->next = new node(x);
}
/**
* deQueue 函数 出队
*/
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node *tmp = front;
elemType value = front->data;
front = front->next;
if (front == nullptr)
rear = nullptr;
delete tmp;
return value;
}
4.队列的单循环链表实现
#include <iostream>
using namespace std;
template <class elemType>
class queue
{
public:
virtual bool isEmpty() const = 0;
virtual void enQueue(const elemType &x) = 0;
virtual elemType deQueue() = 0;
virtual elemType getHead() const = 0;
virtual ~queue() {}
};
template <class elemType>
class linkQueue : public queue<elemType>
{
private:
struct node
{
elemType data;
node *next;
node(const elemType &x, node *n = nullptr)
{
data = x;
next = n;
}
node() : next(nullptr) {}
~node() {}
};
node *rear;
public:
linkQueue();
~linkQueue();
bool isEmpty() const;
void enQueue(const elemType &x);
elemType deQueue();
elemType getHead() const;
};
/**
* 构造函数
*/
template <class elemType>
linkQueue<elemType>::linkQueue()
{
rear = nullptr;
}
/**
* 析构函数
*/
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node *tmp, *delp;
if (rear == nullptr)
return;
delp = rear->next;
rear->next = nullptr;
while (delp != nullptr)
{
tmp = delp;
delp = delp->next;
delete tmp;
}
}
/**
* isEmpty 函数 判断是否为空
*/
template <class elemType>
bool linkQueue<elemType>::isEmpty() const
{
return rear == nullptr;
}
/**
* enQueue 函数 进队
*/
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType &x)
{
if (rear == nullptr)
{
rear = new node(x);
rear->next = rear;
}
else
rear = rear->next = new node(x, rear->next);
}
/**
* deQueue 函数 出队
*/
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node *tmp = rear->next;
elemType value = tmp->data;
if (rear == tmp)
rear = nullptr;
else
rear->next = tmp->next;
delete tmp;
return value;
}
/**
* getHead 函数 获取头元素
*/
template <class elemType>
elemType linkQueue<elemType>::getHead() const
{
if (rear == nullptr)
{
exit(-1);
return NULL;
}
else
return rear->next->data;
}