kevin55

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素。 要求函数 min、push 以及 pop 的时间复杂度都是 O(1)。 

  1 template<typename T>
  2 struct MinStackElement
  3 {
  4     T data;
  5     T min;
  6 };
  7 
  8 template<typename T>
  9 struct MinStack
 10 {
 11     MinStack() : pData(NULL), size(0), top(0){}
 12     MinStackElement<T>* pData;
 13     int size;
 14     int top;
 15 };
 16 
 17 template<typename T>
 18 void MinStackInitialize(MinStack<T>& s, int maxSize)
 19 {
 20     MinStackFree(s);
 21      s.pData = (MinStackElement<T>*)malloc(maxSize*sizeof(MinStackElement<T>));
 22      memset(s.pData, 0, maxSize*sizeof(MinStackElement<T>));
 23      s.size = maxSize;
 24      s.top = 0;
 25  }
 26 
 27  template<typename T>
 28  void MinStackFree(MinStack<T>& s)
 29  {
 30      if (s.pData)
 31      {
 32          free(s.pData);
 33          s.pData = nullptr;
 34      }
 35 
 36      s.size = 0;
 37      s.top = 0;
 38  }
 39 
 40  template<typename T>
 41  void MinStackPush(MinStack<T>& s, T data)
 42  {
 43      if (s.size == 0)
 44      {
 45          printf("Stack is not initialized.\n");
 46          return;
 47      }
 48 
 49      if (s.top == s.size)
 50      {
 51          printf("Stack is full.\n");
 52          return;
 53      }
 54 
 55      s.pData[s.top].data = data;
 56      s.pData[s.top].min = (s.top == 0 ? data : s.pData[s.top - 1].min);
 57      if (s.pData[s.top].min > data)
 58      {
 59          s.pData[s.top].min = data;
 60      }
 61 
 62      wcout << L"stack push " << data << endl;
 63      s.top++;
 64  }
 65 
 66  template<typename T>
 67  T MinStackPop(MinStack<T>& s)
 68  {
 69     if (s.top == 0)
 70     {
 71         printf("Stack is empty.\n");
 72         return -1;
 73     }
 74 
 75     T data = s.pData[--s.top].data;
 76     wcout << L"stack pop " << data << endl;
 77 
 78     return data;
 79  }
 80 
 81 template<typename T>
 82 T MinStackGetMin(const MinStack<T>& s)
 83 {
 84     if (s.top == 0)
 85     {
 86         printf("Stack is empty.\n");
 87         return 0;
 88     }
 89 
 90     T min = s.pData[s.top - 1].min;
 91     printf("Min :%d\n", min);
 92 
 93     return min;
 94 }
 95 
 96 int _tmain(int argc, _TCHAR* argv[])
 97 {
 98     int min = 0;
 99     MinStack<int> s;
100 
101     MinStackPush(s, 9);
102 
103     MinStackInitialize(s, 10);
104     MinStackPush(s, 9);
105     MinStackPush(s, 5);
106     MinStackPush(s, 6);
107     MinStackPush(s, 4);
108     MinStackPop(s);
109     MinStackGetMin(s);
110     MinStackPush(s, 3);
111     MinStackPush(s, 8);
112     MinStackPush(s, 2);
113 
114     MinStackPop(s);
115 
116     MinStackPush(s, 7);
117     MinStackPush(s, 1);
118 
119     MinStackGetMin(s);
120 
121     MinStackPop(s);
122     MinStackGetMin(s);
123     MinStackPop(s);
124     MinStackGetMin(s);
125     MinStackPop(s);
126     MinStackGetMin(s);
127     MinStackPop(s);
128     MinStackGetMin(s);
129     MinStackPop(s);
130     MinStackGetMin(s);
131     MinStackPop(s);
132     MinStackPop(s);
133     MinStackPop(s);
134     MinStackGetMin(s);
135 
136     MinStackFree(s);
137 
138     return 0;
139 }

>test.exe
Stack is not initialized.
stack push 9
stack push 5
stack push 6
stack push 4
stack pop 4
Min :5
stack push 3
stack push 8
stack push 2
stack pop 2
stack push 7
stack push 1
Min :1
stack pop 1
Min :3
stack pop 7
Min :3
stack pop 8
Min :3
stack pop 3
Min :5
stack pop 6
Min :5
stack pop 5
stack pop 9
Stack is empty.
Stack is empty.

posted on 2016-07-01 15:17  kernel_main  阅读(392)  评论(0编辑  收藏  举报