Cyclic queue (c)

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> struct queuerecord {int capacity; int front; int rear; int size; char * array ;}; struct queuerecord * createqueue (INT size) {char * P; struct queuerecord * queue; P = (char *) malloc (size); queue = (struct queuerecord *) malloc (sizeof (struct queuerecord); queue-> capacity = size; queue-> size = 0; queue-> array = P; queue-> front = 0; queue-> rear = 0;} int isfull (struct queuerecord * q) {return Q-> size = Q-> capacity;} int isempty (struct queuerecord * q) {return Q-> size = 0;} int succ (INT value, struct queuerecord * q) {value ++; If (value = Q-> capacity) {value = 0;} return value;} void enqueue (char X, struct queuerecord * q) {If (isfull (q) {printf ("full \ n"); return;} Q-> size ++; q-> array [q-> rear] = x; q-> rear = succ (Q-> rear, q);} int dequeue (struct queuerecord * q) {int X; If (isempty (q) {printf ("Empty \ n"); Return-1 ;}else {q-> size --; X = Q-> array [q-> front]; q-> front = succ (Q-> front, q); Return x ;}} void queue_view (struct queuerecord * q) {int I; int TMP; printf ("front = % d \ n", Q-> front ); printf ("Rear = % d \ n", Q-> rear); printf ("size = % d \ n", Q-> size); for (I = 0; I <q-> size; I ++) {TMP = Q-> front + I; If (TMP> = Q-> capacity) {TMP = TMP-Q-> capacity;} printf ("No [% d] = % d \ n", TMP, Q-> array [TMP]);} int main () {int I; struct queuerecord * queue; queue = createqueue (5); for (I = 0; I <5; I ++) {enqueue (I, queue);} queue_view (Queue); for (I = 0; I <2; I ++) {printf ("de [% d] = % d \ n ", i, dequeue (Queue);} enqueue (5, queue); enqueue (6, queue ); printf ("******************* \ n"); queue_view (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.