Idea: 1: Add two fields to the data structure of the stack, such as
typedef struct {
int Data[max]; // all numbers in the storage stack
int top; // the top element of the storage stackDatathe position in the array
int min; // Minimum Value
int second;// sub-decimal value
}stack;
Pop,push the top element of the stack, so it's O (1)
Min Time takes the Min field of the stack, so is also O (1)
everyPushwhen comparing, if the currentPushthe element ratioStack->minSmall, replace with the current elementStack->min, with the originalminReplaceSecond. If the currentPushthe element ratioStack->minlarge, but thanSecondSmall, replace with the current elementStack->second. Thus reaching theminthe effect,The procedure is as follows
int push (Stack * s,int x) {
ASSERT (S!=null);
if (S->top>=max)// Prevent Stack Overflow
{
printf ("stackoverload!");
return-1;
}
s->data[s->top++]=x;//Pushthe basic function of thexthe position of the top element of the stack is self-increasing.1
if (x < s->min)//If the newly pressed x is smaller than the current min, replace min
S->second = s->min;
s->min=x;
else if (x< s->second) // Otherwise, if x is smaller than second , this replaces second
S->second= x;
// otherwise,minand theSecondIt 's not the same .
return 0;
}
everyPopthe time to compare, ifPopthe element ismin, you useSecondReplacemin(it's used here. )Second). Thus reaching theminthe effect,The procedure is as follows
int pop (stack *s,int *x)
{
ASSERT (S!=null);
if (s->top<=0)
{
printf ("stackempty!");
return-1;
}
(*x) =s->data[s->top--];//Popthe basic function of thexThe position of the top element of the stack is self-reducing .1
if (x==s->min)// ifxexactly equalsmin, it is necessary toSecondGivemin
s->min=s->second;
return 0;
}
int min (Stack *s,int *x)
{
ASSERT (S!=null);
(*x) =s->min;
return 0;
}
Idea 2: Set the secondary stack to the bottom, record the minimum value for each state, get the current value of the secondary stack each time it is inserted, and compare the inserted values
If the small is inserted into the minimum stack is it, otherwise is the original minimum value, in this way, pop,push,min three are
O (1) algorithm.
if the maximum value is required, then set another ass2 to record the maximum value of the current stack.
typedef struct {
int Data[max];
int top;
}stack;
STATIC int Push_stack (stack *s,int x)
{
ASSERT ((s!=null));
if (S->top>=max)
{
printf ("Stack overload");
return-1;
}
s->data[s->top++]=x;
return 0;
}
STATIC int pop_stack (Stack *s,int *x)
{
ASSERT (S!=null);
if (s->top<=0)
{
printf ("Stack Empty");
return-1;
}
(*x) =s->data[s->top--];
return 0;
}
int push (stack *main,stack *ass,int x)
{
ASSERT ((main!=null) && (ass!=null));
int temp;
Pop_stack (ass,&temp);
Push_stack (MAIN,X);
if (x<temp)
{
Push_stack (ass,temp);
Push_stack (ASS,X);
}
Else
{
Push_stack (ass,temp);
Push_stack (ass,temp);
}
return 0;
}
int pop (stack *main,stack *ass,int *x)
{
ASSERT ((main!=null) && (ass!=null));
int temp;
Pop_stack (ass,&temp);
Pop_stack (MAIN,X);
return 0;
}
int min (Stack *ass,int *x)
{
ASSERT ((main!=null) && (ass!=null));
Pop_stack (ASS,X);
Push_stack (*X);
return 0;
}
Stack for min value in O (1) time