[英]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.