gpt4 book ai didi

c - 如何将链表传递给C中的函数

转载 作者:行者123 更新时间:2023-11-30 19:09:06 26 4
gpt4 key购买 nike

所以我制作了一个工作程序,用于从文件中获取数据并根据您想要在 main 中的文件的哪一部分打印出文件。但我的目标是通过创建用户界面并附加到文件中的链接列表或根据要求打印出该链接列表的一部分(如果有)来使此模块化。只有一个问题:我似乎无法找到一种方法来成功地将链表传递给函数,以便当您在函数(Append)中创建新节点时,它也将在 main 中工作,然后在 (打印)。

这是工作代码:

#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#define TSIZE 100 /* size of array to hold title */
struct book {
char title[TSIZE], author[TSIZE],year[6];
struct book * next; /* points to next struct in list */
};
//typedef struct book ITEM;
typedef struct book * Node; // struct book *
void Append(Node *List, Node *Lcurrent,char filename[]);
void ClearGarbage(void);
int main(void){
Node head=NULL;
Node current;
current=head;
char fname[]="HW15Data.txt";
char op;
do{
puts("Select operation from the following list:");
puts("a. Append p. Print q. quit");
op = getchar();
ClearGarbage();
if (op=='a'){
/* Gather and store information */
Append(&head,&current,fname);
}
else if (op=='q'){
/* User quit, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
else{
/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Invalid characted entered. Bye!\n");
return 0;
}
} while (op!='q');
return 0;
}
void Append(Node *List, Node * Lcurrent,char filename[TSIZE]){
FILE * fp;
Node head=*List;
Node current=*Lcurrent;
int loops=0;
fp=fopen(filename,"r");
if (head==NULL){
head=(Node) malloc(sizeof(struct book));
current=head;
current->next=NULL;
}
do{
current->next = (Node) malloc(sizeof(struct book));
current=current->next;
loops++;
} while(fgets(current->title,sizeof(current->title),fp) && fgets(current->author,sizeof(current->title),fp) && fgets(current->year,sizeof(current->year),fp));;
free(current);
printf("Number of records written: %d\n",loops);
//Same as Print function in the nonworking code
int num;
int i=0;
if (head == NULL){
printf("No data entered. ");
}
else{
printf("Enter record # to print: ");
scanf("%d",&num);
ClearGarbage();
num=num+1;
current = head;
while (current != NULL && i<num)
{
for (i=0;i<num;i++)
current = current->next;
printf("Book: %sAuthor: %sYear: %s\n",
current->title, current->author, current->year);
}
}
}
void ClearGarbage(void){
while (getchar()!='\n');
}

好吧,这很酷,但我的猜测是,一旦 Append 完成,Append 中创建的节点在 main 中就没用了,因为它们现在已经消失了。因此,当我尝试在以下代码中创建打印函数时,没有任何内容可打印。

#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#define TSIZE 100 /* size of array to hold title */
struct book {
char title[TSIZE], author[TSIZE],year[6];
struct book * next; /* points to next struct in list */
};
//typedef struct book ITEM;
typedef struct book * Node; // struct book *
void Append(Node *List, Node *Lcurrent,char filename[]);
int Print(Node *List,Node *Lcurrent);
void ClearGarbage(void);
int main(void){
Node head=NULL;
Node current;
current=head;
char fname[]="HW15Data.txt";
char op;
do{
puts("Select operation from the following list:");
puts("a. Append p. Print q. quit");
op = getchar();
ClearGarbage();
if (op=='a'){
/* Gather and store information */
Append(&head,&current,fname);
}
else if (op=='p'){
/*Print book record of user's choice*/
Print(&head,&current);
}
else if (op=='q'){
/* User quit, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
else{
/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Invalid characted entered. Bye!\n");
return 0;
}
} while (op!='q');
return 0;
}
void Append(Node *List, Node * Lcurrent,char filename[TSIZE]){
FILE * fp;
Node head=*List;
Node current=*Lcurrent;
int loops=0;
fp=fopen(filename,"r");
if (head==NULL){
head=(Node) malloc(sizeof(struct book));
current=head;
current->next=NULL;
}
do{
current->next = (Node) malloc(sizeof(struct book));
current=current->next;
loops++;
} while(fgets(current->title,sizeof(current->title),fp) && fgets(current->author,sizeof(current->title),fp) && fgets(current->year,sizeof(current->year),fp));
free(current);
printf("Number of records written: %d\n",loops);
}
int Print(Node *List,Node *Lcurrent){
int num;
int i=0;
Node head=*List;
Node current=*Lcurrent;
if (head == NULL){
printf("No data entered.\n");
return -1;
}
printf("Enter record # to print: ");
scanf("%d",&num);
ClearGarbage();
num=num+1;
current = head;
while (current != NULL && i<num)
{
for (i=0;i<num;i++){
current = current->next;
}
printf("Book: %sAuthor: %sYear: %s\n",
current->title, current->author, current->year);
}
return 0;
}
void ClearGarbage(void){
while (getchar()!='\n');
}

感谢任何人的帮助!

编辑:为了清晰起见,删除了未使用的 typedef

最佳答案

似乎大多数人都关注缺乏组织(这也让我很困扰),而不是你的实际问题。

问题的根源似乎是您分配变量头的位置。当您定义“Node head = *List”并且 List 为 NULL 时,因为它是第一次初始化的,它会失去与您从 main 发送的原始列表的连接,并且您只需创建一个仅具有本地引用的链接列表。

我刚刚将“追加”和“打印”功能中“head”的使用更改为“*List”,似乎已经解决了。

这是我更改后的代码:

#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#define TSIZE 100 /* size of array to hold title */
struct book {
char title[TSIZE], author[TSIZE], year[6];
struct book * next; /* points to next struct in list */
};
//typedef struct book ITEM;
typedef struct book * Node; // struct book *
void Append(Node *List, Node *Lcurrent, char filename[]);
int Print(Node *List, Node *Lcurrent);
void ClearGarbage(void);
int main(void) {
Node head = NULL;
Node current;
current = head;
char fname[] = "HW15Data.txt";
char op;
do {
puts("Select operation from the following list:");
puts("a. Append p. Print q. quit");
op = getchar();
ClearGarbage();
if (op == 'a') {
/* Gather and store information */
Append(&head, &current, fname);
}
else if (op == 'p') {
/*Print book record of user's choice*/
Print(&head, &current);
}
else if (op == 'q') {
/* User quit, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
else {
/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Invalid characted entered. Bye!\n");
return 0;
}
} while (op != 'q');
return 0;
}
void Append(Node *List, Node * Lcurrent, char filename[TSIZE]) {
FILE * fp;
Node head = *List;
Node current = *Lcurrent;
int loops = 0;
char line[256];
fp = fopen(filename, "r");
if (*List == NULL) {
*List = (Node)malloc(sizeof(struct book));
current = *List;
current->next = NULL;
}
do {
current->next = (Node)malloc(sizeof(struct book));
current = current->next;
loops++;
} while (fgets(current->title, sizeof(line), fp) && fgets(current->author, sizeof(line), fp) && fgets(current->year, sizeof(line), fp));
free(current);
printf("Number of records written: %d\n", loops);
}
int Print(Node *List, Node *Lcurrent) {
int num;
int i = 0;
Node head = *List;
Node current = *Lcurrent;
if (*List == NULL) {
printf("No data entered.\n");
return -1;
}
printf("Enter record # to print: ");
scanf("%d", &num);
ClearGarbage();
num = num + 1;
current = *List;
while (current != NULL && i<num)
{
for (i = 0; i<num; i++) {
current = current->next;
}
printf("Book: %sAuthor: %sYear: %s\n",
current->title, current->author, current->year);
}
return 0;
}
void ClearGarbage(void) {
while (getchar() != '\n');
}

仍然存在许多逻辑错误和一些错误,但它解决了您寻求帮助的问题。

关于c - 如何将链表传递给C中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768210/

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