当前位置:网站首页>队列实现原理和应用
队列实现原理和应用
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' : ' ');
}
}
}
边栏推荐
- memcached全面剖析–2. 理解memcached的內存存儲
- memcached全面剖析–3. memcached的删除机制和发展方向
- Big factories go out to sea and lose "posture"
- Debugging Analysis of Kernel panics and Kernel oopses using System Map
- 【Camera基础(一)】Camera摄像头工作原理及整机架构
- 《各行业零代码企业应用案例集锦》正式发布
- A field in the database is of JSON type and stores ["1", "2", "3"]
- Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
- 188. the best time to buy and sell stocks IV
- C语言-关键字1
猜你喜欢
Introduce the overall process of bootloader, PM, kernel and system startup
memcached全面剖析–5. memcached的应用和兼容程序
The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
VirtualBox虚拟机安装Win10企业版
介绍BootLoader、PM、kernel和系统开机的总体流程
Network layer & IP
About transform InverseTransformPoint, transform. InverseTransofrmDirection
EditText controls the soft keyboard to search
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
为什么生命科学企业都在陆续上云?
随机推荐
Functional analysis of ebpf sockops
Unity关于本地坐标和世界坐标之间的转换
TCP Jprobe utilization problem location
Auto. JS to automatically authorize screen capture permission
Memcached comprehensive analysis – 2 Understand memcached memory storage
memcached全面剖析–5. memcached的应用和兼容程序
The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
C语言实现DNS请求器
leetcode1720_2021-10-14
What does CTO (technical director) usually do?
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓
Volcano成Spark默认batch调度器
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
WMI and PowerShell get TCP connection list
Wireshark packet capturing skills summarized by myself
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
Volcano becomes spark default batch scheduler
Pattern recognition - 9 Decision tree
CondaValueError: The target prefix is the base prefix. Aborting.