Sequential structured cyclic queue

Source: Internet
Author: User
// Cyclic queue # include <stdio. h> # include <stdlib. h> # define maxsize 5 # define status intstruct queue {int * base; int front; int real;} Q; Status initqueue (queue & Q) {q. base = (int *) malloc (maxsize * sizeof (INT); If (! Q. Base) {printf ("memory allocation failed, Program terminated! \ N "); exit (-1);} Q. front = Q. real = 0; return 1;} bool emptyqueue (queue q) {If (Q. front = Q. real) return true; return false;} bool full_queue (queue q) // full team {If (Q. real + 1) % maxsize = Q. front) return true; return false;} status queuelenngth (queue q) {// return queue length return (Q. real-Q.front + maxsize) % maxsize;} int POP (queue & Q, Int & E) // departure {If (! Emptyqueue (q) {e = Q. base [q. front]; q. front = (Q. front + 1) % maxsize; return 1;} else {printf ("the team is empty. An error occurred while leaving! \ N "); Return 0 ;}} int push (queue & Q, Int & E) // enter the queue {If (Q. real + 1) % maxsize = Q. front) // team full {printf ("team full, failed to join! \ N "); Return 0;} Q. base [q. real] = E; q. real = (Q. real + 1) % maxsize; return 1;} void queuetraverse (queue q) {int T = Q. front; while (T! = Q. real) {printf ("% d", Q. base [T]); t = (t + 1) % maxsize;} printf ("\ n"); return;} int main () {queue Q; int I, e; initqueue (Q); for (I = 1; I <= 5; ++ I) {push (Q, I); queuetraverse (Q);} POP (Q, e); queuetraverse (Q); POP (Q, e); queuetraverse (Q); push (Q, e); queuetraverse (Q); Return 0 ;}

Sequential structured cyclic queue

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.