[Learn More-Data Structure-stack & queue] design the stack of a min Function

Source: Internet
Author: User

Description:

Define the data structure of the stack. Add a min function to obtain the minimum element of the stack.

The time complexity of the min, push, and pop functions is O (1 ).

Solution 1: Design a minimum stack as an auxiliary structure to record the minimum elements in the stack. The elements to be added to the stack are saved in the element stack. The minimum stack stores the current minimum element. So:

1. when the stack is imported, the element stack is first entered, and compared with the element at the top of the minimum stack. If it is smaller than the element at the top of the stack, the minimum element is added to the minimum stack. Otherwise, the element at the top of the stack is added to the minimum stack.

2. When the stack is output, stack 1 and stack 2 go out simultaneously.

The corresponding algorithm is as follows.

Note: This algorithm does not further encapsulate minstack. Therefore, two stacks are used for operations. To meet the requirements of the subject, further encapsulation should be made on the outer layer. Use minstack and element stack as the final stack elements. Similar

Struct {

Selemtype * top;

Selemtype * base;
Elemstack S;
Minstack min;

Int stacksize;
}
This structure.

The algorithm has a clear idea. A secondary stack is required.

 

# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <assert. h> // initial maximum stack capacity # define stack_max_size 100 // stack Capacity Increment # define increament 10 // status function # define error 0 # define OK 1 # define soverflow-1 # define Yes 1 # define no 0 typedef int status, selemtype, elemtype; typedef struct {selemtype * base; selemtype * Top; int stacksize; // currently allocated space, not the number of elements in the stack.} Sqstack; Status initstack (sqstack & S) {// initialize the stack space. The stack is empty. base = (selemtype *) malloc (stack_max_size * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base; S. stacksize = stack_max_size; Return OK;} // If the stack is not empty, use e to output the top element of the stack and Return OK. Otherwise, an error is returned. Status gettop (sqstack S, selemtype & E) {If (S. top = S. base) {return error;} e = * (S. top-1); Return OK;}/** push (S, e) medium pressure element e to stack S. Consider the following situations: * 1. the stack is full and space needs to be increased. If you cannot add more space, exit. * 2. the stack is not full. Press the element first, and then modify the stack top pointer. **/Status push (sqstack & S, selemtype e) {If (S. top-s. base)> = S. stacksize) {S. base = (elemtype *) realloc (S. base, (S. stacksize + increament) * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base + S. stacksize; S. stacksize + = increament;} // press the element first, and then modify the pointer. * S. Top = E; S. Top ++; Return OK;}/** pops up the top element of the stack. Note: * 1. the stack is not empty. Modify the pointer, then pop up the element and send it to e. * 2. If the stack is empty, an error is returned. */Status POP (sqstack & S, elemtype & E) {If (S. Top = S. Base) {Return Error ;}// first modify the pointer and then the element pops up. S. top --; E = * s. top; Return OK;} status isempty (sqstack s) {If (S. top = S. base) {return yes;} return no;} // The following functions encapsulate the inbound and outbound stacks. Further stack encapsulation is required in practical applications. There is no encapsulation here. // Struct {// elemstack s; // minstack min; //} void pushin (sqstack & S, sqstack & min, elemtype e) {elemtype TMP; push (S, e); If (isempty (min) {push (Min, e) ;}else {gettop (Min, TMP); If (TMP> = e) {push (Min, e) ;}else {push (Min, TMP) ;}} status popout (sqstack & S, sqstack & min, elemtype & E) {If (! Isempty (s )&&! Isempty (min) {Pop (Min, e); POP (S, e); Return OK;} else {Return Error ;}} status getmin (sqstack S, sqstack min, elemtype & result) {If (! Isempty (s )&&! Isempty (min) {gettop (Min, result); Return OK;} else {Return Error ;}} main () {sqstack S, min; initstack (s ); initstack (min); int rands, rest; printf ("inbound Stack:"); For (INT I = 0; I <100; I ++) {rands = rand () % 1000; pushin (S, Min, rands); getmin (S, Min, rest); printf ("% d -- min: % d \ n", rands, rest);} printf ("\ n outbound Stack:"); While (! Isempty (s) {popout (S, Min, rest); printf ("% d", rest) ;}system ("pause"); Return 0 ;}

Solution 2: if another secondary stack is not designed, you can modify the element type of the stack and add a min field to record the minimum element of the element that has already been written into the stack.

Typedef int selemtype;
Typedef int status;

Typedef struct {
Minelement * base;
Minelement * top;
Int stacksize; // The currently allocated space, not the number of elements in the stack.
} Minstack;

Typedef struct {
Selemtype data;
Selemtype min;
} Minelement;

The operation is similar to the general stack operation. The difference is that two stacks need to be pushed at the same time during the push operation, and the current minimum element in the minstack is always at the top of the stack.

The code for the next thought is as follows:

// Code function, including the implementation of the min function stack. # Include <stdio. h> # include <stdlib. h> # include <malloc. h> // initial maximum stack capacity # define stack_max_size 100 // stack Capacity Increment # define increament 10 // status function # define error 0 # define OK 1 # define soverflow-1 # define Yes 1 # define no 0 typedef int status, elemtype; typedef struct {elemtype data; elemtype min;} minelement; typedef struct {minelement * base; minelement * Top; int stacksize; // currently allocated space, not the number of elements in the stack.} Minstack; // typedef minelement selemtype; Status initstack (minstack & S) {// initialize the stack space. The stack is empty. base = (minelement *) malloc (stack_max_size * sizeof (minelement); If (! S. base) {exit (soverflow);} s. top = S. base; S. stacksize = stack_max_size; Return OK;} status gettop (minstack S, minelement & E) {If (S. top = S. base) {return error;} e = * (S. top-1); Return OK;} status isempty (minstack s) {If (S. top = S. base) {return yes;} return no;} status push (minstack & S, elemtype e) {If (S. top-s. base)> = S. stacksize) {S. base = (minelement *) realloc (S. base, (S. stacksize + incret Ament) * sizeof (minelement); If (! S. base) {exit (soverflow);} s. top = S. base + S. stacksize; S. stacksize + = increament;} // press the element first, and then modify the pointer. On the contrary, the top pointer is always at the next position of the top element. The operation is the opposite of that in pop. // Todo minelement TMP, node; node. data = E; If (isempty (s) {node. min = E;} else {gettop (S, TMP); node. min = TMP. min> E? E: TMP. min;} * (S. top) = node; S. top ++; Return OK;} status POP (minstack & S, minelement & E) {If (S. top = S. base) {return error;} // first modify the pointer and then the element pops up. The top pointer is always at the next position of the top element S. top --; E = * s. top; Return OK;} status getmin (minstack S, elemtype & E) {minelement TMP; If (! Isempty (s) {gettop (S, TMP); E = TMP. min; Return OK;} else {return error;} Main () {minstack s; initstack (s); int rands, min; printf ("inbound Stack :"); for (INT I = 0; I <100; I ++) {rands = rand () % 1000; push (S, rands); getmin (S, min ); printf ("% d -- min: % d \ n", rands, min);} printf ("\ n outbound Stack:"); minelement mine; while (! Isempty (s) {Pop (S, mine); printf ("% d --- % d \ n", mine. data, mine. min);} system ("pause"); Return 0 ;}

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.