151 lines
7.7 KiB
Markdown
151 lines
7.7 KiB
Markdown
|
---
|
||
|
title: 『新版 C言語によるアルゴリズムとデータ構造』
|
||
|
author: kazu634
|
||
|
date: 2008-05-12
|
||
|
url: /2008/05/12/_989/
|
||
|
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"><stdio.h></span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></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->ptr = <span class="synConstant"></span>;
|
||
|
<span class="synComment">/* 配列の確保に失敗した場合 */</span>
|
||
|
<span class="synStatement">if</span> ((s->stk = calloc(max, <span class="synStatement">sizeof</span>(<span class="synType">int</span>))) == <span class="synConstant">NULL</span>) {
|
||
|
s->max = <span class="synConstant"></span>;
|
||
|
<span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
||
|
}
|
||
|
s->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->stk != <span class="synConstant">NULL</span>) {
|
||
|
free(s->stk);
|
||
|
s->max = s->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->ptr >= s->max) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
||
|
s->stk[s->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->ptr <= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
||
|
*x = s->stk[--s->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->ptr <= <span class="synConstant"></span>) <span class="synStatement">return</span>(-<span class="synConstant">1</span>);
|
||
|
*x = s->stk[s->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->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->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->ptr <= <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->ptr >= s->max);
|
||
|
}
|
||
|
<span class="synComment">/* Clearing the Stack */</span>
|
||
|
<span class="synType">void</span> StackClear(Stack *s)
|
||
|
{
|
||
|
s->ptr = <span class="synConstant"></span>;
|
||
|
}
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
Stack s;
|
||
|
<span class="synStatement">if</span> (StackAlloc(&s, <span class="synConstant">100</span>) == -<span class="synConstant">1</span>) {
|
||
|
puts(<span class="synConstant">"Try to allocate the memory, but failed..."</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">"現在のデータ数: </span><span class="synSpecial">%d</span><span class="synConstant"> / </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, StackNo(&s), StackSize(&s));
|
||
|
printf (<span class="synConstant">"(1) Push (2) Pop (0) Quit: "</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> (StackPush(&s, x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">"Failed Pushing the data"</span>);
|
||
|
<span class="synStatement">break</span>;
|
||
|
<span class="synStatement">case</span> <span class="synConstant">2</span>: <span class="synStatement">if</span> (StackPop(&s, &x) == -<span class="synConstant">1</span>) puts(<span class="synConstant">"Cannot Pop the data"</span>);
|
||
|
<span class="synStatement">else</span> printf (<span class="synConstant">"Pop </span><span class="synSpecial">%d</span><span class="synConstant"> into the stack.</span><span class="synSpecial">\n</span><span class="synConstant">"</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->ptr <= <span class="synConstant"></span>);
|
||
|
</pre>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synStatement">return</span> (s->ptr >= s->max);
|
||
|
</pre>
|
||
|
|
||
|
<p>
|
||
|
の意味が分からなかった。
|
||
|
</p>
|
||
|
</div>
|