C + + Learning Note 50: Queue class templates

Source: Internet
Author: User
Tags assert

A queue is a linear population that can only add elements to one side and delete elements from the other end

Loop queue
    • In the imagined array is bent into a ring, when the element is out of the queue, the successor element does not move, and each time the end of the queue reaches the last element of the array, it goes back to the beginning of the array.

Queue class Template

//Queue.h#ifndef Queue_h#defineQueue_h#include<cassert>//definition of a class templateTemplate <classTintSIZE = ->classqueue{Private:    intfront, rear, count; T List[size]; Public: Queue (); voidInsertConstT &item);    T Remove (); voidClear (); ConstT &getfront ()Const; intGetLength ()Const; BOOLIsEmpty ()Const; BOOLIsfull ()Const;}; Template<classTintSize> queue<t, Size>::queue (): Front (0), Rear (0), COUNT (0) {}template<classTintSize>voidQueue<t, Size>::insert (Constt&Item) {Assert (Count!=SIZE); Count++; List[rear]=item; Rear= (Rear +1) %SIZE;} Template<classTintSize> T queue<t, size>:: Remove () {Assert (Count!=0); inttemp =Front; Count--; Front= (front +1) %SIZE; returnlist[temp];} Template<classTintSize>ConstT &queue<t, Size>::getfront ()Const{    returnList[front];} Template<classTintSize>intQueue<t, Size>::getlength ()Const{    returncount;} Template<classTintSize>BOOLQueue<t, Size>::isempty ()Const{    returnCount = =0;} Template<classTintSize>BOOLQueue<t, Size>::isfull ()Const{    returnCount = =SIZE;} Template<classTintSize>BOOLQueue<t, size>:: Clear () {count=0; Front=0; Rear=0;}#endif // 

C + + Learning Note 50: Queue class templates

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.