Stack--21 containing the Min function

Source: Internet
Author: User

Define the data structure of the stack, implement a min function in the type that can get the smallest element of the stack. In this stack, the time complexity of calling Min, push, and Pop is O (1).


First of all, the stack is characterized by "advanced, LIFO," So, for pop and push two operations are directly placed on the top of the stack and directly on the top of the stack to delete elements, if you want to find the minimum value in the stack min, because the time complexity is required to O (1), so certainly cannot traverse the stack to find the smallest You can think of using two stacks in the data structure of this stack, one stack is used to store the deleted data normally, and the other stack is used to store the minimum value in the current stack;

When the first push into the stack, the data is also pushed into the Min stack, and the stack top element in the min stack is the minimum value of the current stack, when the push data is smaller than the stack top element in the Min stack, the push data is put into the min stack, When the push data is larger than the stack top element in the Min stack, the top element of the min stack is then pressed into the stack again, so that, in this way, the stack top element in the Min stack is always the minimum value of the current data stack, while the pop data is On the stack top element of the pop data stack, the top element of the stack is also popped, so that the top element of the stack in min stack is the minimum value, and the time Complexity is O (1);

The above content can be drawn as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/2B/wKiom1c5sKbBtE8GAAAVOpCQabU097.png "title=" stack. png " alt= "Wkiom1c5skbbte8gaaavopcqabu097.png"/>

The program is as follows:

#include  <iostream> #include  <stdlib.h>using namespace std;template < Class t>class my_stack{public:    my_stack ()//The default constructor for the stack, the pointer is empty at the beginning and the capacity is designed as 3         :_data (NULL)         ,_min ( NULL)         ,_size (0)          , _capacity (3)     {}      void my_push (T data)// Push Data     {           if (_data  == null)//When data is first placed         {             _data = new T[_capacity];             _min = new T[_capacity];             _data[_size] = data;             _min[_size++] = data;             return;        }              _checkcapacity ();//Check capacity         _data[_ Size] = data;        if (data < _min[_size-1])// If the data to be placed is smaller than the top element of the stack, put it directly, otherwise, put the top element again             _min[_ size] = data;        else             _min[_size] = _min[_size-1];         ++_size;    }    void my_pop ()//pop data      {&nbsP;       if (_data == null)              return;        --_size;     }    t& min ()//Check out minimum     {         if (_data == null)         {             cout<< "No data ..." << Endl;            exit (0);         }        return _min[_size-1];     }    ~my_stack ()//destructor     {         if (_data != null)         {             delete[] _data;             delete[] _min;            _ data = null;            _min =  NULL;            _size = 0;         }    }    void print_ Stack ()//print stack element     {       if (_data != null)         {             for (int i = 0; i < _size; ++i)              {                 cout<<_data[i]<< " ";             }            cout<<endl;         }    }private:    void _ Checkcapacity ()//Capacity Check     {        if ((_size+1)  <= _capacity)         {             _capacity *= 2;             T *tmp_data = new T[_capacity];             T *tmp_min = new T[_capacity];             for (int i = 0; i <  _size; ++i)             {                 tmp_data[i] = _data[i];                 tmp_min[i] = _min[i];             }             swap (_data, tmp_data);             swap (_min, tmp_min);             delete[] tmp_data;             delete[] tmp_min;        }    }private:     t *_data;    t *_min;    size_t _size ;     size_t _capacity;}; Int main () {    my_stack<int> stack;    stack.my_push (3 );     stack.my_push (5);     stack.my_push (1);     Stack.my_push (2);     stack.my_push (0);     stack.my_push (6);     stack.print_stack ();    cout<< "min data: " << Stack.min () <<endl;    stack.my_pop ();    cout<< "min  data:  "<<stack.min () <<endl;    stack.my_pop ();     cout<< "min data: " <<stack.min () <<endl;    stack.my_pop ();     cout<< "min data: " <<stack.min () <<endl;     stack.my_pop ();    cout<< "min data: " <<stack.min () <<endl ;     return 0;} 


To run the program:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/29/wKioL1c5xFfAIB9CAAAGxzdUkxc207.png "title=" Stack.png "alt=" Wkiol1c5xffaib9caaagxzdukxc207.png "/>


As you can see, each POP data can output the minimum value in the current stack, and the time Complexity is O (1).



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1774066

Stack--21 containing the Min function

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.