Use static arrays to implement the C language of cyclic queue

Source: Internet
Author: User

Use static arrays to implement the C language of cyclic queue

# Include
 
  
# Include
  
   
# Define Data_Type int # define Queue_Len 5 // you can use either of the following two methods to determine whether the team is full: Mark the team, for example, size. // Another is to waste a piece of space, when taking up the N-1, even if full. Typedef struct Queue {Data_Type data [Queue_Len]; int front; // the first element of the team header, int rear; // The team end element // int size; record QUEUE size} QUEUE, * QQUEUE; void create (QQUEUE); bool isFull (QQUEUE); bool isEmpty (QQUEUE); bool add (QQUEUE, Data_Type ); data_Type out (QQUEUE); void traverse (QQUEUE); int main (void) {QUEUE queue; create (& queue); add (& queue, 1); add (& queue, 2); add (& queue, 3); add (& queue, 4); out (& queue); add (& queue, 5); out (& queue ); add (& qu Eue, 6); out (& queue); add (& queue, 7); traverse (& queue);} bool isFull (QQUEUE qQuere) {if (qQuere-> rear + 1) % Queue_Len = qQuere-> front) {return true;} else {return false;} bool isEmpty (QQUEUE qQueue) {if (qQueue-> front = qQueue-> rear) {return true;} else {return false ;}} void create (QQUEUE qQueue) {qQueue-> front = qQueue-> rear = 0; return;} void traverse (QQUEUE qQueue) {int I = qQueue-> front; while (I! = QQueue-> rear) {// here is a comparison. Why do I need to obtain the remainder in the output? // First, front represents the first element of the Team, which must not be output. // If you give him more than 1, you can normally output the first line. // But there will be a problem at the end of the team. If the subscript is the subscript at the end of the team, + 1 will exceed the length of the array. // if the remainder is obtained, the normal printf (% d, qQueue-> data [(I + 1) % Queue_Len]); I ++; I = I % Queue_Len; // find the next legal element first for a better understanding, find and output //, so you can directly output I, because I is already the next element to be found. // I ++; // I = I % Queue_Len; // printf (% d, qQueue-> data [I]) ;}} bool add (QQUEUE qQueue, data_Type val) {if (isFull (qQueue) {return false;} else {qQueue-> rear = (qQueue-> rear + 1) % Queue_Len; qQueue-> data [qQueue-> rear] = val; return true ;}} Data_Type out (QQUEUE qQueue) {if (isEmpty (qQueue) {exit (-1 );} else {Data_Type val = qQueue-> data [qQueue-> front + 1]; qQueue-> front = (qQueue-> front + 1) % Queue_Len; return val ;}}
  
 

 

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.