Stack with min function + one array implementation two stacks + two array implementation min stack

Source: Internet
Author: User

1. Title describes the data structure of the definition stack, implement a min function that can get the smallest element of the stack in this type. Idea: Use a secondary stack to hold the minimum value
Stack 3,4,2,5,1
Auxiliary stack 3,2,1
Each in the stack once, compared to the top of the auxiliary stack size, if the small on the stack, if the large is not in the stack of the current auxiliary stack, when the stack, the auxiliary stack elements are equal to the stack.
classSolution { Public: Stack<int>mystack1;//Auxiliary Stack stack<int>minstack;//Minimum StackvoidPushintvalue) {        if(Minstack.empty ()) {Minstack.push (value); } Else if( () >value)        {Minstack.push (value);    } mystack1.push (value); }    voidpop () {if( () = ()) Minstack.pop ();                  Mystack1.pop (); }    intTop () { (); }    intmin () {//if (!minstack.empty ()) (); }};
2. Two arrays for min stack problem

Title: Define the data structure of the stack, and implement a Min function in the stack to get the smallest element in the stack. In this stack, the time complexity of calling Min, PUSH, and Pop is O (1).
According to the topic can be seen, this time we are involved in the data structure is the stack (stack), first look at the stack of the introduction: Stack aka stacks, it is an operation constrained linear table. The limitation is that only one end of the table is allowed to insert and delete operations. This end is called the top of the stack, and the opposite end is called the bottom of the stack. Inserting a new element into a stack, also known as a stack, a stack, or a stack, is to put a new element on top of the stack, making it a new stack top element, and removing an element from a stack called a stack or a stack, which removes the top element of the stack and makes its adjacent elements a new top element of the stack.
The above description is very detailed, simple point is a sentence, the stack is a first-in and out of the data structure, based on this feature, each time the first push into the stack of elements, always the last to be ejected, and each pop-up element, it is certainly the last element pressed into, based on this feature, the stack to do sort is more troublesome, The time complexity required in the topic is O (1), that is, it is not possible to use the traversal to get the minimum value, which one would say, we can record a global variable, the initial value is pressed into the first element in the stack, and then each time the stack is compared to the element in the variable and the size of the stack element, To ensure that the global variable is the smallest numeric value, the idea is to meet the time complexity of O (1) requirements, but if you do the bomb stack operation? What if the smallest element in the stack pops up? This time your global variables will be wasted, so this approach is not feasible.
What should we do then? In fact, in the pressure stack operation of the smallest element stored up the idea is not a problem, but only consider a minimum element, not consider the second small element, the third small element and so on, and in order to ensure that the smallest element pop-up, you can immediately get the second small element, we have to use a secondary storage space to complete this matter.
If the stack of our stored elements is the data stack, the auxiliary structure used to store the smallest element is the temp stack (the stack is designed to be consistent with the data stack and easy to understand), so we have two stacks, and we should also notice that our title is "two arrays to implement Min stack". Now the array can come in handy.
Arrays should be the most contact, the most widely used data structure, the definition of simple, easy to understand, directly through the subscript access to data, can achieve O (1) time complexity, but also there are many defects in the array, such as size once determined, can not be changed, the use of the process if the slightest careless, It is possible to cross the border to bring unpredictable errors. But in our problem, the first is to make use of an array to implement the two stacks, there are the following two ideas:
1, the definition of two subscript index1 and Index2, the initial value index1 = 0, Index2 = length-1, where length is an array length, each time a stack 1 is pressed into the element, index1 1, each time the stack 2 is pressed into the element, Index2 minus 1, the stack is the opposite, The maximum depth of the stack is half of the length, so that the index value is pointed to the top of the stack to meet the requirements;
2, also define two index values INDEX1 and INDEX2, the initial value index1 = 0, Index2 = 1, each time the stack, the corresponding index value is increased 2, the stack is reduced by 2, equivalent to an array in two, even the number of bits as a stack, odd bit as a stack, two stacks do not interfere with each other, Independent use, can also meet the requirements.
Compare the above two methods can see the idea 1 more flexible, and almost will not waste the array space, assuming two stacks of depth of M and N, if using the idea of 2, then regardless of the relationship between M and N, will inevitably waste | M-n| a space, and thinking 1 does not have this problem, but in our problem, the data stack and the size of the temp stack, in fact, with that kind of thinking can be

Reference: Data structure and algorithm (c) two arrays implementation min stack problem

3. Use an array to implement two stacks to maximize space

Pay attention to the relationship of subscript.

/*The ***************************************************** \file array implements two stacks. cpp* \date 2017/03/17 23:02* \author ranjiewen* \contact: [email protected] * \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n * \ Problem Analysis: Two stacks from the two sides of the array start to grow in the middle, when the stack top pointer of two stacks meet, indicating that two stacks are full *****************************************************/#include<stdio.h>#include<stdlib.h>#defineMaxsive 20typedefintElementtype;typedefstructDstack *Dstack_;structdstack{ElementType data[maxsive]; intTOP1;//Stack top pointer of stack 1    intTOP2;//Stack top pointer of stack 2};voidPush (structDstack *ptrs, ElementType item,intTag) {    /*tag as a flag that distinguishes two stacks, takes a value of*/    if(ptrs->top2-ptrs->top1==1) {printf ("Full Stack! ");return; }    if(tag==1) {Ptrs->data[++ (PTRS-&GT;TOP1)] =item; }    Else{ptrs->data[--(PTRS-&GT;TOP2)] =item; }}elementtype Pop (structDstack *ptrs,intTag) {    if(tag==1)    {        if(ptrs->top1==-1) {printf ("Stack 1 Empty! "); returnNULL; }        Else        {            returnptrs->data[(PTRS-&GT;TOP1)--]; }    }    Else    {        if(ptrs->top2==maxsive) {printf ("Stack 2 empty! "); returnNULL; }        Else        {            returnptrs->data[(PTRS-&GT;TOP2) + +]; }    }}intMain () {//struct Dstack *s= (dstack_) malloc (sizeof (struct dstack));    structDstack Stack; Dstack_ S= &Stack; S-&GT;TOP1 =-1; S-&GT;TOP2 =maxsive;  for(inti =0; I <8; i++) {Push (S, I,1); Push (S,8I2); }     for(inti =0; I <= s->top1;i++) {printf ("%d",s->Data[i]); } printf ("\ n");  for(inti = maxsive-1; I >= s->top2; i--) {printf ("%d", s->Data[i]); } printf ("\ n"); return 0;}

Stack with min function + one array implementation two stacks + two array implementation min stack

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: 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.