3.1 队列的操作C(5段代码)
<h1>队列的结构体</h1>
<pre><code class="language-c">#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#define MaxSize 100
typedef struct{
int data[MaxSize];
int font;
int rear;
}queue;</code></pre>
<h1>队列初始化</h1>
<pre><code class="language-c">queue* init(){
queue* q = (queue*)malloc(sizeof(queue));
q-&gt;font = -1;
q-&gt;rear = -1;
return q;
}</code></pre>
<h1>入队</h1>
<pre><code class="language-c">int enQueue(queue* q, int t){
if(q-&gt;font==(q-&gt;rear+1)%MaxSize)
return 0;
q-&gt;rear = (q-&gt;rear+1)%MaxSize;
q-&gt;data[q-&gt;rear] = t;
return 1;
}</code></pre>
<h1>获取队列长度</h1>
<pre><code class="language-c">int length(queue* q){
return (q-&gt;rear+MaxSize - q-&gt;font) % MaxSize;
}</code></pre>
<h1>出队</h1>
<pre><code class="language-c">int deQueue(queue* q){
if(length(q) == 0)
return 0;
q-&gt;font = (q-&gt;font+1)%MaxSize;
int t = q-&gt;data[q-&gt;font];
return t;
}</code></pre>