// 解答6.4 test_list.c #include #include // 環境によっては不要 typedef enum { false=0, true=1 } bool; // 線形リストの型定義 typedef struct list { int val; struct list *next; } List; // テスト対象のプロトタイプ宣言 void input_list_sub(char *,List*); bool duplicated(const List*); // 入力を文字列で与えるように修正 void input_list_sub(char *buf, List *l) { int v, n; List *p=l; char *bp = buf; while (sscanf(bp, "%d %n", &v, &n) >0 ) { List *newp = (List*)malloc(sizeof(List)); newp->val = v; p->next = newp; p = p->next; bp += n; } } // リストの要素の重複を検査する bool duplicated(const List *l) { List *lp = l->next; for (; lp; lp=lp->next) { List *lp2 = lp->next; for (; lp2; lp2=lp2->next) { if (lp->val == lp2->val) return true; } } return false; } List list; // 1行毎にテストデータを読むように修正 void test_input_list() { char buf[256]; while (fgets(buf,256,stdin) != 0) { input_list_sub(buf, &list); int d = duplicated(&list); printf("%d\n",d); } } int main() { test_input_list(); return 0; }