Simple Sequential stack (C ++ template technology implementation)

Source: Internet
Author: User

The following Code is only used by me to review the data structure. Its practicality is low ~~ Haha:>

/// C ++ template technology to implement a Simple Sequential stack. // # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // sequence stack template. // template <typename T> class Stack {PRIVATE: int _ top; size_t _ size; T * _ pstack; static const size_t _ def_size = 20; /// _ min_top should not be of the size_t type. // because the int type is converted to the size_t type when the size_t type is compared with the int type. // static const int _ mix_top = 0; public: Stack (const size_t size = _ def_size): _ top (_ mix_top-1), _ SIZE (size) {_ pstack = new T [_ SIZE] ;}~ Stack (void) {Delete [] _ pstack;} bool isempty (void) const {return _ mix_top> _ top;} bool isfull (void) const {return _ size-1 = _ top;} t gettop (void) const {If (isempty () {Throw STD: underflow_error ("Stack is empty and off! ");} Return _ pstack [_ top];} void push (const T & Val) {If (isfull () {Throw STD: overflow_error (" the stack is full! ");} _ Pstack [++ _ top] = val;} t POP (void) {If (isempty () {Throw STD: underflow_error (" Stack is empty and off! ") ;}Return _ pstack [_ top --] ;}}; // test stack. // int main (void) {const size_t max_size = 20; stack <int> stack (max_size); STD: cout <"stack" <(stack. isempty ()? "": "No") <"blank." <STD: Endl; STD: cout <"stack" <(stack. isfull ()? "": "No") <"is full. "<STD: Endl; For (size_t I = 0; I <max_size; ++ I) {stack. push (I); STD: cout <STD: SETW (3) <stack. gettop ();} // stack. push (max_size); STD: cout <STD: Endl; STD: cout <"stack" <(stack. isempty ()? "": "No") <"blank." <STD: Endl; STD: cout <"stack" <(stack. isfull ()? "": "No") <"is full. "<STD: Endl; For (size_t I = 0; I <max_size; ++ I) {STD: cout <STD: SETW (3) <stack. pop ();} // STD: cout <STD: SETW (3) <stack. pop (); Return exit_success ;}

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.