Cstack. h:
# Pragma onceclass cstackelement {public: cstackelement (void) {} cstackelement (INT data, int min = 0) {m_ndata = data; m_nmin = min ;}~ Cstackelement (void) {}public: int m_ndata; int m_nmin ;}; class cstack {public: cstack (INT maxsize); // common constructor, construct a maxsize stack cstack (const cstack & stack); // copy the constructor cstack & operator = (const cstack & stack); // value assignment function ~ Cstack (void); void push (INT npushelement); // press an element nelementvoid POP () into the stack; // an element pops up from the stack, return int min (); // returns the minimum element value for the O (1) PRIVATE: cstackelement * m_pstackarr; int m_top; // point to the next position of the top element of the stack, int m_nmaxsize ;};
Cstack. cpp:
# Include "stdafx. H "# include" cstack. H "# include <iostream> using namespace STD; // common constructor that constructs a maxsize stack cstack: cstack (INT maxsize) {m_top = 0; // point to the next position of the top element of the stack m_nmaxsize = maxsize; m_pstackarr = new cstackelement [m_nmaxsize];} // copy constructor cstack: cstack (const cstack & stack) {m_top = stack. m_top; m_nmaxsize = stack. m_nmaxsize; m_pstackarr = new cstackelement [m_nmaxsize]; memcpy (m_pstackarr, stack. m_pstackarr, M_n Maxsize) ;}// value assignment function cstack & cstack: Operator = (const cstack & stack) {If (this = & stack) // check {return * This;} If (stack. m_top! = 0) // stack is empty {If (m_nmaxsize <stack. m_nmaxsize) {m_nmaxsize = stack. m_nmaxsize; Delete [] m_pstackarr; m_pstackarr = new cstackelement [m_nmaxsize]; memcpy (m_pstackarr, stack. m_pstackarr, m_nmaxsize) ;}} return * This ;}// medium-voltage an element to the stack nelementvoid cstack: Push (INT npushelement) {If (m_top = m_nmaxsize) {cout <"Full stack! "<Endl;} else if (m_top = 0) // stack null {m_pstackarr [m_top ++]. m_ndata = npushelement; m_pstackarr [m_top ++]. m_nmin = npushelement; cout <"Press" <npushelement <Endl;} else {If (m_pstackarr [m_top-1]. m_nmin> npushelement) {m_pstackarr [m_top]. m_nmin = npushelement;} else {m_pstackarr [m_top]. m_nmin = m_pstackarr [m_top-1]. m_nmin;} m_pstackarr [m_top ++]. m_ndata = npushelement; cout <"press in" <npushe LEMENT <Endl ;}/// an element pops up from the stack and returns void cstack: Pop () {int npopelement = 0; If (m_top = 0) {npopelement =-1; cout <"Stack empty! "<Endl;} else {npopelement = m_pstackarr [-- m_top]. m_ndata; cout <"pop-up" <npopelement <Endl ;}// O (1) returns the minimum element value int cstack: min () {If (m_top = 0) {cout <"Stack empty! "<Endl; Return-1;} else {return m_pstackarr [m_top-1]. m_nmin;} cstack ::~ Cstack (void ){}
Test code:
// Flexible application of stack and queue 1: Stack containing min and Max functions. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include" cstack. H "using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {cstack stack (20); stack. push (4); cout <"Min:" <stack. min () <Endl; stack. push (5); cout <"Min:" <stack. min () <Endl; stack. push (2); cout <"Min:" <stack. min () <Endl; stack. pop (); cout <"Min:" <stack. min () <Endl; stack. push (3); cout <"Min:" <stack. min () <Endl; System ("pause"); Return 0 ;}