当前位置:网站首页>Principle and application of queue implementation
Principle and application of queue implementation
2022-06-24 22:00:00 【Weng Weiqiang】
Loop array implements queue :
#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 << " The queue length " << queue.size() << endl;
while (!queue.isEmpty())
{
cout << queue.front()<< endl;
queue.pop();
}
getchar();
return 0;
}
Queue implementation :
#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;// Delete node
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(" I love programming ");
q.push(" Programming makes me happy ");
q.push(" Enter Tencent Change the fate ");
cout << q.Size() << endl;
while (!q.empty())
{
cout << q._front() << endl;
q.pop();
}
}
application :
subject :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();// take 0 or 1 extracted Add the opposite number
note[i] = temp;
q[m ^ 1].push(temp);
}
else
{
++ans;// If it doesn't exist at this time Create a new sequence
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' : ' ');
}
}
}边栏推荐
猜你喜欢

socket(1)

Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)

手动事务的几个类

使用region折叠代码
Visit Amazon memorydb and build your own redis memory database

LeetCode-513. 找树左下角的值

【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index

权限想要细化到按钮,怎么做?

Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance

面试官:你说你精通Redis,你看过持久化的配置吗?
随机推荐
Multiplexer select
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
Detailed installation and use of performance test tool wrk
[untitled]
权限想要细化到按钮,怎么做?
dp问题集
Collective search + drawing creation
Development trend and path of SaaS industry in China
03---增反膜
That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
Want to be a test leader, do you know these 6 skills?
降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)
即构「畅直播」上线!提供全链路升级的一站式直播服务
拖动拖动拖动
Machine learning: gradient descent method
【吴恩达笔记】卷积神经网络
Xinlou: Huawei's seven-year building journey of sports health
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
Kubernetes 集群中流量暴露的几种方案
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)