使用两个栈实现一个队列+使用两个队列实现一个栈

1. 使用两个栈实现一个队列+使用两个队列实现一个栈

  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");
    }
  2. 两个队列实现一个栈

    
    #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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值