C++ 数据结构算法 学习笔记(7) - 队列及企业级应用
队列的故事导入
Jack 在一个著名软件外包公司上班,公司每年要接很多的外包项目给各个开发团队开发,现在刚 刚接到一个项目,要实现银行排队叫号系统, 基本需求是:
取号
- 银行用户进入银行大厅办理业务
- 用户从自动排号机上取号
- 自动取号机生成号码以及当前等待人数信息给用户
叫号
当银行业务员(1 位或多位)处理完业务以后,会指示叫号系统呼叫下一位(按排队次序)正在等待的用 户,用户达到窗口以后,银行业务员会将用户的号码从叫号系统中删除。
如果你是 Jack ,你该如何设计这个功能?
队列的原理讲解
-
队列是一种首先得线性结构
-
他只允许在表的前段(front)进行删除操作, 而在标的后端(rear)进行插入操作
生活中队列场景随处可见:比如在电影院,商场,厕所排队等…
队列的算法实现
顺序存储
采用数组来保存队列的元素,设立一个队首指针 front ,一个队尾指针 rear,分别指向队首和队尾元素。则 rear-front 即为存储的元素个数!
#define MaxSize 5
typedef int DataType;
typedef struct Queue
{
DataType queue[MaxSize];
int front;
int rear;
}SeqQueue;
队列的初始化
void InitQueue(SeqQueue* SQ)
{
if (!SQ) return;
SQ->front = SQ->rear = 0;
}
队列是否为空
int isEmpty(SeqQueue* SQ)
{
if (!SQ) return 0;
if (SQ->front == SQ->rear)
{
return 1;
}
return 0;
}
队列是否为满
int IsFull(SeqQueue* SQ)
{
if (!SQ) return 0;
if(SQ->rear == MaxSize)
{
return 1;
}
return 0;
}
队列的入队
int EnterQueue(SeqQueue* SQ, DataType data)
{
if (!SQ) return 0;
if (IsFull(SQ))
{
cout << "Not able to minsert the element inside the queue because it's full!" << endl;
return 0;
}
SQ->queue[SQ->rear] = data;
SQ->rear++;
return 1;
}
队列的打印元素
void PrintQueue(SeqQueue* SQ)
{
if (!SQ)return;
int i = SQ->front;
while (i < SQ->rear)
{
cout << setw(4) << SQ->queue[i];
i++;
}
cout << endl;
}
队列的删除Front元素(后面的所有元素往前移1位并返回被删除的元素)
int DeleteQueue(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The Queue is empty!" << endl;
return 0;
}
if (!data) return 0;
*data = SQ->queue[SQ->front];
for (int i = SQ->front + 1; i < SQ->rear; i++)
{
SQ->queue[i - 1] = SQ->queue[i];
}
SQ->rear--;
return 1;
}
队列的删除Front元素 (Front 加一然后返回)
int DeleteQueue2(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The queue is empty!" << endl;
return 0;
}
if (SQ->front >= MaxSize)
{
cout << "The queue is reach until the end" << endl;
return 0;
}
*data = SQ->queue[SQ->front];
SQ->front = (SQ->front) + 1;
return 1;
}
返回Front指向的元素值
int GetHead(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The queue is empty" << endl;
}
return *data = SQ->queue[SQ->front];
}
清空队列
void ClearQueue(SeqQueue* SQ)
{
if (!SQ) return;
SQ->front = SQ->rear = 0;
}
完整代码实现
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MaxSize 5
typedef int DataType;
typedef struct Queue
{
DataType queue[MaxSize];
int front;
int rear;
}SeqQueue;
void InitQueue(SeqQueue* SQ)
{
if (!SQ) return;
SQ->front = SQ->rear = 0;
}
int IsFull(SeqQueue* SQ)
{
if (!SQ) return 0;
if(SQ->rear == MaxSize)
{
return 1;
}
return 0;
}
int isEmpty(SeqQueue* SQ)
{
if (!SQ) return 0;
if (SQ->front == SQ->rear)
{
return 1;
}
return 0;
}
// Insert the element inside the queue
int EnterQueue(SeqQueue* SQ, DataType data)
{
if (!SQ) return 0;
if (IsFull(SQ))
{
cout << "Not able to minsert the element inside the queue because it's full!" << endl;
return 0;
}
SQ->queue[SQ->rear] = data;
SQ->rear++;
return 1;
}
//Remove the element from the queue. Must follow the FIFO sequence
int DeleteQueue(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The Queue is empty!" << endl;
return 0;
}
if (!data) return 0;
*data = SQ->queue[SQ->front];
for (int i = SQ->front + 1; i < SQ->rear; i++)
{
SQ->queue[i - 1] = SQ->queue[i];
}
SQ->rear--;
return 1;
}
int DeleteQueue2(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The queue is empty!" << endl;
return 0;
}
if (SQ->front >= MaxSize)
{
cout << "The queue is reach until the end" << endl;
return 0;
}
*data = SQ->queue[SQ->front];
SQ->front = (SQ->front) + 1;
return 1;
}
//Printout the element of the queue
void PrintQueue(SeqQueue* SQ)
{
if (!SQ)return;
int i = SQ->front;
while (i < SQ->rear)
{
cout << setw(4) << SQ->queue[i];
i++;
}
cout << endl;
}
//Only want to get the first element, but not remove it from the queue
int GetHead(SeqQueue* SQ, DataType* data)
{
if (!SQ || isEmpty(SQ))
{
cout << "The queue is empty" << endl;
}
return *data = SQ->queue[SQ->front];
}
// Clear the queue
void ClearQueue(SeqQueue* SQ)
{
if (!SQ) return;
SQ->front = SQ->rear = 0;
}
//Get the length of the queue
int getLength(SeqQueue* SQ)
{
if (!SQ) return 0;
return (SQ->rear - SQ->front);
}
int main()
{
SeqQueue* SQ = new SeqQueue;
DataType data;
//Initial the Queue List
InitQueue(SQ);
//2. Insert the element inside the queue
for (int i = 1; i < 8; i++)
{
EnterQueue(SQ, i);
}
PrintQueue(SQ);
//3. Comeout from the queue
DeleteQueue(SQ, &data);
cout << "The comeout element from the queus is " << data << endl;
cout << "The remaining element inside the queue is ";
cout << getLength(SQ) << endl;
PrintQueue(SQ);
cout << endl;
for (int i = 0; i < 10; i++)
{
if (DeleteQueue2(SQ, &data))
{
cout << "The comeout element from the queue by using second function is " << data << endl;
cout << "The remaining element inside the queue is ";
cout << getLength(SQ) << endl;
PrintQueue(SQ);
}
else
{
cout << "Failed to remove the element from the queue" << endl;
}
}
system("pause");
return 0;
}