Static array implementation Loop Queue C language

Source: Internet
Author: User

#include <stdio.h> #include <malloc.h> #define DATA_TYPE int#define Queue_len 5//judgment team is full of two ways, one is to be marked, For example, size. The other is to waste a piece of space, when the N-1, even if full. typedef struct QUEUE{DATA_TYPE Data[queue_len];int front;//The first element of the team head element int rear;//the tail element//int size; record Queue Size}queue,* qqueue ; void Create (Qqueue); BOOL Isfull (qqueue); bool IsEmpty (QQUEUE); bool Add (Qqueue,data_type);D ata_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 (&queue,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) {//This compares around, why should the output have to take the remainder? First of all, front represents the first element of the team, it must not be output. If you give him +1, you can output the team head normally. But the end of the team will be a problem, if the subscript is the tail subscript, +1 will exceed the array length//So the remainder, then output normal printf ("%d\n", qqueue->data[(i+1)%queue_len]); i++;i=i%queue_len;/ /in exchange for a good understanding//First find the next legal element, find the re-output//So the direct output I can, because I is already looking for the next element. i++;//i=i%queue_len;//printf ("%d\n", 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;}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Static array implementation Loop Queue C language

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.