2016-05-08 14 views
0

私はユーザーがユニークな単位文字を入力した場合、リンクされたリストにユニットを追加するこの機能を実行しようとしています。私のコードはコンパイルされます。しかし、最初の試合の後に、それはおそらくまだないはずだとしても、「ユニットレターは既に存在している」としか言いません。誰かが私が間違っていた場所だけ教えてもらえますか?どんな助けもありがとう!Cでの文字比較

void addU(NODE** head){ 
    NODE *newNode, *temphead, *current; 
    int check = 0; 
    char ul, nl; 
    newNode = (NODE*)malloc(sizeof(NODE)); 
    newNode->unit = malloc(sizeof(UNIT)); 

    /*Get unit details*/ 
    printf("\n\n\tUnit Letter: "); 
    scanf(" %c", &(newNode->unit->letter)); 
    printf("\n\tMaximum number of occupants: "); 
    scanf("%d", &(newNode->unit->max)); 

    /*Check if unit letter is unique*/ 
    temphead = current = *head; 
    if (temphead!=NULL){ 
     while (current!=NULL){ 
      ul = tolower(current->unit->letter); //convert to small case for comparison 
      nl = tolower(newNode->unit->letter); 
      if (ul == nl){ 
       check=1; 
       printf("\n\n\tInvalid: Unit letter already exists.\n"); 
       break; 
      }else current = current->next; 
    }} 

    /*Add newNode to list if unit letter is unique*/ 
    if (check==0){ 
     if (*head==NULL){ 
      newNode->next = NULL; 
      *head = newNode; 
     }else{ 
      newNode->next = *head; 
      *head = newNode; 
     } 
     printf("\n\n\tUnit successfully added!\n"); 
    } 
    free(newNode); 
    free(newNode->unit); 
} 
+0

temphead = current = * head;これは有効ではありません – Striker

+0

1)マクロと_enum-constants_以外のものには大文字の名前を使用しないでください。 2)mallocとfriendsの結果をC言語でキャストしないでください(奇妙なことに、あなたはそれらを不自然にキャストします)。 3)デバッガの使い方を学ぶ時間。 – Olaf

答えて

-1

新しいノードをリストに挿入した場合は、すぐに破棄しないでください。