Stack containing min functions, including min Functions

Source: Internet
Author: User

Stack containing min functions, including min Functions
Question: To define the stack data structure, add a min function to obtain the minimum element of the stack. The time complexity of the min, push, and pop functions is O (1 ).

Analysis: in addition to the stack required by the question, a new stack is created to record the minimum value. Each time data is pushed in the original stack, it is compared with the top element of the stack in the minimum stack. If the new value is small, the new value is pushed in the minimum stack; otherwise, the top element of the stack is pushed again. pop, you just need to pop the minimum stack. in this way, the min function only needs to return the top element of the stack of the minimum stack.

 1 #include<stack> 2 #include<assert.h> 3 #include<stdio.h> 4 #include<tchar.h> 5  6 template<typename T>class  StackWithMin 7 { 8 public: 9     StackWithMin(void) {}10     virtual ~StackWithMin(void) {}11 12     T& top(void);13     const T& top(void) const;14 15     void push(const T& value);16     void pop(void);17 18     const T& min(void) const;19 20     bool empty() const;21     size_t size() const;22 23 private:24     std::stack<T> m_data;25     std::stack<T> m_min;26 };27 28 template <typename T>void StackWithMin<T>::push(const T& value)29 {30     m_data.push(value);31 32     if(m_min.size() == 0 || value < m_min.top())33         m_min.push(value);34     else35         m_min.push(m_min.top());36 }37 38 39 template <typename T>void StackWithMin<T>::pop()40 {41     assert(m_data.size() > 0 && m_min.size() > 0);42 43     m_data.pop();44     m_min.pop();45 }46 47 template<typename T>const T& StackWithMin<T>::min() const48 {49     assert(m_data.size()>0 && m_min.size() >0);50 51     return m_min.top();52 }53 54 template<typename T>T& StackWithMin<T>::top()55 {56     return m_data.top();57 }58 template <typename T>const T& StackWithMin<T>::top() const59 {60     return m_data.top();61 }62 63 template <typename T> bool StackWithMin<T>::empty() const64 {65     return m_data.empty();66 }67 68 template<typename T> size_t StackWithMin<T>::size() const69 {70     return m_data.size();71 }72 73 void Test(char* testName, const StackWithMin<int>& stack, int expected)74 {75     if(testName != NULL)76         printf("%s begins: \n", testName);77     if(stack.min() == expected)78         printf("Passed.\n");79     else80         printf("Failed.\n");81 }82 83 int main()84 {85     StackWithMin<int> stack;86     stack.push(3);87     printf("min = %d\n", stack.min());88     stack.push(4);89     printf("min = %d\n", stack.min());90     stack.push(2);91     printf("min = %d\n", stack.min());92     stack.pop();93     printf("min = %d\n", stack.min());94     95     return 0;96 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.