ですが、mallocを使わずにプログラムを考えたいのです。
(今後、マイコン等メモリ制限のある機器を作成したいため)
自分なりに調べた結果、「スタック」および「キュー」が引っかったので勉強をしましたが、どのようにソースを書けばいいのか分かりません。
参考にでもソースを教えていただけないでしょうか?
また、詳しく解説されている(初級と中級レベルそれぞれについて)サイトや書籍がありましたらそちらもお願いいたします。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMBER 3
#define NAME_MAX 20
#define WEEK_MAX 11
// 構造体の定義
struct node {
char name[NAME_MAX]; // 名前
int month; // 月
int day; // 日
char week[WEEK_MAX]; // 週
struct node *next; // nodeへのポインタ
} ;
// 配列
struct node data[]={
{ "riri", 3, 5, "fir" },
{ "hara", 4, 6, "san" },
{ "maka", 3, 5, "thu" }
};
// リストに要素を追加する
struct node* list_add( struct list* list, char n[], int m, int d, char w[] )
{
// 新しい要素を作成
struct node* node = ( struct node* )malloc( sizeof( struct node ) );
strcpy( node->name,n );
node->month = m;
node->day = d;
strcpy( node->week, w );
// 次の要素がないことを示すためにNULLを代入
node->next = NULL;
if( list == NULL )
{
// リストが空の場合は特別扱いする
return node;
}
else
{
// リストの末尾の要素を探す
struct node* p = list;
while( p->next != NULL )
{
p = p->next;
}
// ここで変数pは末尾の要素をさす
p->next = node;
return list;
}
}
// 表示
void display( struct node* list )
{
printf( "%s %d/%d %s \n",
list->name, list->month, list->day, list->week );
printf( "%s %d/%d %s \n",
list->next->name, list->next->month, list->next->day, list->next->week );
printf( "%s %d/%d %s \n",
list->next->next->name, list->next->next->month, list->next->next->day, list->next->next->week );
}
int main( void )
{
struct node* list = NULL;
int i;
for( i = 0; i < NUMBER; i ++ )
list = list_add( list, data[i].name, data[i].month, data[i].day, data[i].week );
display( list );
return 0;
}