使用两个栈实现一个队列+使用两个队列实现一个栈
来源:互联网 发布:简述单片机的引脚功能 编辑:程序博客网 时间:2024/06/06 01:09
1. 使用两个栈实现一个队列+使用两个队列实现一个栈
两个栈实现一个队列
#include<iostream>#include<stack>using namespace std;template<class T>class StackQueue{public: void Push(T data) { Spush.push(data); } void Pop() { if (Spop.empty()) { while (!Spush.empty()) { Spop.push(Spush.top()); Spush.pop(); } } Spop.pop(); } T Front() { if (Spop.empty()) { while (!Spush.empty()) { Spop.push(Spush.top()); Spush.pop(); } } return Spop.top(); } bool empty() { return Spop.empty() && Spush.empty(); }private: stack<T> Spop; stack<T> Spush;};int main(){ StackQueue<int> q; q.Push(3); q.Push(4); q.Push(5); q.Push(6); q.Push(7); q.Push(8); q.Push(9); q.Push(10); while (!q.empty()) { cout << q.Front() << " "; q.Pop(); } system("pause");}
两个队列实现一个栈
#include<iostream>#include<queue>using namespace std;template<class T>class QueueStack{public: void Pop() { while (PushQ.size() > 1) { PopQ.push(PushQ.front()); PushQ.pop(); } PushQ.pop(); while (!PopQ.empty()) { PushQ.push(PopQ.front()); PopQ.pop(); } } void Push(T data) { PushQ.push(data); } T Top() { while (PushQ.size() > 1) { PopQ.push(PushQ.front()); PushQ.pop(); } T top=PushQ.front(); PopQ.push(PushQ.front()); PushQ.pop(); while (!PopQ.empty()) { PushQ.push(PopQ.front()); PopQ.pop(); } return top; } bool Empty() { return PushQ.empty()&& PopQ.empty(); }private: queue<T> PopQ; queue<T> PushQ;};int main(){ QueueStack<int> s; s.Push(5); s.Push(6); s.Push(7); while (!s.Empty()) { cout << s.Top() << " "; s.Pop(); } system("pause");};
2. 将字符串的空格替换成%20
```#include<iostream>using namespace std;int getBlankCount(const char *arr, int len){ int count = 0; for (int i = 0; i < len; i++) { if (arr[i] == ' ') ++count; } return count;}void addTo20(char *arr, int len){ if (arr == nullptr || len <= 0) return; int tail = len; int count = getBlankCount(arr,len); int end = 2 * count + tail; while (tail < end) { if (arr[tail] == ' ') { arr[end--] = '%'; arr[end--] = '0'; arr[end--] = '2'; } else arr[end--] = arr[tail]; --tail; }}int main(){ char arr[100] = " are you ok "; int len = strlen(arr); addTo20(arr, len); cout << arr << endl; system("pause");}```
阅读全文
0 0
- 栈&队列--使用两个栈实现一个队列+使用两个队列实现一个栈
- 使用两个队列实现一个栈
- 使用两个队列实现一个栈
- 使用两个栈实现一个队列
- 使用两个栈实现一个队列
- 使用两个队列实现一个栈
- 数据结构-使用两个栈实现一个队列
- 使用两个队列实现一个栈
- 使用两个栈实现一个队列
- 使用两个队列实现一个栈
- 使用两个栈实现一个队列
- 使用两个栈实现一个队列
- 使用两个栈实现一个队列
- ~使用两个栈实现一个队列~
- ~使用两个队列实现一个栈~
- 使用两个栈实现一个队列
- 使用两个队列实现一个栈
- 使用两个栈实现一个队列
- [leetcode]38. Count and Say@Java
- linux驱动模型开发——linux platform总线机制讲解与实例开发
- 安卓性能优化之布局优化(二)
- Linux源码解析-进程-进程
- 17个新手常见Python运行时错误
- 使用两个栈实现一个队列+使用两个队列实现一个栈
- 千锋教育回应“骗子、骗局、坑”——浮言可以事久而明
- 阿里云备案类型介绍以及遇到的问题
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- 内存对齐与位域
- 微软夏令营笔试测验第一题 Array Partition
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭
- 条件时间选择器
- linux-1 history 历史命令调用以及常用快捷键