Use arrays to implement dual-end queues (C ++)

Source: Internet
Author: User
/** Use an array to implement a dual-end queue that can be inserted or deleted at both ends */static const int nlength = 5; static int deque [nlength]; static int nidxhead = 0; static int nidxtail = 0; Enum err {noerr, erroverflow, errunderflow,}; // The err endequehead (int x) {If (nidxhead + nlength-1) % nlength) = nidxtail) return erroverflow; nidxhead = (nidxhead + nlength-1) % nlength); deque [nidxhead] = x; return noerr ;} // out-of-the-head err dedequehead (int * px) {If (nidxhea D = nidxtail) return errunderflow; If (null! = Px) * PX = deque [nidxhead]; nidxhead = (nidxhead + 1) % nlength; return noerr;} // err endequetail (int x) {If (nidxtail + 1) % nlength) = nidxhead) return erroverflow; deque [nidxtail] = x; nidxtail = (nidxtail + 1) % nlength ); return noerr;} // err dedequetail (int * px) {If (nidxhead = nidxtail) return errunderflow; nidxtail = (nidxtail + nlength-1) % nlength; if (null! = Px) * PX = deque [nidxtail]; return noerr ;}

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.