2. Design a stack (stack) containing min Functions)
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 ).
My ideas:
Use an additional element to record the minimum value. If the value is smaller during push, it is updated. But I encountered a problem during pop. How can I get the next smallest value when the smallest pop-up occurs? I always felt that I had to sort the logs and store another min chain. The result does not reach O (1 ).
/* 2. design the stack (stack) that contains the min function to 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 ). */# Include <stdio. h> # include <stdlib. h> typedef int elemtype; typedef struct stack_elem {elemtype data; stack_elem * p_next;}; typedef struct my_stack {elemtype min; stack_elem * Top;} my_stack; elemtype stack_min (my_stack S) {return S. min;} void stack_push (my_stack * s, elemtype e) {stack_elem * new_element = (stack_elem *) malloc (sizeof (stack_elem); new_element-> DATA = E; new_element-> p_next = s-> top; s -> Top = new_element; s-> min = (S-> min <e )? S-> min: E;} elemtype stack_pop (my_stack * s) {If (S = NULL) {printf ("error"); Return-1 ;} stack_elem * pop_elem = s-> top; s-> Top = pop_elem-> p_next; pop_elem-> p_next = NULL; elemtype E = pop_elem-> data; if (E = s-> min) {elemtype min = s-> top-> data; stack_elem * P = s-> top; while (p-> p_next! = NULL) {If (p-> data <min) {min = p-> data;} p = p-> p_next;} s-> min = min ;} free (pop_elem); Return e;} int main () {my_stack s; S. min = 999999; S. top = NULL; stack_push (& S, 5); stack_push (& S, 4); stack_push (& S, 3); stack_push (& S, 2 ); stack_push (& S, 1); elemtype E = stack_min (s); E = stack_pop (& S); E = stack_min (s ); return 0 ;}
When I found the answer online, I suddenly realized that it would be better to store a stack with a minimum value. Every time I encounter a new minimum value, I will go to the stack. I will ignore it if it is not the new minimum value. This is okay, as shown in figure
Stack elements: 3 4 2 5 6 1
Elements in the min Stack: 3 2 1
After 1 is displayed
Stack elements: 3 4 2 5 6
Elements in the min Stack: 3 2
The minimum value is not affected when the value is 5 or 6.
Code: http://www.cnblogs.com/likwo/archive/2010/12/21/1912331.html
// Minstatck. cpp: defines the entry point of the console application. # Include "stdafx. H "# include <iostream> # include <vector> # include <cassert> using namespace STD; Template <typename T> class stacksuppliedmin {public: vector <t> datas; vector <size_t> minstack; void push (t data) {datas. push_back (data); If (minstack. empty () | data <datas [minstack. back ()]) minstack. push_back (datas. size ()-1);} void POP () {assert (! Datas. empty (); If (datas. back () = datas [minstack. back ()]) minstack. pop_back (); datas. pop_back ();} T min () {assert (! Datas. Empty ()&&! Minstack. empty (); Return datas [minstack. back ()];} void display () {cout <"datas ="; for (unsigned I = 0; I <datas. size (); I ++) cout <datas [I] <""; cout <Endl; cout <"minstack ="; for (unsigned I = 0; I <minstack. size (); I ++) cout <datas [minstack [I] <""; cout <Endl; cout <"min =" <datas [minstack. back ()] <Endl ;}}; void main () {stacksuppliedmin <int> S; S. push (3); S. display (); S. push (4); S. display (); S. push (2); S. display (); S. push (0); S. display (); S. pop (); S. display (); S. pop (); S. display (); S. push (0); S. display ();}
[Programming question] design a stack containing min Functions