C's cyclic queue

Source: Internet
Author: User

# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>

# Define maxquesize 3

Typedef struct prepare Param
{
Int typeid;
Unsigned char Chan;
Unsigned char type;
Unsigned char interval time;
Unsigned char comment frame;
} Else Param;

Typedef struct cirqueue
{
Using Param * using Param;
Int front;
Int rear;
} Cirqueue;

Struct cirqueue Q;

Int initqueue (cirqueue * q)
{
Q-> partition Param = (partition Param *) malloc (maxquesize * sizeof (partition PARAM ));
If (Q-> callback Param = NULL)
{
Return exit_failure;
}
Q-> front = Q-> rear = 0;
Return exit_success;

}

 

Int inqueue (cirqueue * q, partition Param * PARAM)
{
If (Q-> rear + 1) % maxquesize) = Q-> front)
{
Return exit_failure;
}
Q-> prepare Param [q-> rear] = * Param;
Q-> rear = (Q-> rear + 1) % maxquesize;
Return exit_success;
}

Int dequeue (cirqueue * q, partition Param * PARAM)
{
If (Q-> front = Q-> rear)
{
Return exit_failure;
}
* Param = Q-> prepare Param [q-> front];
Q-> front = (Q-> front + 1) % maxquesize;
Return exit_success;
}

Int main (INT argc, char ** argv)
{
Int I = 0;
Callback Param * Param = (callback Param *) malloc (sizeof (callback PARAM ));
Using Param PM;
Param-> typeid = 21;
Param-> Chan = 2;
Param-> type = 1;
Param-> runtime time = 3;
Param-> writable frame = 4;
I = initqueue (& Q );
If (I = 1)
{
Perror ("init queue failure :");
}
If (inqueue (& Q, Param)> 0)
{
Puts ("queue is full! ");
}
If (dequeue (& Q, & PM)> 0)
{
Puts ("queue is empty! ");
}
Printf ("% S % d/N", _ file __, _ line __, PM. typeid, PM. chan, PM. type, pm.jpeg time, pm.jpeg frame );
Return 0;
}

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.