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

Source: Internet
Author: User
Tags wrapper

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

Queues are implemented in very similar ways to stacks. I added something directly to the code on the stack I implemented, and I implemented this queue by replacing some identifiers globally.

I implemented a queue<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 _queue_impl, and then write a wrapper class queue, packaging basic operations, and then implement Size,copy Struction,top,clear and throw the exception function. The benefits of doing so (Pimpl) are self-evident.

My implementation of copy Structurion is actually a wrapper with the friend function _queue_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 classQueue {9Template <classV>TenFriendvoid_queue_copy (queue<v>& DST,Constqueue<v>&src); One  A Private: -   struct_queue_impl; -_queue_impl *_impl =nullptr; the size_t _siz; -  -   void_init_empty () { -_siz =0; +     if(_impl)Delete_impl; -_impl =New_queue_impl; +   } A  at   void_destory () {_siz =0;Delete_impl;} -  -  Public: - Queue () {_init_empty ();} -QueueConstQueue<value> &o) {_queue_copy (* This, O); } -~Queue () {_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::queue::p op ()-Empty queue");Panax Notoginseng--_siz;return_impl->pop (); -   } the  +   BOOLEmpty ()Const{return_siz = =0; } A  theValue Front ()Const { +     if(_siz = =0) -       ThrowStd::out_of_range ("Jt::queue::front ()-Empty queue"); $     return_impl->f->Val; $   } -  -Value back ()Const { the     if(_siz = =0) -       ThrowStd::out_of_range ("jt::queue::back ()-Empty queue");Wuyi     return_impl->b->Val; the   } - }; Wu  -Template <classValue> About Static void_queue_copy (queue<value> &dst,ConstQueue<value> &src) { $ dst._init_empty (); -Auto **DN = &dst._impl->f;//dest Node -  -    for(Auto *s = src._impl->f; s; s = s->next) { A*DN =NewTypeName Queue<value>:: _queue_impl::node; +(*DN)->val = s->Val; thedst._impl->b = *DN; -DN = & (*DN)Next; $   } the  theDst._siz =Src._siz; the } the  -Template <classValue> in structQueue<value>:: _queue_impl { the   structNode { the value Val; AboutNode *next =nullptr; the} *f = nullptr,//Front the(By// Back the  +~_queue_impl () { -      while(f) { theAuto *tmp = f->Next;Bayi       DeleteF; thef =tmp; the     } -   } -  the   voidPushConstvalue&val) { theNode *t =Newnode; theT->val =Val; the  -     if(f) B->next =T; the     Elsef = t;//if empty, set front the  theb =T;94   } the  the value Pop () { theValue v = f->Val;98  AboutNode *tmp = f->Next; -     DeleteF;101f =tmp;102 103     returnv;104   } the };106 107}

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

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.