当前位置:网站首页>队列实现原理和应用
队列实现原理和应用
2022-06-24 19:28:00 【翁炜强】
循环数组实现队列:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
template<class T>
class LoopQueue
{
public:
LoopQueue(int c );
~LoopQueue();
public:
bool isEmpty();
int size();
bool push(T t);
bool pop();
T front();
private:
int capacity;
int begin;
int end;
T* queue;
};
template<typename T>
LoopQueue<T>::LoopQueue(int c ) :capacity(c), begin(0), end(0), queue(nullptr)
{
queue = new T[capacity];
}
template<typename T>
LoopQueue<T>::~LoopQueue()
{
delete[]queue;
}
template<typename T>
bool LoopQueue<T>::isEmpty()
{
if (begin == end)
{
return true;
}
return false;
}
template<typename T>
int LoopQueue<T>::size()
{
return (end - begin + capacity) % capacity;
}
template<typename T>
bool LoopQueue<T>::push(T t)
{
if ((end + 1) % capacity == begin)
{
return false;
}
queue[end] = t;
end = (end + 1) % capacity;
return true;
}
template<typename T>
bool LoopQueue<T>::pop()
{
if (end == begin)
{
return false;
}
begin = (begin + 1) % capacity;
return true;
}
template<typename T>
T LoopQueue<T>::front()
{
if (end == begin)
{
exit(0);
}
else
{
return queue[begin];
}
}
int main()
{
LoopQueue<string>queue(6);
queue.push("one");
queue.push("two");
queue.push("three");
queue.push("four");
queue.push("five");
queue.push("six");
cout << "队列长度 " << queue.size() << endl;
while (!queue.isEmpty())
{
cout << queue.front()<< endl;
queue.pop();
}
getchar();
return 0;
}
队列实现:
#include<iostream>
#include<cstring>
#include<queue>
#include<assert.h>
using namespace std;
template <class T>
class Node
{
public:
Node<T>* next;
T val;
Node(T x):val(x),next(NULL){}
};
template<class T>
class Queue
{
public:
Queue():front(NULL),rear(NULL),size(0){}
bool empty() { return front == NULL; }
void push(T x)
{
if (empty())
{
front = rear = new Node<T>(x);
++size;
return;
}
rear->next = new Node<T>(x);
rear = rear->next;
++size;
}
void pop()
{
assert(!empty());
Node<T>* temp;
temp = front;//删除节点
front= front->next;
delete temp;
--size;
}
T _front()
{
assert(NULL != front);
return front->val;
}
int Size()
{
return size;
}
private:
Node<T>* front;
Node<T>* rear;
int size;
};
int main()
{
Queue<string>q;
q.push("我好爱编程");
q.push("编程使我快乐");
q.push("进入腾讯 改变命运");
cout << q.Size() << endl;
while (!q.empty())
{
cout << q._front() << endl;
q.pop();
}
}
应用:
题目:https://codeforces.com/contest/1399/problem/D
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<string>
using namespace std;
const int N = 2e5 + 10;
int note[N];
char s[N];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int ans = 0;
queue<int>q[2];
int n;
scanf("%d", &n);
scanf("%s", s+1);
for (int i = 1; i <=n; ++i)
{
int m = s[i] - '0';
if (!q[m].empty())
{
int temp = q[m].front();
q[m].pop();//将0或1提取出来 加入相反的数中
note[i] = temp;
q[m ^ 1].push(temp);
}
else
{
++ans;//如果此时不存 则新建一个序列
q[m ^ 1].push(ans);
note[i]=ans;
}
}
printf("%d\n", ans);
for (int i = 1; i <=n; ++i)
{
printf("%d%c", note[i], i == n? '\n' : ' ');
}
}
}边栏推荐
- WMI and PowerShell get TCP connection list
- Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
- EditText controls the soft keyboard to search
- [camera Foundation (I)] working principle and overall structure of camera
- (to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
- 66 pitfalls in go programming language: pitfalls and common errors of golang developers
- 装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
- 2022国际女性工程师日:戴森设计大奖彰显女性设计实力
- Prompt that the device has no permission when using ADB to connect to the device
- VirtualBox虚拟机安装Win10企业版
猜你喜欢

使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北

架构实战营 第 6 期 毕业设计

leetcode-201_2021_10_17

EditText controls the soft keyboard to search

Transport layer UDP & TCP

字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?

【吴恩达笔记】机器学习基础

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

介绍BootLoader、PM、kernel和系统开机的总体流程

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
随机推荐
leetcode-201_2021_10_17
Web project deployment
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
VirtualBox virtual machine installation win10 Enterprise Edition
Auto. JS to automatically authorize screen capture permission
【吴恩达笔记】多变量线性回归
Functional analysis of ebpf sockops
Rewrite, maplocal and maplocal operations of Charles
如何做到全彩户外LED显示屏节能环保
Visit Amazon memorydb and build your own redis memory database
Implementation of adjacency table storage array of graph
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
Unity about conversion between local and world coordinates
力扣每日一题-第26天-496.下一个更大元素Ⅰ
Pattern recognition - 1 Bayesian decision theory_ P1
ping: www.baidu. Com: unknown name or service
Tournament sort
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
架构实战营 第 6 期 毕业设计
#国企央企结构化面试#国企就业#墨斗互动就业服务管家