119 lines
6.8 KiB
Markdown
119 lines
6.8 KiB
Markdown
---
|
|
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"><stdio.h></span>
|
|
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></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->num = q->front = q->rear = <span class="synConstant"></span>;
|
|
<span class="synStatement">if</span> ((q->que = calloc(max, <span class="synStatement">sizeof</span>(<span class="synType">int</span>))) == <span class="synConstant">NULL</span>) {
|
|
q->max = <span class="synConstant"></span>;
|
|
<span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
|
}
|
|
q->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->que != <span class="synConstant">NULL</span>) {
|
|
free(q->que);
|
|
q->max = q->num = q->front = q->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->num >= q->max) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
|
<span class="synStatement">else</span> {
|
|
q->num++;
|
|
q->que[q->rear++] = x;
|
|
<span class="synStatement">if</span> (q->rear == q->max) q->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->num <= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
|
<span class="synStatement">else</span> {
|
|
q->num--;
|
|
*x = q->que[q->front++];
|
|
<span class="synStatement">if</span> (q->front == q->max) q->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->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->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->num <= <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->num >= q->max);
|
|
}
|
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
|
{
|
|
Queue que;
|
|
<span class="synStatement">if</span> (QueueAlloc(&que, <span class="synConstant">100</span>) == -<span class="synConstant">1</span>) {
|
|
puts(<span class="synConstant">"Fail to secure the memory of the queue."</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">"Current Number of the Data: </span><span class="synSpecial">%d</span><span class="synConstant"> / </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, QueueNo(&que), QueueSize(&que));
|
|
printf (<span class="synConstant">"(1) Enque (2) Deque 0) Exit"</span>);
|
|
scanf(<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant">"</span>, &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">"Data: "</span>);
|
|
scanf(<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant">"</span>, &x);
|
|
<span class="synStatement">if</span> (QueueEnque(&que, x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">"Fail to enque the data into the que"</span>);
|
|
<span class="synStatement">break</span>;
|
|
<span class="synStatement">case</span> <span class="synConstant">2</span>:
|
|
<span class="synStatement">if</span> (QueueDeque(&que, &x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">"Cannot Deque!"</span>);
|
|
<span class="synStatement">else</span> printf (<span class="synConstant">"The Decued Data: </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, x);
|
|
<span class="synStatement">break</span>;
|
|
}
|
|
}
|
|
QueueFree(&que);
|
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
|
}
|
|
</pre>
|
|
</div> |