Basic cyclic queue operations

Source: Internet
Author: User

tag: Io for art ar HTML line amp HTM

/** Basic cyclic queue operations. * Use less than one element space. It is agreed that "the queue header pointer is at the next position of the queue's tail Pointer" will be used as a sign of the queue's full capacity. * "The queue header pointer is equal to the end pointer of the queue" as a flag of queue null. */# Include <stdio. h> # include <stdbool. h> # include <malloc. h> # define maxqsize 100 typedef char elemtype; typedef struct {elemtype * base; int front; int rear;} queue; bool initqueue (queue * Queue ); int queuelength (queue Queue); bool enqueue (queue * queue, elemtype E); bool dequeue (queue * queue, elemtype * E); int main () {queue; elemtype ELEM [26], temp; int I, length; initqueue (& Queue); for (I = 0; I <26; I ++) {ele M [I] = 'A' + I; enqueue (& queue, ELEM [I]);} length = queuelength (Queue); printf ("length: % d \ n ", length); for (I = 0; I <length; I ++) {dequeue (& queue, & temp); putchar (temp );} return 0;} // construct an empty queue. Bool initqueue (queue * Queue) {queue-> base = (elemtype *) malloc (sizeof (elemtype) * maxqsize); If (! Queue-> base) return false; queue-> front = queue-> rear = 0; return true;} // evaluate the queue length. Int queuelength (queue Queue) {return (queue. Rear-queue. Front + maxqsize) % maxqsize;} // insert element e to the end of the queue. Bool enqueue (queue * queue, elemtype e) {If (queue-> rear + 1) % maxqsize = queue-> front) // return false when the queue is full; queue-> base [queue-> rear] = E; queue-> rear = (queue-> rear + 1) % maxqsize; return true ;} // Delete the queue Header element and use e to return its value. Bool dequeue (queue * queue, elemtype * E) {If (queue-> front = queue-> rear) // return false for empty queue; * E = queue-> base [queue-> front]; queue-> front = (queue-> front + 1) % maxqsize; return true ;}

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.