簡體   English   中英

C中的字符比較

[英]Char comparison in 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);
}

如果在列表中插入新節點,請不要立即銷毀它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM