- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 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", ¤ta->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/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!