赞
踩
越早到达越早离开,先进先出。
例如:银行储蓄柜前排队,计算机打印管理器中对打印队列的管理。
结合生活实际,队列的基本操作如下:
template<class elemType>
class queue {
public:
virtual bool isEmpty() = 0;
virtual elemType getHead() = 0;
virtual void enQueue(cosnt elemType& x) = 0;
virtual elemType deQueue() = 0;
virtual ~queue() {};
};
缺点: 时间复杂度为
O
(
1
)
O(1)
O(1),但出队时会空出前面的位置,造成空间的浪费
根据上述说明,给出循环队列类的声明如下
template<class elemType> class seqQueue :public queue<elemType> { private: elemType* elem; int maxSize; int front, rear; void doublespace(); public: seqQueue(int size = 10) { elem = new elemType[size]; maxSize = size; front = rear = 0; } ~seqQueue() { delete[]elem; }; bool isEmpty() { return front == rear; } elemType getHead() { return elem[(front + 1) % maxSize]; } void enQueue(const elemType& x); elemType deQueue(); };
由于队列的操作是在队列的两端进行的,不会对队列中的其他元素进行操作,用不带头结点的单链表即可。
其中front指向队首,rear指向队尾。
存储特性分析:
template<class elemType> class linkQueue :public queue<elemType> { private: struct node { elemType data; node* next; node(const elemType& x, node* N = NULL) { data = x; next = N; } node() :next(NULL) {} ~node(){} }; node* front, *rear; public: linkQueue() { front = rear = NULL; } ~linkQueue(); bool isEmpty() { return front == NULL; } elemType getHead() { return front->data; } void enQueue(const elemType& x); elemType deQueue(); };
template<class elemType>
void seqQueue<elemType>::doublespace() {
elemType* tmp = elem;
elem = new elemType[2 * maxSize];
for (int i = 1; i < maxSize; ++i) {
elem[i] = tmp[(front + i) % maxSize];
}
front = 0;
rear = maxSize - 1;
maxSize *= 2;
delete tmp;
}
template<class elemType>
void seqQueue<elemType>::enQueue(const elemType& x) {
//如果数组已经满了,则空间加倍
if ((rear + 1) % maxSize == front) doublespace();
rear = (rear + 1) % maxSize;
elem[rear] = x;
}
template<class elemType>
elemType seqQueue<elemType>::deQueue() {
front = (front + 1) % maxSize;
return elem[front];
}
template<class elemType>
void linkQueue<elemType>::enQueue(const elemType& x) {
if (front == NULL) {
front = rear = new node(x);
}
else {
rear->next = new node(x);
rear = rear->next;
}
}
template<class elemType>
elemType linkQueue<elemType>::deQueue() {
node* tmp = front;
elemType value = tmp->data;
front = front->next;
if (front == NULL) rear = NULL;
delete tmp;
return value;
}
template<class elemType>
linkQueue<elemType>::~linkQueue() {
node* tmp;
while (front != NULL) {
tmp = front;
front = front->next;
delete tmp;
}
}
# include<iostream> using namespace std; template<class elemType> class queue { public: virtual bool isEmpty() = 0; virtual elemType getHead() = 0; virtual void enQueue(const elemType& x) = 0; virtual elemType deQueue() = 0; virtual ~queue() {}; }; template<class elemType> class seqQueue :public queue<elemType> { private: elemType* elem; int maxSize; int front, rear; void doublespace(); public: seqQueue(int size = 10) { elem = new elemType[size]; maxSize = size; front = rear = 0; } ~seqQueue() { delete[]elem; } bool isEmpty() { return front == rear; } elemType getHead() { return elem[(front + 1) % maxSize]; } void enQueue(const elemType& x); elemType deQueue(); }; template<class elemType> void seqQueue<elemType>::doublespace() { elemType* tmp = elem; elem = new elemType[2 * maxSize]; for (int i = 1; i < maxSize; ++i) { elem[i] = tmp[(front + i) % maxSize]; } front = 0; rear = maxSize - 1; maxSize *= 2; delete tmp; } template<class elemType> void seqQueue<elemType>::enQueue(const elemType& x) { //如果数组已经满了,则空间加倍 if ((rear + 1) % maxSize == front) doublespace(); rear = (rear + 1) % maxSize; elem[rear] = x; } template<class elemType> elemType seqQueue<elemType>::deQueue() { front = (front + 1) % maxSize; return elem[front]; }
#include<iostream>
#include"seqQueue.h"
using namespace std;
int main() {
seqQueue<int> s(10);
for (int i = 0; i < 15; ++i)
s.enQueue(i);
while (!s.isEmpty())
cout << s.deQueue() << " ";
cout << endl;
return 0;
}
#include<iostream> using namespace std; template<class elemType> class queue { public: virtual bool isEmpty() = 0; virtual elemType getHead() = 0; virtual void enQueue(const elemType& x) = 0; virtual elemType deQueue() = 0; virtual ~queue() {}; }; template<class elemType> class linkQueue :public queue<elemType> { private: struct node { elemType data; node* next; node(const elemType& x, node* N = NULL) { data = x; next = N; } node() :next(NULL) {} ~node(){} }; node* front, *rear; public: linkQueue() { front = rear = NULL; } ~linkQueue(); bool isEmpty() { return front == NULL; } elemType getHead() { return front->data; } void enQueue(const elemType& x); elemType deQueue(); }; template<class elemType> void linkQueue<elemType>::enQueue(const elemType& x) { if (front == NULL) { front = rear = new node(x); } else { rear->next = new node(x); rear = rear->next; } } template<class elemType> elemType linkQueue<elemType>::deQueue() { node* tmp = front; elemType value = tmp->data; front = front->next; if (front == NULL) rear = NULL; delete tmp; return value; } template<class elemType> linkQueue<elemType>::~linkQueue() { node* tmp; while (front != NULL) { tmp = front; front = front->next; delete tmp; } }
#include<iostream>
#include"linkQueue.h"
using namespace std;
int main() {
linkQueue<int> s;
for (int i = 0; i < 15; ++i)
s.enQueue(i);
while (!s.isEmpty())
cout << s.deQueue() << " ";
cout << endl;
return 0;
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。