gpt4 book ai didi

c - 库为空然后段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:52 25 4
gpt4 key购买 nike

大家好,我正在做一个项目,我不断收到“库是空的”,然后是段错误,用 gdb 运行它告诉我错误出在我的 count_list 函数中。但我似乎无法弄清楚为什么?任何提示将不胜感激它让我如此接近完成并挂断了这个。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Assume max char count in file */
#define MAX 20

/* library struct - DO NOT CHANGE */
typedef struct library
{
char *genre;
char *band;
char *album;
float rating;
struct library *next;
}Library;

/* Function Prototypes - DO NOT CHANGE */
Library *Read_File(FILE *);
void Print(Library *);
Library *Create_List(Library *, Library *);
Library *Create_Album(char *, char *, char *, float);
Library *Merge(Library *, Library *);
int Count_List(Library *);
void Split_List(Library *, Library **, Library **);
Library *Merge_Sort(Library *);
Library *Delete_Genre(Library *);
void Free_Entry(Library *);
Library *Clean(Library *);

/* MAIN
* Error check file parameter
* Call Read_File to fill our list
* Print our list
* Merge Sort the linked list (by genre)
* Delete a genre
* Free the list
*/
int
main(int argc, char **argv)
{

if(argc != 2)
{
printf("Not enough arguments.\n");
return 0;
}

FILE *fp = NULL;

if((fp = fopen(argv[1], "r")) == NULL)
{
printf("File can not be opened.\n");
return 0;
}

Library *Head = NULL;
Head = Read_File(fp);
Print(Head);

Merge_Sort(Head);
Print(Head);

Head = Delete_Genre(Head);
Print(Head);

Head = Clean(Head);
Print(Head);

return 0;
}
/* Clean()
* Delete the linked list, recursively
*/
Library *
Clean(Library *Head)
{
if(Head) return NULL;

Library *Tmp = Head->next;
Free_Entry(Head);
Clean(Tmp->next);
}

/* Free_Entry()
* wrapper function to free a struct Library
*/
void
Free_Entry(Library *Entry)
{
free(Entry);
}

/* Delete_Genre()
* Deletes a genre inputted by user
* Logic:
* prompt user for genre input
* traverse list deleting all structs that contain the genre
*/
Library *
Delete_Genre(Library *Head)
{
if(!Head)
{
printf("List Empty.\n");
return NULL;
}

char *input = malloc(MAX * sizeof(char *));
Library *Current = Head;
Library *Tail = NULL;

printf("Which genre would you like to delete?\n");

scanf("%s", input);

while(Current)
{
if(strcmp(Current->genre, input))
{
if(Current = Head)
{
Head = Head->next;
Free_Entry(Current);
Current = Head;
}
else
Tail->next = Current->next;

}
else
Current = Current->next;
}
}

/* Read_File()
* Open file fp
* Create a struct from information in text file
* Link our list with newly created struct
*/
Library *
Read_File(FILE *fp)
{
Library *Head, *Entry;
Head = Entry = NULL;

char *genre, *band, *album;
float rating;

while(1)
{
fscanf(fp, "%s %s %s %f", &genre, &band, &album, &rating);
if(!feof(fp))
break;
Entry = Create_Album(genre, band, album, rating);
Head = Create_List(Entry, Head);
}

return Head;
}

/* Print()
* Print the linked list
*/
void
Print(Library *Head)
{
if(!Head)
{
printf("Library is empty.\n");
return;
}

while(Head)
{
printf("%20s %20s %20s %20.2f \n",
Head->genre, Head->band, Head->album, Head->rating);
Head = Head->next;
}
printf("\n\n");
//return Head;
}

/* Create_Album
* Create a struct and assign the given args to it as appropriate
*/
Library *
Create_Album(char *genre, char *band, char *album, float rating)
{
Library *Entry = malloc(sizeof(Library));
strcpy(Entry->genre, genre);
strcpy(Entry->band, band);
strcpy(Entry->album, album);
Entry->rating = rating;
Entry->next = NULL;

return Entry;
}

