|
何度も丁寧に教えてくださり、
本当にありがとうございました。
家のパソコンではできなかったのですが、
学校のパソコンでやってみると、
コンパイルも出力もきちんとできるようになりました。
本当にうれしいです。
これからもがんばって勉強していきたいと思いますので、
どうぞよろしくお願いいたします。
***最終版***
/* 線形リストを使って文字を出力する */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*--- テキスト ---*/
typedef struct {
char text[500]; /* テキスト */
} Data;
/*--- ノード ---*/
typedef struct __node {
Data data; /* データ */
struct __node *next; /* 後続ノードへのポインタ */
} Node;
/*--- 線形リスト ---*/
typedef struct {
Node *head; /* 先頭ノードへのポインタ */
Node *crnt; /* 着目ノードへのポインタ */
} List;
/*--- 一つのノードを動的に確保 ---*/
Node *AllocNode(void)
{
return ((Node *)calloc(1, sizeof(Node)));
}
/*--- 線形リストを初期化 ---*/
void InitList(List *list)
{
list->head = NULL; /* 先頭ノード */
list->crnt = NULL; /* 着目ノード */
}
/*--- ノードの各メンバに値を設定 ----*/
void SetNode(Node *n, Data x, Node *next)
{
char *s,*e;
n->data = x; /*文字列を入れていなかった */
n->next = next;
/* strrev 文字列反転、wchar は考慮してません*/
s = n->data.text;
e = s + strlen(s);
while (--e>s) {
register char tmp = *e;
*e = *s;
*s++=tmp;
}
}
/*--- 先頭にノードを挿入 ---*/
void InsertFront(List *list, Data x)
{
Node *ptr = list->head;
list->head = list->crnt = AllocNode();
SetNode(list->head, x, ptr);
}
/*--- データを表示 ---*/
void PrintData(Data x)
{
printf("%s\n", x.text);
}
/*--- ノードのデータを表示 ---*/
void PrintList(List *list)
{
if (list->head == NULL)
puts("ノードがありません。");
else {
Node *ptr = list->head;
while (ptr != NULL) {
PrintData(ptr->data);
ptr = ptr->next; /* 後続ノードに着目 */
}
}
}
/*--- データの入力 ---*/
Data Read(const char *message)
{
Data temp;
printf("%sを入力してください。:", message);
scanf("%s", temp.text);
return (temp);
}
/*--- メイン ---*/
int main(void)
{
List list;
Data x;
InitList(&list); /* 線形リストの初期化 */
x = Read("テキスト");
InsertFront(&list, x);
PrintList(&list);
return 0;
}
|