blog/content/post/2008/05/20/2008-05-20-00000926.md

119 lines
6.8 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: キューの実装
author: kazu634
date: 2008-05-20
url: /2008/05/20/_998/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4027;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<p>
 『<a href="http://d.hatena.ne.jp/asin/4797331631" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4797331631', '新版 C言語によるアルゴリズムとデータ構造');">新版 C言語によるアルゴリズムとデータ構造</a>』から。
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdlib.h&#62;</span>
<span class="synType">typedef</span> <span class="synType">struct</span>
{
<span class="synType">int</span> max; <span class="synComment">/* the size of the queue */</span>
<span class="synType">int</span> num; <span class="synComment">/* Current Number of the elements */</span>
<span class="synType">int</span> front; <span class="synComment">/* the front cursor */</span>
<span class="synType">int</span> rear; <span class="synComment">/* the rear cursor */</span>
<span class="synType">int</span> *que; <span class="synComment">/* the pointer to the first element */</span>
} Queue;
<span class="synComment">/* initialization of the que */</span>
<span class="synType">int</span> QueueAlloc(Queue *q, <span class="synType">int</span> max)
{
q-&#62;num = q-&#62;front = q-&#62;rear = <span class="synConstant"></span>;
<span class="synStatement">if</span> ((q-&#62;que = calloc(max, <span class="synStatement">sizeof</span>(<span class="synType">int</span>))) == <span class="synConstant">NULL</span>) {
q-&#62;max = <span class="synConstant"></span>;
<span class="synStatement">return</span>(-<span class="synConstant">1</span>);
}
q-&#62;max = max;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
<span class="synComment">/* Clear the queue */</span>
<span class="synType">void</span> QueueFree(Queue *q)
{
<span class="synStatement">if</span> (q-&#62;que != <span class="synConstant">NULL</span>) {
free(q-&#62;que);
q-&#62;max = q-&#62;num = q-&#62;front = q-&#62;rear = <span class="synConstant"></span>;
}
}
<span class="synComment">/* Enque the data into the queue */</span>
<span class="synType">int</span> QueueEnque(Queue *q, <span class="synType">int</span> x)
{
<span class="synStatement">if</span> (q-&#62;num &#62;= q-&#62;max) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
<span class="synStatement">else</span> {
q-&#62;num++;
q-&#62;que[q-&#62;rear++] = x;
<span class="synStatement">if</span> (q-&#62;rear == q-&#62;max) q-&#62;rear = <span class="synConstant"></span>;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
}
<span class="synComment">/* Deque the data from the queue */</span>
<span class="synType">int</span> QueueDeque(Queue *q, <span class="synType">int</span> *x)
{
<span class="synStatement">if</span> (q-&#62;num &#60;= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
<span class="synStatement">else</span> {
q-&#62;num--;
*x = q-&#62;que[q-&#62;front++];
<span class="synStatement">if</span> (q-&#62;front == q-&#62;max) q-&#62;front = <span class="synConstant"></span>;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
}
<span class="synComment">/* the size of the que */</span>
<span class="synType">int</span> QueueSize(<span class="synType">const</span> Queue *q)
{
<span class="synStatement">return</span>(q-&#62;max);
}
<span class="synComment">/* the number of the data stored in the queue */</span>
<span class="synType">int</span> QueueNo(<span class="synType">const</span> Queue *q)
{
<span class="synStatement">return</span>(q-&#62;num);
}
<span class="synComment">/* Is the queue empty? */</span>
<span class="synType">int</span> QueueIsEmpty(<span class="synType">const</span> Queue *q)
{
<span class="synStatement">return</span>(q-&#62;num &#60;= <span class="synConstant"></span>);
}
<span class="synComment">/* Is the queue full? */</span>
<span class="synType">int</span> QueueIsFull(<span class="synType">const</span> Queue *q)
{
<span class="synStatement">return</span>(q-&#62;num &#62;= q-&#62;max);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
Queue que;
<span class="synStatement">if</span> (QueueAlloc(&#38;que, <span class="synConstant">100</span>) == -<span class="synConstant">1</span>) {
puts(<span class="synConstant">&#34;Fail to secure the memory of the queue.&#34;</span>);
<span class="synStatement">return</span>(<span class="synConstant">1</span>);
}
<span class="synStatement">while</span> (<span class="synConstant">1</span>) {
<span class="synType">int</span> m, x;
printf (<span class="synConstant">&#34;Current Number of the Data: </span><span class="synSpecial">%d</span><span class="synConstant"> / </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, QueueNo(&#38;que), QueueSize(&#38;que));
printf (<span class="synConstant">&#34;(1) Enque (2) Deque 0) Exit&#34;</span>);
scanf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, &#38;m);
<span class="synStatement">if</span> (m == <span class="synConstant"></span>) <span class="synStatement">break</span>;
<span class="synStatement">switch</span> (m) {
<span class="synStatement">case</span> <span class="synConstant">1</span>:
printf (<span class="synConstant">&#34;Data: &#34;</span>);
scanf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, &#38;x);
<span class="synStatement">if</span> (QueueEnque(&#38;que, x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">&#34;Fail to enque the data into the que&#34;</span>);
<span class="synStatement">break</span>;
<span class="synStatement">case</span> <span class="synConstant">2</span>:
<span class="synStatement">if</span> (QueueDeque(&#38;que, &#38;x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">&#34;Cannot Deque!&#34;</span>);
<span class="synStatement">else</span> printf (<span class="synConstant">&#34;The Decued Data: </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, x);
<span class="synStatement">break</span>;
}
}
QueueFree(&#38;que);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
</div>