Interview Question 19: stacks containing min Functions

Source: Internet
Author: User

 

# 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 ;};
# 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 ){}
// 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 ;}

 

Related Article

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.