[C ++] Data Structure: A linked list-type stack consumer Stack

Source: Internet
Author: User
// Stack in the form of custom linked list # include <iostream> using namespace STD; Template <class T> class node {public: t data; node <t> * link ;}; template <class T> class Consumer stack {public: Consumer stack () {Top = 0 ;}~ Consumer stack (); bool isempty () const {return Top = 0;} bool isfull () const; t top () const; consumer stack <t> & pushstack (const T & X); consumer stack <t> & popstack (T & X); node <t> * Top; void show () const ;}; class outofbounds {public: outofbounds () {cout <"out of bounds! "<Endl ;}; // exception class nomem {public: nomem () {cout <" no memory! "<Endl ;}}; // cause nomem exception caused by new instead of xalloc. // if you want to restore the original behavior, you can call the following: // _ set_new_handler (old_handler ); int my_new_handler (size_t size) {Throw nomem ();} template <class T> void consumer stack <t >:: show () const {node <t> * Current = top; while (current) {cout <Current-> data <Endl; current = Current-> link ;}} // The stack destructor template <class T> producer stack <t> ::~ Consumer stack () {node <t> * Next; while (top) {next = Top-> link; Delete top; Top = next ;}} // determine whether the current stack has a fully loaded template <class T> bool stack <t>: isfull () const {try {node <t> * P = new node <t>; delete P; return false;} catch (nomem) {return true ;}// return the Header element data template of the current stack <class T> T linkedstack <t> :: top () const {If (isempty () Throw outofbounds (); Return top-> data ;} // press the data into the stack template <class T> consumer stack <t> & Consumer stack <t>: pushstack (const T & X) {node <t> * P = new node <t>; P-> DATA = x; P-> link = top; Top = P; return * This ;} // Delete the top element of the stack and send it to the xtemplate <class T> consumer stack <t> & Consumer stack <t>: popstack (T & X) {If (isempty ()) throw outofbounds (); X = Top-> data; node <t> * P = top; Top = Top-> link; Delete P; return * This;} int main () {consumer stack <int> mystack; mystack. pushstack (1); mystack. pushstack (2); mystack. pushstack (3); mystack. pushstack (4); mystack. pushstack (5); mystack. pushstack (6); mystack. show (); int popnode; mystack. popstack (popnode); cout <"pop one from Stack:" <popnode <Endl; mystack. show (); Return 0 ;}

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.