C Language Implementation Loop queue (basic operation and illustration)

Source: Internet
Author: User

Tag: Exit queue IMA Scan causes logs status include Ror

————————————————————————————————————————————

If you use a sequential table as a queue, you cannot continue inserting new tail elements when you are in the right state, otherwise the program code will be corrupted because the array is out of bounds.

This results in a circular queue implemented by the list, which can be inserted into a new tail element only if the queue is not full.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Basic operation:

/* To define a linked list queue */

Defines the structure in which front indicates the team header position, rear indicates the end of the team, and the base pointer is used to request space and hold data.

/* Initialize Queue */

Use pointer *base to request 100 memory spaces, front and rear 0 respectively, when the queue is empty

/* judging empty or full */

  • when initializing, Front = Rear = 0 Is empty, q->rear = ( 0+1 ) %100 = 1 , the queue is not full to insert the queue

  • Queue 3 an element, rear = 3 , q->rear = ( 3+1 ) %100 = 4 , the queue is not full

  • Queue About an element, rear = , q->rear = ( 99+1 ) %100 = 0 , queue full, not queued

  • out Team 2 an element, Front = 2

    after the team, execute two times Q->front = (q->front + 1)% maxqsize , Get Q->front = 2

  • Team up again . 1 an element, Rear = 0 , q->rear = ( 99+1 ) %100=0 , queue is not full, can be queued

Implementation code:

1#include <stdio.h>2#include <stdlib.h>3 #defineOK 14 #defineERROR 05 #defineOVERFLOW-26 #defineMaxqsize 1007typedefintStatus;8typedefintQelemtype;9typedefstructNodeTen { OneQelemtype *Base;//initialize dynamic allocation of storage space A     intFront; -     intRear; - } sqqueue; theStatus Initqueue (Sqqueue *Q) - { -Q->Base= (Qelemtype *) malloc (maxqsize *sizeof(Qelemtype)); -     if(! Q->Base) + exit (OVERFLOW); -Q->front = Q->rear =0; +     returnOK; A } atStatus EnQueue (Sqqueue *Q, Qelemtype elem) - { -     //queue is empty when 1%100==1, queue full (99+1)%100==0, up to 99 elements -     if((Q->rear +1)% Maxqsize = = (q->front)) -         returnERROR; -Q->Base[Q->rear] =Elem; inQ->rear = (Q->rear +1)% Maxqsize;//rear is always circulating in 0-100 -     returnOK; to } +Status Outqueue (Sqqueue *q, Qelemtype *e) - { the     if(Q->front = = q->rear) *         returnERROR; $*e = q->Base[q->Front];Panax NotoginsengQ->front = (Q->front +1) %maxqsize; -     returnOK; the } + Status PrintQueue (sqqueue Q) A { theprintf"The queue is:"); +      for(inti = Q.front; i < q.rear; ++i) -printf"%d"Q.Base[i]); $     returnOK; $ } - intMain () - { the sqqueue queue; - qelemtype Elem;Wuyi     inti; theInitqueue (&queue); -printf"Input:"); Wu      while(SCANF ("%d", &elem)! =EOF) -EnQueue (&queue, elem); About PrintQueue (queue); $     /*Enter the number of queues to be out*/ -printf"\noutput:"); -scanf"%d", &i); -      while(I! =0) A     { +Outqueue (&queue, &elem); thei--; -     } $ PrintQueue (queue); the     returnOK; the}

C Language Implementation Loop queue (basic operation and illustration)

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.