簡體   English   中英

C程序排序鏈表

[英]C Program Sorted Linked List

該程序應該創建一個排序列表並按名字和姓氏對每個用戶進行排序。 我似乎無法弄清楚如何正確排序名稱。

我只有 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);
        }
    }
}

問題在於您比較節點的方式:

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 個約翰呢?)並在prevcur之間插入,或者如果prevNULL則在family之前插入。

for循環看起來很難看:難以閱讀,容易出錯。 編寫一個單獨的比較函數,該函數采用 2 個節點,並根據電話簿順序返回-1, 0, +1 您將使用此函數append_to_listdelete_from_list ,編寫更少的代碼,更具可讀性和更一致。

我認為重疊比較運算符是一個更好的主意,然后用一些現有算法對您的列表進行排序。

您在此循環中遇到條件問題:

 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;
;

博士。 鏈表

提示:刪除有問題的無用代碼。 歡呼!

for 循環將在插入點(前一個節點和當前節點之間)處停止。

即使 new_node 大於最后一個節點,下一部分也會對列表進行排序。 如果新節點大於最后一個節點,則cur變為空,即 //分配給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;
           }

暫無
暫無

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

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