C Language Enhancement (2) stack design for minimum elements

Source: Internet
Author: User
Tags pop value stack pop

C Language Enhancement (2) stack design for minimum elements

 

How to Use the stack "advanced and later features, how to skillfully use the auxiliary stack to define shared static member variables in the struct Question

It seems simple to find the minimum value function. There are many ideas. I first thought that every push into the stack would be sorted to make the top of the stack always the smallest element. Then I found this was a stupid and stupid idea, first, this will change the stack structure, and second, it will not meet the time complexity requirements of the question. It's a bit silly to be stupid. Since we cannot change the structure of the original stack, why don't we get two stacks? The idea of the auxiliary stack comes from this. Next is the issue of time complexity. Obviously, sorting every time is not feasible. Can we record the next minimum value of each minimum value? That is to say, when the current minimum value is pop, I want to know which is the next minimum value. At this time, the [advanced and later] features of the stack will come in handy. This is the end of brainstorming. The following are specific ideas:Firmly grasp the features of the stack [advanced and later], simulate various practical situations, and obtain Algorithms
Auxiliary Stack:Add an auxiliary stack to the stack structure, called the "minimum stack", to store the historical records of the minimum value. Therefore, the top of the stack is the minimum element of the current stack.
Algorithm Implementation:-- Push: When the element value pushed in is greater than or equal to the top of the stack with the minimum value, the minimum value stack remains unchanged, because according to the characteristics of the stack, only all the elements above the current minimum value element go out, only the minimum element can pop out. Otherwise, the minimum element is used as the minimum value.
If the element value pushed in is smaller than the top of the stack of the minimum stack, the element is placed in the minimum stack.
-- Pop: When the elements that pop Goes out are not the top of the minimum stack, the minimum stack remains unchanged.
If the pop value is the current minimum value, the minimum value Stack also pops an element. At this time, the top of the minimum value stack is the next minimum value element.

Source code
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std;/** subject: defining the data structure of the stack requires adding a min function to identify the minimum elements of the stack. the time complexity of min, push, and pop functions is O (1) the idea is to firmly grasp the features of the stack [advanced and later], simulate various actual situations, and find that an auxiliary stack is added to the algorithm stack structure. This is called the [minimum Stack ], store the minimum value history. Therefore, the top of the stack is the minimum element of the current stack. When the element value pushed in is greater than or equal to the top of the stack, the minimum value stack remains unchanged because according to the stack features, the minimum value element can pop out only when all the elements above the current minimum value element go out. Otherwise, put this element into the minimum value stack. When the elements that pop Goes out are not the minimum value stack top, the minimum value stack remains unchanged. Otherwise, the minimum value stack pop goes out of the stack top so that if the value of pop Goes out is the current minimum value, the minimum value Stack also pops out an element. At this time, the top of the stack of the minimum value stack is the next minimum value element * // * Create a stack node struct value nextNode next node push () into the stack function pop () out-of-stack function min () Evaluate the minimum value function push2 () into the stack and obtain the minimum value pop2 () out of the stack and the minimum value */struct StackNode {int value; static StackNode * minNode; StackNode * nextNode; /** push () function value returned to the stack top */StackNode * push (int value) {StackNode * top = new StackNode (); if (NULL! = This) {top = this; StackNode * push = new StackNode (); push-> value = value; push-> nextNode = top; top = push ;} else {top-> nextNode = NULL; top-> value = value;} cout <inbound stack, value = <
    
     
Value <
     
      
NextNode;} else {cout <stack is empty <
      
        Value) {cout <Minimum value stack into Stack <
       
         Push (value) ;}} else {// null, put it directly in cout <
        
          Push (value) ;}} else {// pop // non-empty and the pop value is equal to the minimum value, then the minimum stack popif (NULL! = MinNode) & (minNode-> value = value) {cout <Minimum value stack output stack <
         
           Pop () ;}} if (NULL! = MinNode) cout <minimum value of the current stack = <
          
            Value <
           
             Push (value); this-> min (true, value); return node;} // pop + minStackNode * pop2 () {int value = this-> value; stackNode * node = this-> pop (); this-> min (false, value); return node ;}; // initialize the static variable of the struct! StackNode * StackNode: minNode = NULL; StackNode * stack; void main () {stack = stack-> push2 (5); stack = stack-> push2 (2 ); stack = stack-> push2 (4); stack = stack-> push2 (1); stack = stack-> push2 (1); while (NULL! = Stack) {stack = stack-> pop2 ();} system (pause );}
           
          
         
        
       
      
     
    
   
  
 



To sum up, all stack-related questions should be remembered.

 

Related Article

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.