【问题标题】:C Program Sorted Linked ListC程序排序链表
【发布时间】:2015-11-24 21:59:25
【问题描述】:

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

我只有 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

但是安德鲁,亚历克斯应该在用户 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);
        }
    }
}

【问题讨论】:

  • 为什么在循环终止条件和append_to_list()的循环体中都有名称比较?
  • 无论如何,我认为终止条件和循环体中的比较 both 都是错误的。至少,相同姓氏的情况似乎在两个地方都处理不正确。

标签: c sorting linked-list strcmp


【解决方案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 Does?)并在 prevcur 之间插入,或者如果 prevNULL 则在 family 之前插入。

for 循环看起来很丑:难以阅读,容易出错。写一个单独的比较函数,取2个节点,按照电话簿顺序返回-1, 0, +1。您将对append_to_listdelete_from_list 使用此函数,编写更少的代码,更具可读性和一致性。

【讨论】:

  • 您提到的比较函数在某些语言中有时称为“宇宙飞船”运算符。
  • @ChuckCottrill:宇宙飞船运算符来自 Perl。我不确定我是否后悔没有在 C 中使用它...&lt;=&gt; 在 C 中作为 2 个左值之间的交换运算符会更有用。
  • 是的,perl(数字)、ruby (stackoverflow.com/questions/26581619/…)、php、python(拼写 cmp)和 ocaml(拼写比较)。
  • 交换运算符也不错。
  • @ChuckCottrill:perl 早于所有这些,但如果 caml 有这个运算符,它可能除外。我不是 perl 的拥护者:如果 perl 不存在,就不应该发明它。交换运算符对于 C 和 C 的精神来说会很好,因为它通常可以作为机器指令使用,并且作为函数实现非常麻烦。
【解决方案2】:

我认为重叠比较运算符是一个更好的主意,然后使用一些现有算法对列表进行排序。

【讨论】:

    【解决方案3】:

    您在此循环中遇到了条件问题:

     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
    

    因此,您将在开头添加新记录。

    此外,在循环中,您有一个错误的“if”代码:

    if((strcmp(new_node->first_name,cur->first_name) == 0)) 
    
    if((strcmp(new_node->first_name,cur->first_name) < 0)) break;
    ;
    

    Doc. strcmp

    提示:删除有问题的无用代码。 加油!

    【讨论】:

      【解决方案4】:

      for 循环将在插入点(前一个节点和当前节点之间)停止。

      即使 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;
                 }
      

      【讨论】:

        猜你喜欢
        • 2011-08-07
        • 2012-07-19
        • 2016-08-26
        • 2016-02-21
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多