2011-01-19 5 views
1

私は単純なリンクリストをコーディングしていますが、少し問題に直面しています。このプログラムは、ユーザーによって名前、年齢、DOBを受け取り、そのメモリが動的に割り当てられるようなものです。ユーザーからデータを取得した後、ユーザーが入力した名前を検索しています。存在する場合は、そのすべての詳細を印刷する必要があります。 はここCでリンクされたリストプログラム

//function declarations 
struct node *initnode(char *, char *, char *); 

void add(struct node *); 

struct node *printnode(struct node *); 

struct node *searchname(struct node *, char *); 

struct node { 
    char name[25]; 
    char age[10]; 
    char dob[10]; 
    struct node *next; 
}; 

struct node *head = (struct node *) NULL; 

struct node *initnode(char *name, char *age, char *dob1) 
{ 
    struct node *ptr; 
    ptr = (struct node *) malloc(sizeof(struct node)); 
    if (ptr == NULL) 
     return (struct node *) NULL; 
    else { 
     strcpy(ptr->name, name); 
     strcpy(ptr->age, age); 
     strcpy(ptr->dob, dob1); 
     ptr->next = NULL; 
     return ptr; 
    } 

} 

struct node *printnode(struct node *ptr) 
{ 
    printf("Name -> %s\n", ptr->name); 
    printf("age -> %s \n", ptr->age); 
    printf("dob ->%s\n", ptr->dob); 
    return ptr; 
} 

void add(struct node *newp) 
{ 
    struct node *temp = (struct node *) malloc(sizeof(struct node)); 
    if (head == NULL) 
     head = newp; 
    else { 
     for (temp = head; temp->next != NULL; temp = temp->next); 
     temp->next = newp; 
     temp = newp; 
    } 
    free(temp); 
} 

struct node *searchname(struct node *ptr, char *name1) 
{ 

    if (strcmp(name1, ptr->name) == 0) { 
     printf("\n name found \n"); 
     printf("\n details are -\n"); 
     printnode(ptr); 
     return ptr; 
    } else { 
     printf("\n name not found in the database \n"); 
    } 

} 

int main() 
{ 
    char name[25]; 
    char name1[25]; 
    char rep; 
    char age[10]; 
    char dob[10]; 
    int i; 
    int flag = 1; 
    struct node *ptr; 

    do { 
     fflush(stdin); 
     while (flag != 0) { 
      printf("Enter name -- "); 
      gets(name); 

      for (i = 0; name[i] != '\0'; i++) 
       if (isdigit(name[i])) { 
        printf("Error in user input, name should be in alphabets\n"); 
        flag = 1; 
        break; 
       } 

       else 
        flag = 0; 
     } 

     flag = 1; 

     while (flag != 0) { 
      printf("Enter age -- "); 
      scanf("%s", &age); 
      fflush(stdin); 

      for (i = 0; age[i] != '\0'; i++) 
       if (isalpha(age[i])) { 

        printf("Error in user input, age should be in numbers\n"); 
        flag = 1; 
        break; 
       } else { 
        flag = 0; 
       } 
     } 

     flag = 1; 
     while (flag != 0) { 
      printf("Enter dob in DD/MM/YY format-- "); 
      scanf("%s", &dob); 
      fflush(stdin); 

      for (i = 0; dob[i] != '\0'; i++) { 
       if (isalpha(dob[i])) { 
        printf("Error in user input, dob should be in numbers\n"); 
        flag = 1; 
        break; 
       } else 
        flag = 0; 

      } 

     } 

     flag = 1; 

     ptr = initnode(name, age, dob); 
     add(ptr); 

     printf("\n Do you want to continue?<Y/N>:\n "); 
     scanf("%s", &rep); 
     //rep = getchar(); 

    } 
    while (rep == 'Y' || rep == 'y'); 

    printf("\n do u want to search for a name in the database? <Y/N>:\n"); 
    scanf("%s", &rep); 

    if (rep == 'Y' || rep == 'y') { 
     printf("Enter name you want to search-- "); 
     scanf("%s", &name1); 

     ptr = searchname(head, name1); 
    } else { 
     printf("\n goodbye \n"); 
    } 

    do { 
     printf("\n do u want to search again? <Y/N>:\n"); 
     scanf("%s", &rep); 

     if (rep == 'Y' || rep == 'y') { 

      printf("Enter name you want to search-- "); 
      scanf("%s", &name1); 

      ptr = searchname(head, name1); 
     } else { 
      printf("\n goodbye \n"); 
     } 
    } 
    while (rep == 'Y' || rep == 'y'); 
    return 0; 

} 

問題は、それが最初のエントリのためだけではなく他の人を探しているということである私のコード - である、誰も私がこれを整理するのに役立つことができますか?私は "gcc"を通してコンパイルしています。

+1

は 'stdin'上fflush''を呼び出すことはありません - これは**未定義になりBehavio(u)r ** - 'fflush'のマニュアルページを参照してください。出力ストリームでのみ使用してください。 –

+2

コードを再フォーマットしてください。 – Rozuur

+0

この宿題はありますか?それがそうであるなら、そのようにそれをタグ付けするべきです。 –

答えて

1

検索機能は、リストの先頭である1つの要素のみを比較しています。

1つの要素を比較した後、次の要素に移動する必要があります。これは、再帰やwhileで行うことができ、次のいずれか

再帰的使用:

struct node *searchname(struct node *ptr, char *name1) 
{ 
if (ptr==NULL) //Reached end of the list 
{ 
    printf("\n name not found in the database \n"); 
    return NULL; 
} 
    if (strcmp(name1, ptr->name) == 0) { //found the element 
     printf("\n name found \n"); 
     printf("\n details are -\n"); 
     printnode(ptr); 
     return ptr; 
    } else { //search next element 
     return searchname(ptr->next,name1); //this will call your function again for the next element on the list 
    } 
} 

使用中:

struct node *searchname(struct node *ptr, char *name1) 
{ 
struct node *aux; // usually i use an auxiliary pointer in order to avoid unwanted overwrite at my pointer. 
aux = ptr; 
while (aux!=NULL) 
{ 
    if (strcmp(name1, aux->name) == 0) { //found the element 
     printf("\n name found \n"); 
     printf("\n details are -\n"); 
     printnode(aux); 
     return aux; 
    } 
    else { //move pointer to next element 
    aux=aux->next; 
    } 
} 
//if it reaches this point, the element was not found 
printf("\n name not found in the database \n"); 
return NULL; 
} 
+0

私もあなたの機能を使用している場合は、また、それは名だけを検索しています。最初のもの以外のエントリには「名前が見つかりません」というメッセージが表示されています。 – kamakshi

+0

おかげさまで、コードは正常に動作しています。現在、すべての名前を検索しています:) – kamakshi

1

追加機能を確認してください。間違っています。精神的にステップを進めて、ポインターとそれが指しているメモリーに何が起こるかを確認してください( )。

add関数で使用されるmallocとfreeは何ですか?

+0

Sryが解決できません。 – kamakshi

+0

@kamakshi:あなたの想像力を使うことができないなら、これはあなたが好きなデバッガをプログラムを一歩進んでください。 – misha

+0

mallocは一時ポインタ用にメモリを割り当てるために使用されますが、正しくありませんか?私が間違っているなら、私を案内してください。 – kamakshi

関連する問題