blog/content/post/2008/05/12/2008-05-12-『新版-c言語によるアルゴリズム...

151 lines
7.7 KiB
Markdown

---
title: 『新版 C言語によるアルゴリズムとデータ構造』
author: kazu634
date: 2008-05-12
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:4005;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<p>
 第四章になりました。スタックとキューです。いまいち良く理解できていませんが、とりあえずこれでスタックが実装で着るみたいです。
</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="synComment">/* スタックを実現する構造体 */</span>
<span class="synType">typedef</span> <span class="synType">struct</span>
{
<span class="synType">int</span> max; <span class="synComment">/* スタックのサイズ */</span>
<span class="synType">int</span> ptr; <span class="synComment">/* スタックポインタ */</span>
<span class="synType">int</span> *stk; <span class="synComment">/* スタック(の先頭要素へのポインタ) */</span>
} Stack;
<span class="synComment">/* initializing the stack */</span>
<span class="synType">int</span> StackAlloc(Stack *s, <span class="synType">int</span> max)
{
s-&#62;ptr = <span class="synConstant"></span>;
<span class="synComment">/* 配列の確保に失敗した場合 */</span>
<span class="synStatement">if</span> ((s-&#62;stk = calloc(max, <span class="synStatement">sizeof</span>(<span class="synType">int</span>))) == <span class="synConstant">NULL</span>) {
s-&#62;max = <span class="synConstant"></span>;
<span class="synStatement">return</span>(-<span class="synConstant">1</span>);
}
s-&#62;max = max;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
<span class="synComment">/* Free the memory the stack occupies */</span>
<span class="synType">void</span> StackFree(Stack *s)
{
<span class="synStatement">if</span> (s-&#62;stk != <span class="synConstant">NULL</span>) {
free(s-&#62;stk);
s-&#62;max = s-&#62;ptr = <span class="synConstant"></span>;
}
}
<span class="synComment">/* Push the data into the stack */</span>
<span class="synType">int</span> StackPush(Stack *s, <span class="synType">int</span> x)
{
<span class="synStatement">if</span> (s-&#62;ptr &#62;= s-&#62;max) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
s-&#62;stk[s-&#62;ptr++] = x;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
<span class="synComment">/* Pop the data from the stack */</span>
<span class="synType">int</span> StackPop(Stack *s, <span class="synType">int</span> *x)
{
<span class="synComment">/* スタックが空の時 */</span>
<span class="synStatement">if</span> (s-&#62;ptr &#60;= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
*x = s-&#62;stk[--s-&#62;ptr];
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
<span class="synComment">/* Peek the stack */</span>
<span class="synType">int</span> StackPeek(<span class="synType">const</span> Stack *s, <span class="synType">int</span> *x)
{
<span class="synStatement">if</span> (s-&#62;ptr &#60;= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
*x = s-&#62;stk[s-&#62;ptr - <span class="synConstant">1</span>];
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
<span class="synComment">/* Get the size of the stack */</span>
<span class="synType">int</span> StackSize(<span class="synType">const</span> Stack *s)
{
<span class="synStatement">return</span>(s-&#62;max);
}
<span class="synComment">/* The numbers of the data inside the stack */</span>
<span class="synType">int</span> StackNo(<span class="synType">const</span> Stack *s)
{
<span class="synStatement">return</span>(s-&#62;ptr);
}
<span class="synComment">/* Is the stack empty? */</span>
<span class="synType">int</span> StackIsEmpty(<span class="synType">const</span> Stack *s)
{
<span class="synStatement">return</span>(s-&#62;ptr &#60;= <span class="synConstant"></span>);
}
<span class="synComment">/* Is the stack full? */</span>
<span class="synType">int</span> StackIsFull(<span class="synType">const</span> Stack *s)
{
<span class="synStatement">return</span>(s-&#62;ptr &#62;= s-&#62;max);
}
<span class="synComment">/* Clearing the Stack */</span>
<span class="synType">void</span> StackClear(Stack *s)
{
s-&#62;ptr = <span class="synConstant"></span>;
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
Stack s;
<span class="synStatement">if</span> (StackAlloc(&#38;s, <span class="synConstant">100</span>) == -<span class="synConstant">1</span>) {
puts(<span class="synConstant">&#34;Try to allocate the memory, but failed...&#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;現在のデータ数: </span><span class="synSpecial">%d</span><span class="synConstant"> / </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, StackNo(&#38;s), StackSize(&#38;s));
printf (<span class="synConstant">&#34;(1) Push (2) Pop (0) Quit: &#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> (StackPush(&#38;s, x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">&#34;Failed Pushing the data&#34;</span>);
<span class="synStatement">break</span>;
<span class="synStatement">case</span> <span class="synConstant">2</span>: <span class="synStatement">if</span> (StackPop(&#38;s, &#38;x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">&#34;Cannot Pop the data&#34;</span>);
<span class="synStatement">else</span> printf (<span class="synConstant">&#34;Pop </span><span class="synSpecial">%d</span><span class="synConstant"> into the stack.</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, x);
<span class="synStatement">break</span>;
}
}
StackFree;
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<h4>
気になった部分
</h4>
<pre class="syntax-highlight">
<span class="synType">int</span> *x;
x = calloc(適当な値, <span class="synStatement">sizeof</span>(<span class="synType">int</span>));
</pre>
<p>
こいつは動的に配列の領域を確保しているのだろう。
</p>
<h4>
よく分からなかった部分
</h4>
<pre class="syntax-highlight">
<span class="synStatement">return</span> (s-&#62;ptr &#60;= <span class="synConstant"></span>);
</pre>
<pre class="syntax-highlight">
<span class="synStatement">return</span> (s-&#62;ptr &#62;= s-&#62;max);
</pre>
<p>
の意味が分からなかった。
</p>
</div>