该程序应该创建一个排序列表,并按名字和姓氏对每个用户进行排序。我似乎无法弄清楚如何正确地对名称进行排序。
我只有append_to_list函数的问题,其余的函数运行正常。
当我第一次开始输入名字时:
user ID: Last Name: First Name:
3 Alex Alex
2 Jones Alex
1 andrew john
它排序很好,直到我输入一个应在两个名称之间排序的名称 当我输入安德鲁的名字时,亚历克斯就会发生这种情况。
user ID: Last Name: First Name:
4 Andrew Alex
3 Alex Alex
2 Jones Alex
1 andrew john
但安德鲁,亚历克斯应该在用户2和3之间
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"
#include "readline.h"
// function append_to_list takes user input from user, id, name and puts it to the end of the list
// as well as sorts each name alphabetically
struct user *append_to_list(struct user *family)
{
struct user *cur, *prev, *new_node;
// generate memory
new_node = malloc(sizeof(struct user));
if (new_node == NULL) {
printf("malloc failed in append\n");
return family;
}
printf("Enter user ID: \n");
scanf("%d", &new_node->number);
printf("Enter user last name: \n");
read_line(new_node->last_name,NAME_LEN);
printf("Enter user first name: \n");
read_line(new_node->first_name,NAME_LEN);
for( cur=family; cur != NULL; cur = cur->next) {
if (new_node->number == cur->number) {
printf("user already exists: %s, %s\n",new_node->first_name, new_node->last_name);
free(new_node);
return family;
}
}
for (cur=family, prev = NULL; cur != NULL
&& (strcmp(new_node->first_name,cur->first_name) < 0)
&& (strcmp(new_node->last_name,cur->last_name) < 0);
prev = cur, cur = cur->next) {
if((strcmp(new_node->last_name,cur->last_name) < 0)) break;
if((strcmp(new_node->first_name,cur->first_name) == 0))
if((strcmp(new_node->first_name,cur->first_name) < 0)) break;
;
}
// use strcmp == 0 to see if name already exists
if (cur != NULL && (strcmp(new_node->first_name,cur->first_name) == 0)
&& (strcmp(new_node->last_name,cur->last_name)) == 0)
{
printf("user already exists: %s, %s\n",new_node->first_name, new_node->last_name);
free(new_node);
return family;
}
// append the linkedlist to the end
new_node->next = cur;
// check to see if the node is empty
if (prev == NULL) {
return new_node;
} else {
prev->next = new_node->next;
return family;
}
}
// function delete_from_list removes a user from the family
struct user* delete_from_list(struct user *family)
{
struct user *prev, *cur;
int id;
int not_found = 0;
printf("Enter user ID: \n");
scanf("%d", &id);
for (cur = family, prev = NULL; cur != NULL; prev = cur, cur = cur->next) {
if (id == cur->number) {
// if only one user on family
if (prev == NULL) {
family = cur->next;
// if user is in the middle of family
// connects prev node to cur node
} else {
prev->next = cur->next;
}
printf("user deleted: %s ,%s\n",cur->first_name, cur->last_name);
free(cur);
}
else
not_found = 1;
}
if (not_found == 1) {
printf("user not found\n");
}
return family;
}
// function find_user searches the family by ID and matches it with the users name
void find_user(struct user *family)
{
struct user *p = family;
int id;
int count = 0;
printf("Enter user ID: \n");
scanf("%d", &id);
// compares the family with the user entered ID
// if the ID is the same count set to 1
if (p != NULL) {
for (p = family; p != NULL; p = p->next) {
if (id == p->number) {
count = 1;
break;
}
}
}
// if count is 1 we know the function found that specific user
if ( count == 1) {
printf("user found: %s, %s\n", p->last_name, p->first_name);
} else {
printf("user not found");
}
}
// function printList prints the entire family
void printList(struct user *family)
{
struct user *p;
printf("user ID:\tLast Name:\tFirst Name:\n");
for (p = family; p != NULL; p = p->next) {
printf("%d\t\t%s\t\t%s\n", p->number, p->last_name, p->first_name);
}
}
// function clearList clears the entired linked list
void clearList(struct user *family)
{
struct user *p;
while (family != NULL) {
p = family;
family = family->next;
if (p != NULL) {
free(p);
}
}
}
答案 0 :(得分:1)
问题在于比较节点:
for (cur=family, prev = NULL; cur != NULL
&& (strcmp(new_node->first_name,cur->first_name) < 0)
&& (strcmp(new_node->last_name,cur->last_name) < 0);
prev = cur, cur = cur->next) { ... }
您应该跳过具有较小系列或(相同姓氏和较小名字)的节点。以这种方式修复比较:
for (cur=family, prev = NULL;
cur != NULL
&& ((strcmp(new_node->first_name, cur->first_name) < 0)
|| ((strcmp(new_node->first_name, cur->first_name) == 0)
&& (strcmp(new_node->last_name,cur->last_name) < 0)));
prev = cur, cur = cur->next) { ... }
简化后续代码。你实际上不需要任何代码。循环将在插入点停止。只检查是否存在相同的姓氏和名字(但如果有2个John会怎么样?)并在prev
和cur
之间插入,或者在family
之前插入prev
是NULL
。
for
循环看起来很难看:难以阅读,容易出错。编写一个单独的比较函数,它接受2个节点并根据电话簿顺序返回-1, 0, +1
。您将对append_to_list
和delete_from_list
使用此函数,编写更少的代码,更具可读性和更一致性。
答案 1 :(得分:0)
我认为重叠比较运算符是一个更好的主意,然后用一些存在的算法对你的列表进行排序。
答案 2 :(得分:0)
此循环中的条件有问题:
for (cur=family, prev = NULL;
cur != NULL && (strcmp(new_node->first_name,cur->first_name) < 0)
&& (strcmp(new_node->last_name,cur->last_name) < 0);
prev = cur, cur = cur->next)
循环不会执行:
(cur=family, family exist)
cur != NULL -> True
(Alex == Alex)
((strcmp(new_node->first_name,cur->first_name) -> 0) < 0 -> False
(Andrew > Alex)
((strcmp(new_node->last_name,cur->last_name) -> 1) < 0 -> False
True && False && False -> False
因此,您将在开头添加新记录。
此外,在循环中你有一个不好的&#39;如果&#39;代码:
if((strcmp(new_node->first_name,cur->first_name) == 0))
if((strcmp(new_node->first_name,cur->first_name) < 0)) break;
;
提示:删除有问题的无用代码。 振作!
答案 3 :(得分:0)
for循环将在插入点处停止(在上一个节点与cur节点之间)。
即使new_node大于最后一个节点,下一部分也会对列表进行排序。 如果新节点大于最后一个节点,则cur变为null,// //分配给new_node_> next。因此,prev等于cur;因此,prev-> next = new_node;
for (cur=family, prev = NULL; cur!=NULL && ((strcmp(new_node->first_name,
cur->first_name)>0) || ((strcmp(new_node->first_name,
cur->first_name)==0)&&(strcmp(new_node->last_name,
cur->last_name)>0))); prev = cur, cur = cur->next);
new_node->next = cur;
if(prev==NULL)
return new_node;
else {
prev->next = new_node;
return family;
}