当日のOHP資料です。
/*
* icell.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "icell.h"
/*
* new_ICell : 新しい ICell を確保 ( NULL は、確保に失敗した場合.. )
* int value; -- この Cell が保持する整数値データ
*/
ICell *new_ICell ( int value ) {
ICell *newObject = xalloc ( ICell ); /* 新しい ICell を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> next = NULL; /* 個々の ICell は最初は「次」がない */
newObject -> data = value;
/*
newObject
|
| +-------+
+--> | *--------> NULL
+-------+
|value |
+-------+
*/
}
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* free_ICell
*/
void free_ICell ( ICell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
xfree ( object );
}
}
/*
* printf_ICell
*/
void printf_ICell ( char *format, ICell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
printf ( format, object -> data );
}
}
/*
* print_ICell
*/
void print_ICell ( ICell *object ) {
printf_ICell ( "%d", object ); /* 標準の出力形式は 「%d」で.. */
}
/*
* append_ICell
*/
ICell *append_ICell ( ICell *here, ICell *append ) {
if ( here != NULL ) {
here -> next = append;
}
return append;
}
/*
*
*/
C:\usr\c\> icell C:\usr\c\>
/*
* ilist.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "icell.h"
#include "boolean.h"
#include "ilist.h"
/*
* new_IList : 新しい IList を確保 ( NULL は、確保に失敗した場合.. )
*/
IList *new_IList () {
IList *newObject = xalloc ( IList ); /* 新しい IList を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> top = NULL; /* 個々の IList は最初は「空」になる */
}
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* isEmpty_IList
*/
Boolean isEmpty_IList ( IList *object ) {
if ( object != NULL ) { /* NULL でなく.. */
if ( object -> top != NULL ) { /* 先頭の要素があれば.. */
return TRUE; /* 「真(TRUE)」を返す */
}
}
return FALSE; /* その他の場合は「偽(FALSE)」を返す */
}
/*
* free_IList
*/
void free_IList ( IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭のデータを取り出し */
while ( cp != NULL ) { /* まだ要素があれば.. */
ICell *nx = cp -> next; /* 次の要素を予め取り出しておいて .. */
free_ICell ( cp ); /* その ICell を開放し.. */
cp = nx; /* 次の要素を考える.. */
}
xfree ( object ); /* 中身が開放されたので安心して自分の開放 */
}
}
/*
* printf_IList
*/
void printf_IList ( char *format, IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭のデータを取出.. */
while ( cp != NULL ) { /* まだ要素があれば.. */
printf_ICell ( format, cp ); /* その要素を出力し */
cp = cp -> next; /* 次の要素をみる */
}
}
}
/*
* print_IList
*/
void print_IList ( IList *object ) {
printf_IList ( "%d ", object );
}
/*
* insert_IList
*/
IList *insert_IList ( IList *object, int data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = new_ICell ( data ); /* 新しい ICell を作成し.. */
if ( cp != NULL ) { /* それが作れれば.. */
cp -> next = object -> top; /* その要素を先頭に挿入 */
object -> top = cp;
}
}
return object;
}
/*
* delete_top_IList
*/
void delete_top_IList ( IList *object ) {
if ( object != NULL ) {
ICell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 先頭の ICell があれば.. */
object -> top = cp -> next; /* その ICell を取り外し.. */
free_ICell ( cp ); /* その ICell を開放 */
}
}
}
/*
* append_IList
*/
IList *append_IList ( IList *object, int data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *newICell = new_ICell ( data ); /* 新しい ICell を作成 */
if ( newICell != NULL ) { /* 新しい ICell が作れれば.. */
/* 追加のために、まず、最後の要素の ICell を搜す */
ICell *last = NULL; /* 最後の要素候補へのポインター */
ICell *cp = object -> top; /* 先頭の要素 */
while ( cp != NULL ) { /* 最後の要素をさがす */
last = cp; /* 最後の要素候補を更新 */
cp = cp -> next; /* 次の要素を確認 */
}
if ( last == NULL ) { /* 実は List は空だった */
object -> top = newICell; /* 先頭に追加 */
} else {
last -> next = newICell; /* 最後の要素の次に追加 */
}
}
}
return object;
}
/*
* delete_end_IList
*/
void delete_end_IList ( IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 要素があった場合に初めて処理開始 */
/* 削除のために、まず、最後の要素の前の ICell を搜す */
ICell *pre = NULL; /* 最後の要素の前の要素候補へのポインター */
ICell *last = cp; /* 最後の要素候補へのポインター */
cp = cp -> next;
while ( cp != NULL ) { /* 要素がある限り.. */
pre = last; /* pre は last の前 */
last = cp; /* last は cp の前 */
cp = cp -> next; /* cp を次の要素に.. */
}
if ( pre == NULL ) { /* 要素が一つしかなかった */
object -> top = NULL; /* List は空に */
} else {
pre -> next = NULL; /* 最後の要素を取り外す */
}
free_ICell ( last ); /* 最後の要素を開放 */
}
}
}
/*
* search_IList
*/
ICell *search_IList ( IList *object, int number ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top;
while ( cp != NULL ) { /* 要素があれば.. */
if ( cp -> data == number ) { /* データを比較 */
return cp; /* 見付かったのでそれを返す */
}
cp = cp -> next; /* ちがったら、次を調べる */
}
}
return NULL; /* 見付からなかった場合は NULL を返す */
}
/*
*
*/
C:\usr\c\> ilist C:\usr\c\>
Download : sample-001.c ( SJIS 版 )
/*
* 2011/12/09 sample-001.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "boolean.h"
#include "ilist.h"
/*
*
*/
#define COMMAND_LINE_SIZE 1024
/*
*
*/
int main ( void ) {
char cmdline [ COMMAND_LINE_SIZE ];
IList *list = new_IList();
int cmd = 'h';
int number = 0;
/*
*
*/
if ( list == NULL ) {
printf ( "List が確保できませんでしたので終了します\n" );
exit ( -1 ); /* プログラムを終了 */
}
/*
*
*/
while ( cmd != 'q' ) {
/* コマンドの実行 */
switch ( cmd ) {
default:
printf ( "I don't know %c command\n", cmd );
// break;
case 'h':
printf ( "Command Help, Quit, Print, Insert #, Append #, Check, delete \
Top, delete End, Find #\n" );
break;
case 'q':
break;
case 'p':
print_IList ( list );
printf ( "\n" );
break;
case 'i':
insert_IList ( list, number );
break;
case 'a':
append_IList ( list, number );
break;
case 'c':
if ( isEmpty_IList ( list ) == FALSE ) {
printf ( "Empty\n" );
} else {
printf ( "No Empty\n" );
}
break;
case 't':
delete_top_IList ( list );
break;
case 'e':
delete_end_IList ( list );
break;
case 'f':
if ( search_IList ( list, number ) != NULL ) {
printf ( "Found\n" );
} else {
printf ( "Not Found\n" );
}
break;
}
/* コマンドの入力 */
printf ( "cmd> " );
fgets ( cmdline, COMMAND_LINE_SIZE, stdin ); /* 一行入力 */
cmd = cmdline[0]; /* 先頭の一文字がコマンド */
number = atoi ( cmdline + 2 ); /* 3 文字目以後は引数 */
}
/*
*
*/
free_IList ( list );
/*
*
*/
return 0;
}
/*
*
*/
p i 10 i 20 p a -10 a -20 p t p e p q
C:\usr\c\> sample-001< sample-001.in
Command Help, Quit, Print, Insert #, Append #, Check, delete Top, \
delete End, Find #
cmd> p
cmd> i 10
cmd> i 20
cmd> p
20 10
cmd> a -10
cmd> a -20
cmd> p
20 10 -10 -20
cmd> t
cmd> p
10 -10 -20
cmd> e
cmd> p
10 -10
cmd> q
C:\usr\c\>
Download : xalloc.h ( SJIS 版 )
/* * xalloc.h */ #ifndef __XALLOC_H__ #define __XALLOC_H__ /* * */ #define xalloc(Type) ((Type *)malloc(sizeof(Type))) /* 指定した型の領域を一つ分 alloc し、かつその型にキャストして返す */ #define xfree(PTR) (free(PTR)) /* * */ #endif
Download : boolean.h ( SJIS 版 )
/* * boolean.h */ #ifndef __BOOLEAN_H__ #define __BOOLEAN_H__ /* * */ #define FALSE (0) /* 偽 (0) */ #define TRUE (!FALSE) /* 真 (0 以外) */ /* * */ #define Boolean int /* 真偽値は、単なる整数値 */ /* * */ #endif
/*
* icell.h
*/
#ifndef __ICELL_H__
#define __ICELL_H__
/*
*
*/
typedef struct icell { /* 構造体には構造体を区別するタグ名を付ける事ができる */
struct icell *next; /* 次の ICell へのポインター */
/* 本当は「ICell *」と書きたいが、この時点では存在しないので.. */
int data; /* 要素 ( int 型 ) */
} ICell;
/*
*
*/
extern ICell *new_ICell ( int value );
extern void free_ICell ( ICell *object );
extern void printf_ICell ( char *format, ICell *object );
extern void print_ICell ( ICell *object );
extern ICell *append_ICell ( ICell *here, ICell *append );
/*
*
*/
#endif
C:\usr\c\> icell C:\usr\c\>
/*
* ilist.h
*/
#ifndef __ILIST_H__
#define __ILIST_H__
/*
*
*/
#ifndef __ICELL_H__
#include "icell.h"
#endif
#ifndef __BOOLEAN_H__
#include "boolean.h"
#endif
/*
*
*/
typedef struct {
ICell *top;
} IList;
/*
*
*/
extern IList *new_IList ();
extern void free_IList ( IList *object );
extern void printf_IList ( char *format, IList *object );
extern void print_IList ( IList *object );
extern ICell *search_IList ( IList *object, int number );
/*
*
*/
#endif
C:\usr\c\> ilist C:\usr\c\>
Download : 20111209-01.c ( SJIS 版 )
/*
* 20111209-1-QQQQ.c
*/
#include <stdio.h>
/*
*
*/
#include "icell.h"
#include "boolean.h"
#include "ilist.h"
/*
* nth_IList
* number がリストの何番目かを答える関数を作る
* 例 1 : List が 1,2,3,4 で、number が 3 の時には、2 が答になる
* 例 2 : List が 1,2,3,4 で、number が 1 の時には、0 が答になる
* 例 3 : List が 1,2,3,4 で、number が 5 の時には、-1 が答になる
*/
int nth_IList ( IList *object, int number ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top;
int nth = 0; /* 先頭なら 0 番目 */
while ( cp != NULL ) { /* 要素があれば.. */
if ( cp -> data == number ) { /* データを比較 */
/* 搜し物が見付かったので... */
/*
** この部分を完成させなさい
*/
}
/* ここにはなかったので、次の場所をみる.. */
/*
** この部分を完成させなさい
*/
cp = cp -> next; /* ちがったら、次を調べる */
}
}
/* 最後までみたけど見付からなかった.. */
return -1; /* 見付からない場合は -1 を返す */
}
/*
*
*/
void main()
{
IList *list = new_IList();
int i;
if ( list != NULL ) {
append_IList ( list, 20 );
append_IList ( list, 30 );
append_IList ( list, 10 );
print_IList ( list );
printf ( "\n" );
for ( i = 0; i < 50; i += 10 ) {
printf ( "%d is %dth elements\n", i, nth_IList ( list, i ) );
}
free_IList ( list );
}
}
1 2 3 4 5 6 7 8 9 0 0 1 0 1 0 1 0 0
C:\usr\c\> 01< 01.in 20 30 10 0 is -1th elements 10 is 2th elements 20 is 0th elements 30 is 1th elements 40 is -1th elements C:\usr\c\>