/* Create_List()
* Push Entry onto our List
*/
Library *
Create_List(Library *Head, Library *Entry)
{
if(!Head)
return Entry;

Entry->next = Head;
return Entry;
}
/* Merge_Sort()
* Recursively split our list between Left and Right
* Merge our Left and Right lists
*/
Library *
Merge_Sort(Library *Head)
{
Library *Tmp = Head;
Library *Left, *Right, *Result;
Left = Right = Result = NULL;

int count = Count_List(Head);

if(count = 1)
return Tmp;

Left = Merge_Sort(Left);
Right = Merge_Sort(Right);

Result = Merge(Left, Right);

return Result;
}
/* Split_List()
* split our list in half
*/
void
Split_List(Library *Head, Library **Left, Library **Right)
{
int size = Count_List(Head);
int i;
Library *Tmp = Head;

*Left = Head;

for(i=1; i<size/2; ++i)
Tmp=Tmp->next;

*Right = Tmp->next;
Tmp->next = NULL;
}

/* Merge()
* Merge two linked lists Left and Right together in sorted order
*/
Library *
Merge(Library *Left, Library *Right)
{
Library *Result, *Tmp;
Result = Tmp = NULL;

if(strcmp(Left->genre, Right->genre) <= 0)
{
Result = Left;
Left = Left->next;
}
else
{
Result = Right;
Right = Right->next;
}
Tmp = Result;

while(Left != NULL && Right != NULL)
{
if(Left != NULL && Right!= NULL)
{
if (strcmp(Left->genre, Right->genre) <= 0)
{
Tmp->next = Left;
Left = Left->next;
}
else
{
Tmp->next = Right;
Right = Right->next;
}
Tmp = Tmp->next;
}
}
return Result;
}
/* Count_List()
* Count the number of elements in a linked list
*/
int
Count_List(Library *Head)
{
Library *Tmp = Head;
int count = 0;

while(Tmp->next != NULL)
{
count++;
Tmp = Tmp->next;
}

}

最佳答案

观察,

Count_List() 应该对列表中的每个元素进行计数,您的版本只从列表中的第二个项目开始计数。它应该总是返回一个 int,

int
Count_List(Library *Head)
{
int count = 0;
Library *Tmp = Head;
if(!Tmp) return(count);

while(Tmp != NULL)
{
count++;
Tmp = Tmp->next;
}
return(count);
}

在 Read_File() 中调用 Create_List(Entry,Head),但函数签名为 Create_List(Head,Entry);你想要哪个?可能是(头,条目)。使用 fgets 和 sscanf 读取文件,

Library *
Read_File(FILE *fp)
{
Library *Head, *Entry;
Head = Entry = NULL;
char genre[100], band[100], album[100];
float rating;
char line[100];

while(fgets(line,sizeof(line),fp)
{
sscanf(line,"%s %s %s %f", genre, band, album, &rating);
Entry = Create_Album(genre, band, album, rating);
Head = Create_List(Head,Entry);
}
return Head;
}

查看 Create_List(),您似乎将 List 实现为堆栈(将 Entry 插入列表头部),

Library *
Create_List(Library *Head, Library *Entry)
{
if(!Head)
return Entry;

Entry->next = Head;
return Entry;
}

Create_Album() 在为成员变量赋值之前需要检查 malloc 是否成功,

    Library *Entry=NULL;
if( !(Entry=(Library*)malloc(sizeof(Library))) )
{
printf("error creating album\n");fflush(stdout);
return Entry;
}

说到 Library struct,你将成员 genre,band,album 声明为指针,但你需要空间来复制内存,例如,

typedef struct library
{
char genre[50];
char band[50];
char album[50];
float rating;
struct library *next;
}Library;

我的建议是构建构造函数和析构函数 LibraryNew、LibraryDel。

检查 (argc<2) 个参数(您的消息说不够,而不是需要 2 个,

    if(argc < 2)
{
printf("Not enough arguments.\n");
return 0;
}

这解决了最大的问题,

./library music.x 2
line:Rock Antrax Party 1.2

Rock,Antrax,Party,1.200000
Rock,Antrax,Party,1.200000
added: Rock,Antrax,Party,1.200000
Rock Antrax Party 1.20
Rock Antrax Party 1.20
Which genre would you like to delete?

关于c - 库为空然后段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394223/

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