栈与队列的相关题目

  1. 栈的实现(数组)
  2.  1 template<typename T> class ArrayStack {
     2 public:
     3     ArrayStack(int c = 100): capacity(c), top(-1) {
     4         data = new T[capacity]();
     5     }
     6     ArrayStack(const ArrayStack &rhs):capacity(rhs.capacity), top(rhs.top) {
     7         data = new T[capacity]();
     8 
     9         std::copy(rhs.data, rhs.data + capacity, data);
    10     }
    11 
    12     ArrayStack& operator=(ArrayStack rhs) {
    13         Swap(rhs);
    14 
    15         return *this;
    16     }
    17 
    18     ~ArrayStack() {
    19         delete []data;
    20     }
    21 
    22     void Push(const T& val) {
    23         data[++top] = val;
    24     }
    25 
    26     void Pop()
    27     {
    28         if (top > -1)
    29             --top;
    30     }
    31 
    32     T& Top()
    33     {
    34         return data[top];
    35     }
    36 
    37     const T& Top() const 
    38     {
    39         return data[top];
    40     }
    41 
    42     bool Empty()
    43     {
    44         return top == -1;
    45     }
    46 
    47     std::size_t Size()
    48     {
    49         return top + 1;
    50     }
    51 
    52 private:
    53     void Swap(ArrayStack &rhs) {
    54         std::swap(capacity, rhs.capacity);
    55         std::swap(top, rhs.top);
    56         std::swap(data, rhs.data);
    57     }
    58 
    59     int capacity;
    60     int top;
    61     T *data;
    62 };

     

  3. 两个栈实现一个队列
     1 #include<stack> //std::stack
     2 
     3 template<typename T> class MyQueue {
     4 public:
     5     MyQueue(){}
     6 
     7     int size() {
     8         return s1.size + s2.size(0);
     9     }
    10 
    11     void Push(const T& val) {
    12         s1.push(val);
    13     }
    14 
    15     void pop() {
    16         if (s2.empty()) {
    17             while (!s1.empty()) {
    18                 s2.push(s1.top());
    19                 s1.pop();
    20             }
    21         }
    22 
    23         return s2.pop();
    24     }
    25 
    26     T front() {
    27         if (s2.empty()) {
    28             while (!s1.empty()) {
    29                 s2.push(s1.top());
    30                 s1.pop();
    31             }
    32         }
    33 
    34         return s2.top();
    35     }
    36 
    37 private:
    38     std::stack<T> s1, s2;
    39 
    40 };

     

  4. 写一个算法将栈里的元素升序排列。栈的实现未知,算法只能借助栈完成,课使用的函数有push, pop、top、empty等。
    时间复杂度为O(n^2),空间复杂度O(n)
     1 template<typename T>
     2 stack<T> SortStack(stack<T> &s) {
     3     stack<T> res;
     4 
     5     while (!s.empty()) {
     6         T temp = s.top();
     7         s.pop();
     8 
     9         while (!res.empty() && res.top() > temp) {
    10             s.push(res.top());
    11             res.pop();
    12         }
    13 
    14         res.push(temp);
    15     }
    16 
    17     return res;
    18 }

     

  5. 栈的压入、弹出序列
    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出序列。假设压入栈的所有数字均不相等。例如,序列1,2,3,4,5是某栈的压栈序列,序列4,5,3,2,1是该栈的对应的一个弹出序列,但4,3,5,2,1就不可能是该压栈序列的弹出序列。
     1 bool IsPopOrder(const int *pushorder, const int * poporder, int length) {
     2     if (pushorder == NULL || poporder == NULL || length <= 0)
     3         return false;
     4 
     5     std::stack<int> s;
     6 
     7     const int *pop_val = poporder;
     8     const int *push_val = pushorder;
     9 
    10     while (pop_val - poporder < length) { 
    11         while (s.empty() || s.top() != *pop_val) {
    12             if (push_val - pushorder == length) 
    13                 break;
    14 
    15             s.push(*push_val++);
    16         }
    17 
    18         if (s.top() != *pop_val)
    19             return false;
    20 
    21         s.pop();
    22         pop_val++;
    23     }
    24 
    25     if (s.empty() && pop_val - poporder == length)
    26         return true;
    27     else
    28         return false;
    29 
    30 }

     

  6. 队列的实现(数组)

    为了方便起见,约定:初始化建空队时,令
          front=rear=0,
      当队空时:front=rear
      当队满时:front=rear 亦成立
      因此只凭等式front=rear无法判断队空还是队满。  有两种方法处理上述问题:
        (1)另设一个标志位以区别队列是空还是满。
        (2)少用一个元素空间,约定以“队列头指针front在队尾指针rear的下一个位置上”作为队列“满”状态的标志。即:
      队空时: front=rear
      队满时: (rear+1)%capacity=front

      front指向队首元素,rear指向队尾元素的下一个元素。

     1 #include <algorithm> //std::swap
     2 #include<cstddef> //std::size_t
     3 
     4 template <typename T> class MyQueue {
     5 public:
     6     MyQueue(int c = 100):capacity(c), front(0), rear(0) {
     7         data = new T[capacity]();
     8     }
     9 
    10     MyQueue(const MyQueue &rhs): cpacity(rhs.capacity), front(rhs.front), rear(rhs.rear) {
    11         std::copy(rhs.data, rhs.data + capacity, data);
    12     }
    13 
    14     MyQueue& operator=(MyQueue rhs) {
    15         Swap(rhs);
    16 
    17         return *this;
    18     }
    19 
    20     ~MyQueue() {
    21         delete []data;
    22     }
    23 
    24     void Push(const T& val) {
    25         if ((rear + 1) % capacity != front) {
    26             data[rear] = val;
    27             rear = (rear + 1) % capacity;
    28         }
    29     }
    30 
    31     void Pop() {
    32         if (!Empty()) {
    33             front = (front + 1) % capacity;
    34         }
    35     }
    36 
    37     bool Empty() {
    38         return front == rear;
    39     }
    40 
    41     size_t Size() {
    42         return (rear - front + capacity) % capacity;
    43     }
    44 
    45     T& Front() {
    46         if (!Empty()) {
    47             return data[front];
    48         }
    49     }
    50     
    51     const T& Front() const {
    52         if (!Empty())
    53             return data[front];
    54     }
    55 
    56     T& Back() {
    57         if (!Empty())
    58             return rear == 0 ? data[capacity - 1] : data[rear - 1];
    59     }
    60 
    61     const T& Back() const {
    62         if (!Empty())
    63             return rear == 0 ? data[capacity - 1] : data[rear - 1];
    64     }
    65 private:
    66     void Swap(MyQueue& rhs) {
    67         std::swap(data ,rhs.data);
    68         std::swap(capacity, rhs.capacity);
    69         std::swap(front, rhs.front);
    70         std::swap(rear, rhs.rear);
    71     }
    72 
    73     T *data;
    74     int capacity;
    75     int front;
    76     int rear;
    77 };

     

  7. 用两个队列实现一个栈
     1 #include<queue>
     2 #include<cstddef> //std::size_t
     3 
     4 template<typename T>
     5 class MyStack {
     6 public:
     7     MyStack(){}
     8 
     9     size_t size() {
    10         return q1.size() + q2.size();
    11     }
    12 
    13     bool empty() {
    14         return q1.empty() && q2.empty();
    15     }
    16 
    17     void push(const T& val) {
    18         if (q1.empty()) {
    19             q2.empty() ? q1.push(val) : q2.push(val);
    20         } else {
    21             q1.push(val);
    22         }
    23     }
    24 
    25     void pop() {
    26         if (!q1.empty()) {
    27             while (q1.size() > 1) {
    28                 q2.push(q1.front());
    29                 q1.pop();
    30             }
    31             q1.pop();
    32         } else {
    33             while (q2.size() > 1) {
    34                 q1.push(q2.front());
    35                 q2.pop();
    36             }
    37             q2.pop();
    38         }
    39     }
    40 
    41     T top() {
    42         return q1.empty() ? q2.back() : q1.back();
    43     }
    44 
    45 private:
    46     std::queue<T> q1, q2;
    47 };

     

posted @ 2014-12-31 17:03  vincently  阅读(365)  评论(0编辑  收藏  举报