gpt4 book ai didi

c - 我的链表中第一个节点中的数据不断变化到列表中的最后一个节点。 C语言

转载 作者:行者123 更新时间:2023-12-04 08:15:50 24 4
gpt4 key购买 nike

我正在从 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);
}

}

最佳答案

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

/* 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 */
关键是当您向列表中添加新项目时,新数据会替换之前的项目。它解释了为什么您只能看到最后的数据。

关于c - 我的链表中第一个节点中的数据不断变化到列表中的最后一个节点。 C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65708906/

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