Minimum stack principle and basic operations

Source: Internet
Author: User

Minimum stack principle and basic operations
In addition to common stack operations, the minimum stack can return and update the minimum value in the stack with the time complexity of O (1.
Algorithm Description:
Status Push (minStack * S, ElemType e); // Insert a new element e into Stack S
In fact, the value stored in the stack is not the value of e, but the value of e-S-> min, that is, S-> data [++ S-> top] = e-S-> min;
If S-> data [S-> top] <0, that is, the e value of the new stack is smaller than the original min value, update min, that is, S-> min = e; otherwise, min remains unchanged.


Status Pop (minStack * S, ElemType * e); // Delete the stack top element of stack S and assign the element value to e
Corresponding to the inbound stack operation, the outbound stack operation also needs some changes.
If S-> data [S-> top]> 0, output S-> data [S-> top] + S-> min;
Otherwise, output S-> min. In this case, because the minimum value has already exceeded the stack, you must update min, that is, S-> min-= S-> data [S-> top];


The algorithms for other operations are very simple, so we will not explain them one by one. Note that when outputting all elements, we must output them according to the features of stack first-in-first-out. Otherwise, min cannot be correctly updated.
In addition, we can imitate the implementation method of the minimum stack to implement a maximum stack. The basic idea is the same, that is, the formula has changed.
That is, when the stack is imported, S-> data [++ S-> top] = S-> max-e; if S-> data [S-> top] <0, s-> max = e;
If S-> data [S-> top]> 0 during stack export, output S-> max-S-> data [S-> top];
Otherwise, output S-> max, and update max, that is, S-> max + = S-> data [S-> top];


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Define MAXSIZE 10 # define OK 1 # define ERROR 0 # define TRUE 1 # define FALSE 0 typedef int ElemType; typedef int Status; // function type, the value is the function result status code, such as OK and other typedef struct minStack {ElemType data [MAXSIZE]; // Array Storage stack element int top; // The stack top pointer ElemType min; // minimum value in the stack} minStack; Status InitStack (minStack * S); // create an empty stack S Status DisplayStack (minStack S ); // output the Status GetTop (minStack * S, ElemType * e) of all elements of stack S; // use e to return the Status Push (minStack * S, elemType e); // Insert the new element e Status Pop (minStack * S, ElemType * e) into Stack S; // Delete the top element of stack S, and assign this element value to e ElemType MinOfStack (minStack * S); // return the minimum value of int main (void) {minStack a; ElemType * p, e = 0; int I; InitStack (& a); // create an empty stack for (I = 0; I
     
      
= 3) {Pop (& a, & e); // Delete the top element of stack S and assign the element value to e} DisplayStack (); printf ("minimum value: % d \ n", MinOfStack (& a); return 0;} ElemType MinOfStack (minStack * S) // return the minimum value of the stack {return S-> min;} Status InitStack (minStack * S) // create an empty stack S {S-> top =-1; return OK;} Status GetTop (minStack * S, ElemType * e) // use e to return the top element of stack S {if (S-> top =-1) {return ERROR;} if (S-> data [S-> top]> 0) * e = S-> data [S-> top] + S-> min; else * e = S-> min; return OK;} S Tatus Push (minStack * S, ElemType e) // Insert a new element e in stack S {if (S-> top = MAXSIZE-1) {return ERROR ;} if (S-> top =-1) {S-> min = e; S-> data [++ S-> top] = 0 ;} else {S-> data [++ S-> top] = e-S-> min; if (S-> data [S-> top] <0) s-> min = e;} return OK;} Status Pop (minStack * S, ElemType * e) // Delete the top element of stack S, and assign the element value to e {if (S-> top =-1) {return ERROR;} if (S-> data [S-> top]> 0) * e = S-> data [S-> top] + S-> min; else {* e = S-> min; S-> min-= S-> data [S-> top];} S-> top --; return OK;} Status DisplayStack (minStack S) // all elements of output stack S {int I; if (S. top =-1) {printf ("none! \ N "); return ERROR;} for (I = S. top; I> = 0; I --) {if (S. data [I]> 0) printf ("data [% d] = % d \ t", I, S. data [I] + S. min); else {printf ("data [% d] = % d \ t", I, S. min); S. min-= S. data [I] ;}} printf ("\ n"); return OK ;}
     
    
   
  
 


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.