C Language Enhancement (2) design can find the minimum element stack, minimum

Source: Internet
Author: User
Tags pop value stack pop

C Language Enhancement (2) design can find the minimum element stack, minimum

The previous article explains how to convert a binary tree to a two-way linked list. This article serves as the second article in the [C Language Enhancement] series to talk about a stack question,

You can master this question.

  • How to use the "advanced and later" Features of stacks
  • How to skillfully leverage the help Stack
  • How to define shared static member variables in 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 <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** question: to define the data structure of the stack, add a min function, to find out the minimum requirements of the stack, the time complexity of min, push, and pop functions is O (1). The idea is to firmly grasp the [advanced and later] features of the stack and simulate various practical situations, it is concluded that an auxiliary stack is added to the algorithm stack structure, which 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 Stack also pops up an element. At this time, the top of the minimum stack is the next minimum element. * // * Create the value of the stack node struct nextNode next node push () into the stack function pop () out of the stack function min () Evaluate the minimum function push2 () calculate the minimum value of pop2 () and the minimum value of */struct StackNode {int value; static StackNode * minNode; StackNode * nextNode;/** push () the value of the function value returned to the stack is */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 <endl; return top ;} /** pop () The function returned by the stack function */StackNode * pop () {if (NULL! = This) {cout <"output stack, value =" <this-> value <endl; return this-> nextNode ;} else {cout <"Stack is empty" <endl; return this ;}} /** evaluate the minimum value function flag to determine whether it is the value of push or popvaluepush or the value of pop exit */void min (bool flag, int value) {// pusyy (flag) {// non-empty if (NULL! = MinNode) {// if (value <= minNode-> value) {cout <"minimum value stack-in-stack" <endl; minNode = minNode-> push (value) ;}} else {// null, then directly put it in cout <"MinNode = NULL" <endl; cout <"minimum value stack into stack" <endl; minNode = minNode-> 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-out stack" <endl; minNode = minNode-> pop ();}} if (NULL! = MinNode) cout <"minimum value of the current stack =" <minNode-> value <endl;} // push + minStackNode * push2 (int value) {StackNode * node = this-> 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.

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.