C + + simple implementation Loop queue

Source: Internet
Author: User
Tags array length

The data structure of the stack is advanced, and the data structure of the queue is an exit entry that can only be queued and exported to the team.

The implementation code is as follows:

1 /*2 cyclic static queue implementation August 5, 2017 07:50:583 */4 #ifndef __queue_h__5 #define__queue_h__6#include <Windows.h>7 classQueue8 {9  Public:TenQueue ();//non-parametric construction OneQueue (DWORD);//with a reference structure A~queue ();//Destruction -BOOL push (UINT);//Queue -BOOL pop (uint&);//out Team the     voidTraverse ();//Traverse -BOOL IsEmpty ();//is null -BOOL Isfull ();//is full - Private: +DWORD m_queuesize;//array length of the queue -DWORD M_front;//The first element index of the queue +DWORD m_rear;//queue Next available element index APdword m_ptr;//Pointer to element at }; - Queue::queue () -: M_queuesize (6), - M_front (NULL), - m_rear (NULL) - { inM_ptr =NewDword[m_queuesize]; -     if(!M_PTR)return; toZeroMemory (M_ptr,sizeof(DWORD) *m_queuesize); + } - queue::queue (DWORD dwsize) the : M_queuesize (dwsize), * M_front (NULL), $ m_rear (NULL)Panax Notoginseng { -M_ptr =NewDword[m_queuesize]; the     if(!M_PTR)return; +ZeroMemory (M_ptr,sizeof(DWORD) *m_queuesize); A } thequeue::~Queue () + { -     if(m_ptr) $         Delete[] m_ptr; $ } - BOOL Queue::p ush (UINT Element) - { the     if(Isfull ()) -     {Wuyi         //queue is full queued failed the         returnFALSE; -}Else Wu     { -M_ptr[m_rear] =Element; AboutM_rear = (m_rear+1) %m_queuesize; $         returnTRUE; -     } - } -BOOL Queue::p op (uint&Element) A { +     if(IsEmpty ()) the     { -         //queue is null out of team failure $         returnFALSE; the}Else the     { theElement = M_ptr[m_front];//get the data out of the team first. theM_front = (m_front+1) %m_queuesize; -     } in     returnTRUE; the } the BOOL queue::isempty () About { the     if(M_front = =m_rear) the         returnTRUE; the     Else +         returnFALSE; - } the BOOL queue::isfull ()Bayi { the     if((m_rear+1)% M_queuesize = =M_front) the         returnTRUE; -     Else -         returnFALSE; the } the voidQueue::traverse () the { theDWORD Dwtempindex =M_front; -      while(m_rear!=Dwtempindex) the     { theprintf"%d\t", M_ptr[dwtempindex]); theDwtempindex = (Dwtempindex +1) %m_queuesize;94     } theprintf"\ r \ n"); the } the #endif    //__queue_h__

Demo Code

1#include <stdio.h>2#include <stdlib.h>3#include"Queue.h"4 intMainvoid)5 {6UINT M_elem =NULL;7Queue * Tempptr =NewQueue (Ten);8     if(tempptr->isEmpty ())9     {Tenprintf"the queue is null \ r \ n"); One     } ATempptr->push (1); -Tempptr->push (2); -Tempptr->push (3); theTempptr->push (4); -Tempptr->push (5); -     if(tempptr->isfull ()) -     { +printf"queue is full \ r \ n"); -}Else +     { ATempptr->push (6); at     } -Tempptr->traverse (); -Tempptr->pop (m_elem); -printf"m_elem:%d\r\n", M_elem); -Tempptr->traverse (); -     Deletetempptr; inSystem"Pause"); -     return 0; to}

C + + simple implementation Loop queue

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.