c_cpp 按堆栈实现队列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 按堆栈实现队列相关的知识,希望对你有一定的参考价值。

- https://stackoverflow.com/questions/69192/how-to-implement-a-queue-using-two-stacks
- http://www.geeksforgeeks.org/queue-using-stacks/
#include <cassert>
#include <iostream>
#include <stack>

template<class T>
class Queue
{
public:
  Queue()
  {
  }

  ~Queue()
  {
  }

  void Push(T data)
  {
    s.push(data);
  }

  T Pop()
  {
    assert(Size());

    T first;
    if (Size() == 1) {
      first = s.top(); s.pop();
    } else { // Size() > 1
      T last = s.top(); s.pop();
      first = Pop();
      Push(last);
    }

    return first;
  }

  T Peek()
  {
    assert(Size());

    T first;
    if (Size() == 1) {
      first = s.top();
    } else { // Size() > 1
      T last = s.top(); s.pop();
      first = Peek();
      Push(last);
    }

    return first;
  }

  size_t Size()
  {
    return s.size();
  }

  bool Empty()
  {
    return s.empty();
  }

private:
  std::stack<T> s;
};

int main()
{
  Queue<int> q;
  q.Push(1);
  q.Push(2);
  std::cout << q.Peek() << std::endl; // 1
  q.Push(3);
  std::cout << q.Pop() << std::endl;  // 1
  q.Push(4);
  q.Push(5);
  std::cout << q.Peek() << std::endl; // 2
  q.Push(6);
  q.Push(7);
  std::cout << q.Pop() << std::endl;  // 2
  std::cout << q.Peek() << std::endl; // 3
  std::cout << q.Pop() << std::endl;  // 3
  q.Push(8);
  std::cout << q.Pop() << std::endl;  // 4
  q.Push(9);
  q.Push(0);
  while(!q.Empty()) {
    std::cout << q.Pop() << std::endl; // 5, 6, 7, 8, 9, 0
  }

  return 0;
}
#include <cassert>
#include <iostream>
#include <stack>

template<class T>
class Queue
{
public:
  Queue()
  {
  }

  ~Queue()
  {
  }

  void Push(T data)
  {
    inbox.push(data);
  }

  // T Pop()
  // {
  //   assert(Size());
  //   if (outbox.empty()) {
  //     while(inbox.size()) {
  //       outbox.push(inbox.top());
  //       inbox.pop();
  //     }
  //   }

  //   T data;
  //   data = outbox.top();
  //   outbox.pop();

  //   return data;
  // }

  T Pop()
  {
    T data = Peek();
    outbox.pop();
    return data;
  }

  T Peek()
  {
    assert(Size());
    if (outbox.empty()) {
      while(inbox.size()) {
        outbox.push(inbox.top());
        inbox.pop();
      }
    }

    return outbox.top();
  }

  size_t Size()
  {
    return inbox.size() + outbox.size();
  }

  bool Empty()
  {
    return inbox.empty() && outbox.empty();
  }

private:
  std::stack<T> inbox;
  std::stack<T> outbox;
};

int main()
{
  Queue<int> q;
  q.Push(1);
  q.Push(2);
  std::cout << q.Peek() << std::endl; // 1
  q.Push(3);
  std::cout << q.Pop() << std::endl;  // 1
  q.Push(4);
  q.Push(5);
  std::cout << q.Peek() << std::endl; // 2
  q.Push(6);
  q.Push(7);
  std::cout << q.Pop() << std::endl;  // 2
  std::cout << q.Peek() << std::endl; // 3
  std::cout << q.Pop() << std::endl;  // 3
  q.Push(8);
  std::cout << q.Pop() << std::endl;  // 4
  q.Push(9);
  q.Push(0);
  while(!q.Empty()) {
    std::cout << q.Pop() << std::endl; // 5, 6, 7, 8, 9, 0
  }

  return 0;
}
#include <cassert>
#include <iostream>
#include <stack>

