两栈一队列和两队列一栈——题集(五)
今天分享一下,用两队列实现一个栈和用两个栈实现一个队列,顺便稍微说说字符串替换。
用两队列实现一个栈的源代码和运行界面:
原代码如下:
#include<iostream>
using namespace std;
#include<queue>
//用两队列实现一个栈和用两个栈实现一个队列
template<class T>
class Stack{
public:
Stack(){}
Stack(T val){
s1.push(val);
}
~Stack(){
}
//确保一空一满
void push(const T val){//让满的队列来接收数据
if(s2.empty())
s1.push(val);
else
s2.push(val);
}
void pop(){//把数据放到空队列中,并释放数据
if(Empty()) return;
if(s1.empty()){
_pop(s1, s2);
}
else
_pop(s2, s1);
}
T Top(){
if(Empty()) return T();
if(s1.empty()){
return s2.back();
}
else
return s1.back();
}
int Size(){
return s1.size()+s2.size();//不管那个队列中有数,都算一遍
}
bool Empty(){
if(s1.empty() && s2.empty())return true;
return false;
}
protected:
void _pop(queue<T>& s1, queue<T>& s2){//s1空,s2满
T tmp=s2.front();
s2.pop();
while(!s2.empty()){
s1.push(tmp);
tmp=s2.front();
s2.pop();
}
}
queue<T> s1;
queue<T> s2;
};
void Teststack(){
cout<<"两队列实现一个栈"<<endl;
Stack<int> s;
cout<<"Stack<int> s->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(3);
cout<<"s.push(3)->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(8);
cout<<"s.push(8)->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(6);
cout<<"s.push(6)->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->s.Top(): "<<s.Top()<<"; s.Empty(): "<<s.Empty()<<endl;
cout<<endl;
}
int main(){
Teststack();
system("pause");
return 0;
}
运行界面:
用两个栈实现一个队列的源代码和运行界面:
原代码如下:
#include<iostream>
using namespace std;
#include<stack>
template<class T>
class Queue{
public:
Queue(){
}
Queue(T val){
q1.push();
}
~Queue(){
}
//q1收数据,q2释放数据
void push(const T val){//让q1来接收数据
q1.push(val);
}
void pop(){//让q2来释放数据
if(Empty()) return;
if(q2.empty())
_turn(q1,q2);
q2.pop();
}
T Front(){
if(Empty()) return T();
if(q2.empty())
_turn(q1,q2);
return q2.top();
}
T Back(){
if(Empty()) return T();
if(q1.empty())
_turn(q2,q1);
return q1.top();
}
int Size(){
return q1.size()+q2.size();
}
bool Empty(){
if(q1.empty() && q2.empty()) return true;
return false;
}
protected:
void _turn(stack<T>& q1, stack<T>& q2){//把q1中的数据导入q2中
while(!q1.empty()){
T tmp=q1.top();
q1.pop();
q2.push(tmp);
}
}
stack<T> q1;
stack<T> q2;
};
void Testqueue(){
cout<<"用两个栈实现一个队列"<<endl;
Queue<int> s;
cout<<"Queue<int> s->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(1);
cout<<"s.push(1)->Front(): "<<s.Front();
cout<<"; >Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(4);
cout<<"s.push(4)->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.push(8);
cout<<"s.push(8)->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
s.pop();
cout<<"s.pop()->Front(): "<<s.Front();
cout<<"; Back(): "<<s.Back()<<"; s.Empty(): "<<s.Empty()<<endl;
cout<<endl;
}
int main(){
Testqueue();
system("pause");
return 0;
}
运行界面:
替换字符串中的空格为$$$。要求时间复杂度为O(N)
例如:将"talk is cheap show me the code"替换。
替换为"talk$$$is$$$cheap$$$show$$$me$$$the$$$code"。
原代码如下:
#include<iostream>
using namespace std;
//顺便稍微说说字符串替换。替换字符串中的空格为$$$
void replace(string& str,int length) {
//从左到右遍历,从右到左替换
string tmp=str;
int count=0;
int i=0;
while(tmp[i] != '\0'){
if(tmp[i] == ' '){
count +=2;
}
i++;
}
str.resize(length+count);
i=length;
while(i>0){
if(tmp[i] == ' '){
str[count+i]='$';
str[count+i-1]='$';
str[count+i-2]='$';
count-=2;
}
else{
str[count+i]=tmp[i];
}
i--;
}
}
int main(){
cout<<"替换字符串中的空格为$$$"<<endl;
string str="talk is cheap show me the code";
cout<<"原字符串: "<<str.c_str()<<endl;
replace(str,str.size());
cout<<"替换后的字符串: "<<str.c_str()<<endl;
system("pause");
return 0;
}
运行界面:
分享如上,望共同进步!