【问题标题】:The data in the first node in my linked list keeps changing to the last node in the list. C language我的链表中第一个节点中的数据不断更改为列表中的最后一个节点。 C语言
【发布时间】:2021-04-18 21:03:48
【问题描述】:

我正在从 All In One C Desk Reference For Dummies 中了解 C 链表。

每当我运行代码时,我似乎可以添加新元素,但是当我显示内容时,只显示最后一个节点中的数据。

我已经检查并重新检查了代码,但据我所知,我写的和书一样。感谢您的帮助!

由于我不知道哪一部分是错误的,所以我提供了书中的完整示例,除了为我自己的代码替换 fflush() 函数。练习 6.2.1(第 6 章 - 第 2 章 - 银行计划 - bank.c(如果有帮助))

/* Includes */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"../../myheaders/myinput.h"

/* Function Protoyypes */
void addNewAccount(void);
void listAll(void);

/* Structures and global variables  */
struct account {
    int number;
    char lastname[15];
    char firstname[15];
    float balance;
    struct account *next;
};
struct account *firsta, *currenta, *newa;
int anum = 0;

/* Main program */
int main()
{
    char ch;
    firsta = NULL;

    do {
        puts("\nA - Add a new account");
        puts("L - List Accounts");
        puts("Q - Quit this program\n");
        printf("\tYour choice:");
        ch = getonlychar(&ch);
        ch = toupper(ch);
        switch (ch) {
            case 'A':
                puts("Add new account\n");
                addNewAccount();
                break;
            case 'L':
                puts("Listing accounts");
                listAll();
                break;
            case 'Q':
                puts("Quit\n");

            default:
                break;
        }
    } while (ch!= 'Q');

    return 0;
}

/* Definitions */
/* Add a new account */
void addNewAccount(void)
{
    newa = (struct account *)malloc(sizeof(struct account));
    
    /* Check to see if this is the first record.
     * If so, then initialize all the pointers to this
     * first structure in the database */
    if (firsta == NULL) {
        firsta = currenta = newa;
    }

    /* Otherwise, you must find the end of the structure list
     * easily spotted by the NULL pointer) and add on the
     * new structure you just allocated memorey for */
    else {
        currenta = firsta; /* Make first record current */
    }

    /* Loop through all records */
    while (currenta->next != NULL) {
        currenta = currenta->next;

        /* The last reord is found */
        currenta->next = newa; /* Save the address of new */
        currenta = newa; /* Make current record new */
    }

    /* Now you just fill in the new structure */
    anum++;
    printf("%27s: %5i\n", "Account number", anum);
    currenta->number = anum;
    printf("%27s: ", "Enter customer's last name");
    fgets(currenta->lastname, 14, stdin);
    printf("%27s: ", "Enter customer's first name");
    fgets(currenta->firstname, 14, stdin);
    printf("%27s: ", "Enter account balance");
    scanf(" %f", &currenta->balance);

    /* Finally, cap the new record with a NULL pointer
     * so that you know it's the last record */
    currenta->next = NULL;
}

void listAll(void)
{
    if (firsta == NULL) {
        printf("No records to show!\n");
    }
    else {
        printf("%6s %-15s %-15s %11s\n", "Acct#", "Last", "First", "Balance");
        currenta = firsta;
        do {
            printf("%6s %-15s %-15s %11s\n",
                    currenta->number,
                    currenta->lastname,
                    currenta->firstname,
                    currenta->balance);
        } while ((currenta = currenta->next) != NULL);
    }
    
}