template<class T>
class Queue
{
public:
  Queue()
  {
  }

  ~Queue()
  {
  }

  void Push(T data)
  {
    inbox.push(data);
  }

  T Pop()
  {
    assert(inbox.size());

    while(inbox.size()) {
      outbox.push(inbox.top());
      inbox.pop();
    }

    T data = outbox.top();
    outbox.pop();

    while(outbox.size()) {
      inbox.push(outbox.top());
      outbox.pop();
    }

    return data;
  }

  T Peek()
  {
    assert(inbox.size());

    while(inbox.size()) {
      outbox.push(inbox.top());
      inbox.pop();
    }

    T data = outbox.top();

    while(outbox.size()) {
      inbox.push(outbox.top());
      outbox.pop();
    }

    return data;
  }

  size_t Size()
  {
    inbox.size();
  }

  bool Empty()
  {
    return inbox.empty();
  }

private:
  std::stack<T> inbox;
  std::stack<T> outbox;
};

int main()
{
  Queue<int> q;
  q.Push(1);
  q.Push(2);
  std::cout << q.Peek() << std::endl; // 1
  q.Push(3);
  std::cout << q.Pop() << std::endl;  // 1
  q.Push(4);
  q.Push(5);
  std::cout << q.Peek() << std::endl; // 2
  q.Push(6);
  q.Push(7);
  std::cout << q.Pop() << std::endl;  // 2
  std::cout << q.Peek() << std::endl; // 3
  std::cout << q.Pop() << std::endl;  // 3
  q.Push(8);
  std::cout << q.Pop() << std::endl;  // 4
  q.Push(9);
  q.Push(0);
  while(!q.Empty()) {
    std::cout << q.Pop() << std::endl; // 5, 6, 7, 8, 9, 0
  }

  return 0;
}
#include <cassert>
#include <iostream>
#include <stack>

template<class T>
class Queue
{
public:
  Queue()
  {
  }

  ~Queue()
  {
  }

  void Push(T data)
  {
    while(outbox.size()) {
      inbox.push(outbox.top());
      outbox.pop();
    }

    outbox.push(data);

    while(inbox.size()) {
      outbox.push(inbox.top());
      inbox.pop();
    }
  }

  // T Pop()
  // {
  //   assert(outbox.size());
  //   T data = outbox.top();
  //   outbox.pop();
  //   return data;
  // }

  T Pop()
  {
    T data = Peek();
    outbox.pop();
    return data;
  }

  T Peek()
  {
    assert(outbox.size());
    return outbox.top();
  }

  size_t Size()
  {
    return outbox.size();
  }

  bool Empty()
  {
    return outbox.empty();
  }

private:
  std::stack<T> inbox;
  std::stack<T> outbox;
};

int main()
{
  Queue<int> q;
  q.Push(1);
  q.Push(2);
  std::cout << q.Peek() << std::endl; // 1
  q.Push(3);
  std::cout << q.Pop() << std::endl;  // 1
  q.Push(4);
  q.Push(5);
  std::cout << q.Peek() << std::endl; // 2
  q.Push(6);
  q.Push(7);
  std::cout << q.Pop() << std::endl;  // 2
  std::cout << q.Peek() << std::endl; // 3
  std::cout << q.Pop() << std::endl;  // 3
  q.Push(8);
  std::cout << q.Pop() << std::endl;  // 4
  q.Push(9);
  q.Push(0);
  while(!q.Empty()) {
    std::cout << q.Pop() << std::endl; // 5, 6, 7, 8, 9, 0
  }

  return 0;
}

以上是关于c_cpp 按堆栈实现队列的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C ++标准库中的堆栈和队列的简单示例

算法 - 栈与队列(C 语言实现)

c_cpp 使用数组实现堆栈

c_cpp 使用类和数组实现堆栈

c_cpp 使用Vector PopBack进行堆栈实现

c_cpp 使用类和链接列表实现堆栈