C + + implementation of the queue (array)--Create-enter team-out team-back to team first element-empty queue stack-processing queue

Source: Internet
Author: User

The array implementation of the queue, from the end of the team into, the enemy deleted.

Queue Length with the flag variable size, which is a variable independent of front and rear. Size = = 0, the queue is empty. Size = = capacity, full queue.

I. Statement of statements

 1  struct   node{ 2   int   capacity;  3   Front;  4   Rear;  5   Size;  6   Array;  7  };  8  typedef struct  Node Queue; 

capacity queue capacity; Front,rear is the array subscript for the first and the trailing elements of the queue; the size is the current queue, and the array points to the pointer to the shaped array, which holds the queue elements.

Second, non-null judgment

1 int queue::isempty (queue *Q)2{3     return0;    // a sign independent of the existence of Q->rear and Q->front 4 }

Three, full queue judgment

1 int queue::isfull (queue *Q)2{3     return (q->size = = Q >capacity); 4 }

Iv. Creating queues

1Queue::queue *queue::createqueue (intmaxelements)2 {3cout <<"Please input the value of maxelements:"<<Endl;4scanf_s ("%d", &maxelements);5     if(Maxelements <minqueuesize)6     {7cout <<"The size of the queue is too small!"<<Endl;8         return 0;9     }Ten     Else One     { AQueue *Q; -Q = (Queue *)New(Queue); -         if(Q = =NULL) thecout <<"Out of space!"<<Endl; -Q->array =New int[maxelements]; -         if(Q->array = =NULL) -cout <<"Out of space!"<<Endl; +  -Q->capacity =maxelements; +Q->front =1; AQ->rear =1;//rear pre-initialized to 1 atQ->size =0; Empty Queue flag - Makeempty (Q); -         returnQ; -     } -}

Five, empty the queue

1 voidQueue::makeempty (Queue *Q)2 {3     if(IsEmpty (Q))4cout <<"Donnot need to makeempty the queue!"<<Endl;5Q->size =0; Empty queue flag, the initial state is labeled as follows6Q->front =1; 7Q->rear =0; 8}

VI. Cyclic Queue implementation

1 int queue::iscycle (int value, queue *Q)2{3     if (+ +value = = q->capacity)     //subscript starting from 0, so the subscript is capacity, indicating the first element of the loop queue, that is, the subscript is 04         return  0; 5     return        value;     Subscript less than capacity, normal self-increment 6 }

Seven, into the queue

1Queue::queue *queue::enqueue (Queue *Q)2 {3     if(Isfull (Q))4cout <<"Full queue!"<<Endl;5     Else6     {7         intx =0;8cout <<"Please input the number to enqueue!"<<Endl;9scanf_s ("%d", &x); Take address characterTenq->size++; OneQ->rear = Iscycle (q->rear,q);//loop Queue self-increment AQ->array[q->rear] =x; -     } -     returnQ; The full queue returns to the original queue, and the queue is returned when it is not full the}

Viii. return to the first element of the team

1 int queue::front (queue *Q)2{3     return q->array[q-> Front];            // returns only the first element of the team, not the queue 4 }

Nine, out of the queue

1Queue::queue *queue::d equeue (Queue *Q)2 {3     if(IsEmpty (Q))4cout <<"Empty queue!"<<Endl;5     Else6     {7cout <<"The front element of the queue is:"<< Q->array[q->front] <<Endl;8q->front++;9q->size--; Ten     } One     returnQ; A}

Ten, processing queue

1 voidQueue::d isposequeue (Queue *Q)2 {3      while(!IsEmpty (Q))4     {5Q->size =0;//cyclic termination conditions6          Free(q->Array);7          Free(Q);8     }9}

C + + implementation of the queue (array)--Create-enter team-out team-return to team first element-empty queue stack-processing 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.