队列的实现(循环顺序存储结构+链式存储结构+STL类模板)
队列
1.循环顺序存储结构
#include <iostream>
#include<stdio.h>
using namespace std;
typedef int ElemType;
#define OK 1
#define ERROR 0
typedef int Status;
class SqQueue {
private:
int MaxSize;
int front;
int rear;
ElemType* list;
public:
SqQueue();
~SqQueue();
int QueueLength();
Status EnQueue(ElemType e);
Status Dequeue();
void ClearQueue();
void display();
};
SqQueue::SqQueue() {
front = 0;
rear = 0;
MaxSize = 99;
list = new int[MaxSize];
}
SqQueue::~SqQueue() {
delete[]list;
}
int SqQueue::QueueLength() {
return (rear - front + MaxSize) % MaxSize;
}
Status SqQueue::EnQueue(ElemType e) {
if ((rear + 1) % MaxSize == front)
return ERROR;
list[rear] = e;
rear = (rear + 1) % MaxSize;
return OK;
}
Status SqQueue::Dequeue() {
if (front == rear)
return ERROR;
front = (front + 1) % MaxSize;
return OK;
}
void SqQueue::ClearQueue() {
front = 0;
rear = 0;
}
void SqQueue::display() {
if (front == rear)
cout << "当前为空队列!!!" << endl;
else
{
cout << "当前队列内元素有(头---->尾):";
for (int i =front,j=0 ; j < QueueLength(); i++,j++)
{
cout << list[i%MaxSize] << " ";
}
cout << endl;
}
}
int main()
{
SqQueue test;
test.display();
test.EnQueue(2);
test.display();
test.EnQueue(3);
test.display();
test.EnQueue(4);
test.display();
test.Dequeue();
test.display();
test.ClearQueue();
test.display();
return 0;
}
2.链式存储结构
#include <iostream>
#include<stdio.h>
using namespace std;
typedef int ElemType;
#define OK 1
#define ERROR 0
typedef int Status;
class Node {
public:
ElemType data;
Node* next;
Node() {
next = NULL;
data = 0;
}
};
class LinkQueue {
private:
Node* front;
Node* rear;
int len;
public:
LinkQueue();
~LinkQueue();
Status EnQueue(ElemType e);
Status DeQueue();
Status ClearQueue();
void display();
};
LinkQueue::LinkQueue() {
front = new Node;
rear = front;
len = 0;
}
LinkQueue::~LinkQueue() {
Node* p, * q;
p = front;
for (int i = 0; i < len; i++)
{
q = p->next;
delete p;
p = q;
}
front->next = NULL;
rear = front;
len = 0;
}
Status LinkQueue::EnQueue(ElemType e) {
Node* temp = new Node;
temp->data = e;
temp->next = NULL;
rear->next = temp;
rear = temp;
len++;
return OK;
}
Status LinkQueue::DeQueue() {
Node *p = new Node;
if (front == rear)
return ERROR;
p = front->next;
front->next = p->next;
if (rear == p)
rear = front;
delete p;
len--;
return OK;
}
Status LinkQueue::ClearQueue() {
Node* p, * q;
p = front->next;
for (int i = 0; i < len-1; i++)
{
q = p->next;
delete p;
p = q;
}
front->next=NULL;
rear = front;
len = 0;
return OK;
}
void LinkQueue::display() {
Node* temp;
if (rear == front)
cout << "当前为空队列!!!" << endl;
else
{
temp = front;
cout << "当前对列内元素有:";
for (int i = 0; i < len; i++)
{
cout << temp->next->data << " ";
temp = temp->next;
}
cout << endl;
}
}
int main() {
LinkQueue test;
test.EnQueue(1);
test.EnQueue(2);
test.EnQueue(3);
test.display();
test.DeQueue();
test.display();
test.ClearQueue();
test.EnQueue(3);
test.EnQueue(1);
test.display();
test.ClearQueue();
test.display();
return 0;
}
3.STL类模板实现
队列queue的用法如下:
- 1.包含头文件:
#include <queue> - 2.定义一个整数队列对象:
queue<int> myQe ; - 3.定义一个整数队列对象数组:queue myQA[10];
- 4.入队操作:
myQe.push(itemp) ; //把整数itemp进入队列 - 5.出队操作:
myQe.pop() ; //把队头元素弹出队列,注意本操作不获取队头元素 - 6.获取队头元素: itemp =
myQe.front() ; // 把队头元素放入itemp中,注意本操作不弹出元素 - 7.判断队列是否为空:
myQe.empty() ;//队列空则返回true,不空则返回false
|