两个栈的配合

转:http://zhedahht.blog.163.com

  1. 用两个栈实现队列。
  2. 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小min函数。在该栈中,调用min和push及pop的时间复杂度都是O(1)。

1、思路:

  一个栈是先入后出,两个配合就是先入先出。一个栈负责push,一个负责pop,当pop栈没有数据了需要去push栈中去拿。

《两个栈的配合》
《两个栈的配合》
Queue

 1 #include <iostream>
 2 #include <stack>
 3 #include <exception>
 4 
 5 using namespace std;
 6 
 7 template <typename T> class Queue
 8 {
 9     public:
10         Queue();
11         ~Queue();
12         void appendTail(const T& node);
13         T deleteHead();
14     private:
15         stack<T> stack1;
16         stack<T> stack2;
17 };
18 
19 template <typename T> Queue<T>::Queue()
20 {
21 
22 }
23 
24 template <typename T> Queue<T>::~Queue()
25 {
26 
27 }
28 
29 template <typename T> void Queue<T>::appendTail(const T& node)
30 {
31     stack1.push(node);
32 }
33 
34 template <typename T> T Queue<T>::deleteHead()
35 {
36     if (stack2.size() == 0)
37     {
38         while (stack1.size())
39         {
40             T temp = stack1.top();
41             stack1.pop();
42             stack2.push(temp);
43         }
44     }
45     if (stack2.size() == 0)
46         throw;
47     
48     T result = stack2.top();
49     stack2.pop();
50 
51     return result;
52 }
53 
54 int main()
55 {
56     Queue<char> CQueue;
57     CQueue.appendTail('a');
58     CQueue.appendTail('b');
59     CQueue.appendTail('c');
60     char head = CQueue.deleteHead();
61     printf("%c\t", head);
62     head = CQueue.deleteHead();
63     printf("%c\t", head);
64     head = CQueue.deleteHead();
65     printf("%c\t", head);
66     printf("\n");
67 
68     return 0;
69 }

 

2、思路:

  如果设置一个全局最小变量,当最小变量弹出时,就不知道次小变量了。需要一个辅助栈,同步数据栈的量,存放当前位置之前最小的数据。

《两个栈的配合》
《两个栈的配合》
StackWithMin

 1 #include <stack>
 2 #include <iostream>
 3 #include <assert.h>
 4 
 5 using namespace std;
 6 
 7 template <typename T> class StackWithMin
 8 {
 9     public:
10         StackWithMin();
11         ~StackWithMin();
12         const T& min() const;
13         void push(const T& value);
14         void pop();
15     private:
16         stack<T> sData;
17         stack<T> sMin;
18 };
19 
20 template <typename T> StackWithMin<T>::StackWithMin()
21 {
22 
23 }
24 
25 template <typename T> StackWithMin<T>::~StackWithMin()
26 {
27 
28 }
29 
30 template <typename T> void StackWithMin<T>::push(const T& value)
31 {
32     sData.push(value);
33     if (sMin.size() > 0 && sMin.top() <= value)
34         sMin.push(sMin.top());
35     else
36         sMin.push(value);
37 }
38 
39 template <typename T> void StackWithMin<T>::pop()
40 {
41     assert(sData.size() > 0 || sMin.size() > 0);
42     sData.pop();
43     sMin.pop();
44 }
45 
46 template <typename T> const T& StackWithMin<T>::min() const
47 {
48     assert(sMin.size() > 0);
49     return sMin.top();
50 }
51 
52 int main()
53 {
54     StackWithMin<int> stack_min;
55     stack_min.push(3);
56     int min = stack_min.min();
57     printf("min:%d\n", min);
58 
59     stack_min.push(4);
60     min = stack_min.min();
61     printf("min:%d\n", min);
62 
63     stack_min.push(2);
64     stack_min.push(1);
65     
66     min = stack_min.min();
67     printf("min:%d\n", min);
68     
69     stack_min.pop();
70     min = stack_min.min();
71     printf("min:%d\n", min);
72     return 0;
73 }
74     

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/wangpengjie/archive/2013/04/08/3008789.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