问题描述
我正在从 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,"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 */
重点是,当您向列表中添加新项目时,新数据会替换之前的项目。它解释了为什么您只能看到最后的数据。
,这是针对您的问题的简化解决方案,
/* 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,stdin);
printf("%27s: ","Enter customer's first name");
fgets(currenta->firstname,"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;
while (currenta != NULL){
printf("%d %s %s %f\n",currenta->number,currenta->lastname,currenta->firstname,currenta->balance);
currenta=currenta->next;
}
}
}