/*数値データを20個入力し、同一の個数を出力する*/ #include "stdafx.h" #include #include #define N 20 struct cell { int element; struct cell *next; }; int _tmain(int argc, _TCHAR* argv[]) { struct cell *init,*p,*r,*q; int a,i,k; p=NULL; r=(struct cell *)malloc(sizeof(cell)); printf("入力値\n"); scanf("%d",&a); r->element=a; r->next=p; q=r; init=r; for (i=2; i<=N;i++) { r=(struct cell *)malloc(sizeof(cell)); scanf("%d",&a); r->element=a; r->next=p; q->next=r; q=q->next; } printf("数値−個数\n"); p=init; while(p!=NULL) { q=p->next; r=p; k=1; while(q!=NULL) { if(p->element==q->element) { k=k+1; r->next=q->next; } else { r=r->next; } q=q->next; } printf("%d−%d\n",p->element,k); p=p->next; } getch(); return 0; } 結果例 入力値 3 5 12 35 12 45 4 12 3 3 7 60 35 12 5 5 88 17 25 4 数値−個数 3−3 5−3 12−4 35−2 45−1 4−2 7−1 60−1 88−1 17−1 25−1