Implementation of C language queue and C language queue
In C ++, the queue can directly use std: queue
The C language of the queue is implemented as follows:
1 queue. c 2 3/*** @ brief queue, sequential storage, and cyclic queue. 5 */6 # include <stdlib. h>/* for malloc (), free () */7 # include <string. h>/* for memcpy () */8 9 # ifndef _ cplusplus 10 typedef char bool; 11 # define false 0 12 # define true 1 13 # endif 14 15 typedef int queue_elem_t; // element type 16 17/* 18 * @ struct 19 * @ brief queue struct definition. 20 * @ note 21 */22 typedef struct queue_t {23 int front;/* team head */24 int rear;/* team end */25 int capacity; /* capacity, in the unit of elements */26 queue_elem_t * elems;/* memory block for storing data */27} queue_t; 28 29/*** @ brief initialize the queue. 31 * @ param [out] q queue struct pointer 32 * @ param [in] capacity initial capacity 33 * @ return No 34 */35 void queue_init (queue_t * q, const int capacity) {36 q-> front = 0; 37 q-> rear = 0; 38 q-> capacity = capacity; 39 q-> elems = (queue_elem_t *) malloc (capacity * sizeof (queue_elem_t); 40} 41 42/** 43 * @ brief release queue. 44 * @ param [inout] q queue Object Pointer 45 * @ return No 46 */47 void queue_uninit (queue_t * q) {48 q-> front = 0; 49 q-> rear = 0; 50 q-> capacity = 0; 51 free (q-> elems); 52 q-> elems = NULL; 53} 54 55/** 56 * @ brief determine whether the queue is empty. 57 * @ param [in] q queue struct pointer 58 * @ return is null, return TRUE, otherwise return FALSE 59 */60 bool queue_empty (const queue_t * q) {61 return q-> front = q-> rear; 62} 63 64/** 65 * @ brief get the number of elements. 66 * @ param [in] s stack Object Pointer 67 * @ return number of elements 68 */69 int queue_size (const queue_t * q) {70 return (q-> rear-q-> front + q-> capacity) % q-> capacity; 71} 72 73/** 74 * @ brief add elements at the end of the team. 75 * @ param [in] q pointer to the queue struct 76 * @ param [in] x element 77 * @ return without 78 */79 void queue_push (queue_t * q, const queue_elem_t x) {80 if (q-> rear + 1) % q-> capacity = q-> front) 81 {82 // full, re-allocate memory 83 queue_elem_t * tmp = (queue_elem_t *) malloc (q-> capacity * 2 * sizeof (queue_elem_t); 84 if (q-> front <q-> rear) 85 {86 memcpy (tmp, q-> elems + q-> front, (q-> rear-q-> front) * sizeof (queue_elem_t )); 87 q-> rear-= q-> front; 88 q-> front = 0; 89} 90 else if (q-> front> q-> rear) 91 {92/* Copy data from q-> front to q-> capacity */93 memcpy (tmp, q-> elems + q-> front, (q-> capacity-q-> front) * sizeof (queue_elem_t )); 94/* Copy data from q-> data [0] to q-> data [rear] */95 memcpy (tmp + (q-> capacity-q-> front), q-> elems, q-> rear * sizeof (queue_elem_t); 96 q-> rear + = q-> capacity-q-> front; 97 q-> front = 0; 98} 99 free (q-> elems); 100 q-> elems = tmp; 101 q-> capacity * = 2; 102} 103 q-> elems [q-> rear] = x; 104 q-> rear = (q-> rear + 1) % q-> capacity; 105} 106 107 108/** 109 * @ brief delete an element in the queue header. 110 * @ param [in] q pointer to the queue struct 111 * @ param [out] x stores elements of the exit queue 112 * @ return No 113 */114 void queue_pop (queue_t * q) {115 q-> front = (q-> front + 1) % q-> capacity; 116} 117 118/** 119 * @ brief get the first element of the team. 120 * @ param [in] q queue Object Pointer 121 * @ return first element 122 */123 queue_elem_t queue_front (const queue_t * q) {124 return q-> elems [q-> front]; 125} 126 127/** 128 * @ brief get the first element of the team. 129 * @ param [in] q queue Object Pointer 130 * @ return first element 131 */132 queue_elem_t queue_back (const queue_t * q) {133 return q-> elems [q-> rear-1]; 134}