Queue: 依循先进先出的规则的单调队列.
下面是用链表实现的queue的几个基本用法和一个clear()的补充用法:
#include<stdio.h>
/*
*Date:2018/10/22
*Author:Fushicho
*Name:queue链表版
*Function:push(),pop(),front(),empty(),size(),back(),clear()
*/
struct queue {
private:
struct node {
int num; //节点数据
node *next_p; //节点指针指向该节点的下一个节点
node() { //构造函数(初始化)
this->next_p = NULL;
}
};
node *head; //队列头指针
node *tail; //队列尾指针
public:
queue() { //构造函数(初始化)
head = NULL;
tail = NULL;
}
void push(int x) { //在队列尾加入一个元素
node *p = new node();
p->num = x;
if (empty()) { //队列为空
head = p;
tail = p;
}
else { //队列不为空
tail->next_p = p;
tail = p;
}
}
void pop() { //将队列头节点弹出并释放内存
node *p = head;
head = head->next_p;
delete(p);
}
int front() { //获得队列首元素
return head->num;
}
int back() { //输出队尾元素
return tail->num;
}
bool empty() { //判断队列是否为空
return head == NULL;
}
int size() { //计算队列元素个数
if (empty())return 0;
int cnt = 0;
node *p = head;
while (p != tail) {
p = p->next_p;
cnt++;
}
return cnt+1;
}
void clear() { //清空(需要把每个节点都释放内存,优点:节省内存,缺点:时间慢)
node *p;
while (head != tail) {
p = head;
head = head->next_p;
delete(p);
}
head = tail = NULL;
}
};
void debug() { //测试代码
queue que;
int a[] = { 1,2,3,4,5,6 };
printf("入队列顺序:");
for (int i = 0; i < 6; i++) {
printf("%d ", a[i]);
que.push(a[i]); //push()测试
}
printf("\n");
printf("队列元素个数:%d\n", que.size()); //size()测试
que.clear(); //clear()测试
for (int i = 0; i < 6; i++)
que.push(a[i]), printf("队尾元素为:%d\n", que.back());//back()测试
printf("出队列顺序:");
while (!que.empty()) { //empty()测试
int x = que.front(); que.pop(); //pop()测试
printf("%d ", x);
}
printf("\n");
}
int main() {
debug();
return 0;
}
下面是用数组实现的queue的几个基本用法和一个clear()的补充用法:
#include<stdio.h>
const int maxn = 10; //为了测试循环数组是否实现成功取一个较小的值(测试完后可以根据需求该大小)
/*
*Date:2018/10/22
*Author:Fushicho
*Name:queue数组版
*Function:push(),pop(),front(),empty(),size(),back(),clear()
*/
struct queue {
private:
int que[maxn]; //队列数组(最大存储个数:manx , 在多次使用下采用循环数组方式实现重复利用空间)
int head, tail; //这里的"头指针"和"尾指针"都是一个int,用作记录头和尾指针的下标
/*循环数组:存放que的方式有2种:
(1) [head,tail)
(2) [head,maxn) and [0,tail)
*/
//且head和tail都小于maxn
public:
queue() { //构造函数(初始化)
head = tail = 0;
}
void push(int x) { //在队列尾加入一个元素
que[tail++] = x;
tail %= maxn;
}
void pop() { //将队列头节点弹出并释放内存
head++;
head %= maxn;
}
int front() { //获得队列首元素
return que[head];
}
int back() { //获得队列尾元素
return que[((tail - 1)%maxn + maxn) % maxn]; //(避免tail出现负数)
}
bool empty() { //判断队列是否为空
return head == tail;
}
int size() { //获得队列元素个数
if (head <= tail) return tail - head;
return maxn - head + tail;
}
void clear() { //清空(只需要修改头指针和尾指针,优点:节省时间)
head = tail;
}
};
void debug() { //测试代码
queue que;
int a[] = { 1,2,3,4,5,6 };
printf("入队列顺序:");
for (int i = 0; i < 6; i++) {
printf("%d ", a[i]);
que.push(a[i]); //push()测试
}
printf("\n");
printf("队列元素个数:%d\n", que.size()); //size()测试
que.clear(); //clear()测试
for (int i = 0; i < 6; i++)
que.push(a[i]), printf("队尾元素为:%d\n", que.back()); //back()测试
printf("出队列顺序:");
while (!que.empty()) { //empty()测试
int x = que.front(); que.pop(); //pop()测试
printf("%d ", x);
}
printf("\n");
}
int main() {
debug();
return 0;
}
若有不懂或者有写错的地方请在下方评论指出,谢谢~