gpt4 book ai didi

C程序排序链表

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:11 26 4
gpt4 key购买 nike

这个程序应该创建一个排序列表并按名字和姓氏对每个用户进行排序。我似乎无法弄清楚如何正确地对名称进行排序。

我只有 append_to_list 函数有问题,其余函数都可以正常工作。

当我第一次开始输入名字时:

user ID:    Last Name:    First Name:
3 Alex Alex
2 Jones Alex
1 andrew john

在我输入一个应该在两个名字之间排序的名字之前,它排序很好当我输入名字 Andrew、Alex 时,会发生这种情况。

 user ID:    Last Name:    First Name:
4 Andrew Alex
3 Alex Alex
2 Jones Alex
1 andrew john

但是 Andrew,Alex 应该在用户 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);
}
}
}

最佳答案

问题在于您比较节点的方式:

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 Does 怎么办?)并在 prevcur 之间或 family< 之前插入 如果 prevNULL

for 循环看起来很难看:难以阅读,容易出错。编写一个单独的比较函数,它接受 2 个节点并根据电话簿顺序返回 -1, 0, +1。您将为 append_to_listdelete_from_list 使用此函数,从而编写更少的代码、更易读且更一致。

关于C程序排序链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com