【问题讨论】:

    标签: c struct linked-list singly-linked-list function-definition


    【解决方案1】:

    这是您问题的简化解决方案,

    /* Includes */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    /* Function Protoyypes */
    void addNewAccount(void);
    void listAll(void);
    
    /* Structures and global variables  */
    struct account {
        int number;
        char lastname[15];
        char firstname[15];
        float balance;
        struct account *next;
    };
    struct account *firsta, *currenta, *newa;
    int anum = 0;
    
    
    
    /* Main program */
    int main()
    {
        char ch;
        firsta = NULL;
        while(ch!='Q'){
        
         puts("\nA - Add a new account");
            puts("L - List Accounts");
            puts("Q - Quit this program\n");
            printf("\tYour choice:");
            
            scanf(" %c", &ch);  
            
            ch = toupper(ch);
            switch (ch) {
                case 'A':
                    puts("Add new account\n");
                    addNewAccount();
                    break;
                case 'L':
                    puts("Listing accounts");
                    listAll();
                    break;
                case 'Q':
                    puts("Quit\n");
    
                default:
                    break;
            }
    }
        return 0;
    }
    
    
    /* Definitions */
    /* Add a new account */
    void addNewAccount(void)
    {
        newa = (struct account *)malloc(sizeof(struct account));
        /* Check to see if this is the first record.
         * If so, then initialize all the pointers to this
         * first structure in the database */
        if (firsta == NULL) {
            firsta = currenta = newa;
        }
    
        /* Otherwise, you must find the end of the structure list
         * easily spotted by the NULL pointer) and add on the
         * new structure you just allocated memorey for */
        else {
                
                currenta->next = newa; 
                currenta = currenta->next;
        }
    
    
        /* Now you just fill in the new structure */
        anum++;
        printf("%27s: %5i\n", "Account number", anum);
        currenta->number = anum;
        printf("%27s: ", "Enter customer's last name");
        fgets(currenta->lastname, 14, stdin);
        fgets(currenta->lastname, 14, stdin);
        printf("%27s: ", "Enter customer's first name");
        fgets(currenta->firstname, 14, stdin);
        printf("%27s: ", "Enter account balance");
        scanf(" %f", &currenta->balance);
    
        /* Finally, cap the new record with a NULL pointer
         * so that you know it's the last record */
        currenta->next = NULL;
    }
    
    void listAll(void)
    {
        if (firsta == NULL) {
            printf("No records to show!\n");
        }
        else {
            printf("%6s %-15s %-15s %11s\n", "Acct#", "Last", "First", "Balance");
            currenta = firsta;
            
    
            
            while (currenta != NULL){
                printf("%d %s %s %f\n",
                        currenta->number,
                        currenta->lastname,
                        currenta->firstname,
                        currenta->balance);
                currenta=currenta->next;
            }
            
    
            
    
        }
        
    }
    

    【讨论】:

    • 我试了一下。信息添加正常,但显示信息时出现分段错误。也许我也需要在其他地方进行更改?感谢您的帮助!
    • 实际上,它适用于我的 devC++ ide。您使用哪个版本的 C 进行编译?你确定你使用的是我上面分享的相同的列表方法吗?
    • 我复制并粘贴了它,只是为了确定。这一次,信息添加正常,然后它列出了所有内容,没有问题,但是当我运行 addNewAccount() 函数时出现 Seg Fault。我使用命令行 GCC。键入:gcc --version yield:gcc (Debian 8.3.0-6) 8.3.0 版权所有 (C) 2018 Free Software Foundation, Inc. 这是免费软件;查看复制条件的来源。没有保修;甚至不是为了适销性或适合特定目的。
    • 好吧,你应该开始针对这些情况逐步学习调试。我认为您的主要功能有问题。我已经更新了上面的内容,你可以试试。
    【解决方案2】:

    我认为您的问题来自您的这部分代码:

    /* Loop through all records */
    while (currenta->next != NULL) {
        currenta = currenta->next;
    
        /* The last reord is found */
        currenta->next = newa; /* Save the address of new */
        currenta = newa; /* Make current record new */
    }
    

    也许你想要那个:

    /* Loop through all records */
    while (currenta->next != NULL) {
        currenta = currenta->next;
    }
    
    /* The last reord is found */
    currenta->next = newa; /* Save the address of new */
    currenta = newa; /* Make current record new */
    

    重点是,当您向列表中添加新项目时,新数据会替换之前的项目。它解释了为什么您只看到最后的数据。

    【讨论】:

    • 我刚试过,但出现分段错误
    • 感谢您的帮助。也许代码中还有其他需要更改的地方。当我选择 ListAll 时发生了 seg 错误
    猜你喜欢
    • 2013-07-26
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多