[C + +] Implementation of the [data structure] Stack

Source: Internet
Author: User

For the definition of the stack, the previous statement is prepared.

I implemented a stack<value> container class that supports Push,pop,top,size,empty,clear and copy construction operations.

The main implementation of the idea is to write a few support basic operations of the class _stack_impl, and then write a wrapper class stack, packaging basic operations, and then implement Size,copy struction,top,clear and throw exception function. The benefits of doing so (Pimpl) are self-evident.

My implementation of copy Structurion is actually a wrapper with the friend function _stack_copy (DST, SRC), because this allows direct access to the bottom member and replicate, rather than using the user interface, first a pop, then a push , much faster.

The code uses the C++11 standard. If there is a wrong place, please point out.

Compile and test the pass under Win7 mingw32 gcc4.7. The test content is very simple, so there may be some problems that are not tested.

Here is the implementation

1 #pragmaOnce2#include <cstddef>3#include <stdexcept>4 5 namespaceJT {6 7Template <classValue>8 classStack {9Template <classV>TenFriendvoid_stack_copy (stack<v>& DST,Conststack<v>&src); One  A Private: -   struct_stack_impl; -_stack_impl *_impl =nullptr; the size_t _siz; -  -   void_init_empty () { -_siz =0; +     if(_impl)Delete_impl; -_impl =New_stack_impl; +   } A  at   void_destory () {_siz =0;Delete_impl;} -  -  Public: - stack () {_init_empty ();} -StackConstStack<value> &o) {_stack_copy (* This, O); } -~stack () {_destory ();} in  -   voidClear () {_init_empty ();} to   voidPushConstValue &val) {_impl->push (val); + +_siz;} +size_t size ()Const{return_siz;} -  the value Pop () { *     if(_siz = =0) $       ThrowStd::out_of_range ("jt::stack::p op ()-Empty stack");Panax Notoginseng--_siz;return_impl->pop (); -   } the  +   BOOLEmpty ()Const{return_siz = =0; } A  theValue Top ()Const { +     if(_siz = =0) -       ThrowStd::out_of_range ("jt::stack::top ()-Empty stack"); $     return_impl->n->Val; $   } - }; -  theTemplate <classValue> -Staticvoid_stack_copy (stack<value> &dst,ConstStack<value> &src) {Wuyi dst._init_empty (); theAuto **DN = &dst._impl->n;//dest Node -  Wu    for(Auto *s = src._impl->n; s; s = s->next) { -*DN =NewTypeName Stack<value>:: _stack_impl::node; About(*DN)->val = s->Val; $(*DN)->next =nullptr; -DN = & (*DN)Next; -   } -  ADst._siz =Src._siz; + } the  -Template <classValue> $ structStack<value>:: _stack_impl { the   structNode { the value Val; theNode *next =nullptr; the} *n = nullptr;//Head/top -  in~_stack_impl () { the      while(n) { theAuto *tmp = n->Next; About       DeleteN; then =tmp; the     } the   } +  -   voidPushConstvalue&val) { theNode *t =Newnode;BayiT->val =Val; theT->next =N; the  -n =T; -   } the  the value Pop () { theValue v = n->Val; the  -Node *tmp = n->Next; the     DeleteN; then =tmp; the 94     returnv; the   } the }; the 98}

[C + +] Implementation of the [data structure] Stack

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.