C 具有双链表的歌曲名称数据库

C 具有双链表的歌曲名称数据库,c,linked-list,doubly-linked-list,C,Linked List,Doubly Linked List,我是这个网站的新手,我正在寻找一些关于双链接列表的帮助,该列表可以存储带有艺人姓名、歌曲名、专辑名、日期和运行时间的Mp3。如果你能帮我,我会很感激的。根据我的add函数中的GDB,我得到了一个segfault 警告如下: Description Resource Path Location Type 'data' is used uninitialized in this function [-Wuninitialized] Mp3.c /OperatingSystems

我是这个网站的新手,我正在寻找一些关于双链接列表的帮助,该列表可以存储带有艺人姓名、歌曲名、专辑名、日期和运行时间的Mp3。如果你能帮我,我会很感激的。根据我的add函数中的GDB,我得到了一个segfault

警告如下:

Description Resource    Path    Location    Type
'data' is used uninitialized in this function [-Wuninitialized] Mp3.c   /OperatingSystemsLab1   
line 7  C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default]   Mp3.c   /OperatingSystemsLab1   
line 7  C/C++ Problem
implicit declaration of function 'malloc' [-Wimplicit-function-declaration] Mp3.c   /OperatingSystemsLab1   
line 21 C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default]   Mp3.c   /OperatingSystemsLab1   
line 8  C/C++ Problem
implicit declaration of function 'free' [-Wimplicit-function-declaration]   Mp3.c   /OperatingSystemsLab1   
line 40 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default]    Mp3.c   /OperatingSystemsLab1   
line 21 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default]    Mp3.c   /OperatingSystemsLab1   
line 57 C/C++ Problem
incompatible implicit declaration of built-in function 'free' [enabled by default]  Mp3.c   /OperatingSystemsLab1   
line 40 C/C++ Problem
我的代码如下

#include <stdio.h>
#include "Mp3.h"



void add(struct MP3 *pointer, char artistname, char albumname, char songname, int date, int runtime){
    /*make structure for new data*/
struct MP3 *data;
data->artistName = artistname;
data->albumName = albumname;
data->date=date;
data->runTime=runtime;
data->next=NULL;
data->prev=NULL;

if(pointer->next==NULL) {
    pointer->next = data;
}
else {
        while(pointer->next != NULL){
        pointer = pointer->next;
    }
    pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
    pointer->next = data;
    struct MP3 *temp = pointer;
    pointer = pointer->next;
    pointer->prev = temp;
}
};



void delete(struct MP3 *pointer, char *artistname){
while(pointer->next!=NULL && (pointer->next)->artistName!=artistname){
    pointer = pointer->next;
}
if(pointer->next==NULL){
    return;
}
struct MP3 *temp;
temp = pointer->next;
pointer->next = temp->next;
pointer->next->prev = pointer;
free(temp);
};



void print(struct MP3 *pointer){
if(pointer==NULL){
    return;
}
printf("Artist name: %s \n", pointer->artistName);
printf("Album name: %s \n", pointer->albumName);
printf("Title: %s \n", pointer->songName);
printf("Date: %d \n", pointer->date);
printf("Runtime: %d\n", pointer->runTime);

print(pointer->next);
};



int main(){
struct MP3 *start,*current;
start = (struct MP3 *)malloc(sizeof(struct MP3));
current = start;
current->next = NULL;
current->prev = NULL;

printf("1. Add\n");
printf("2. Delete\n");
printf("3. Print\n");
while(1)
{
    int query;
    scanf("%d",&query);
    if(query==1)
    {
        int rt,d;
        char artn,albn,sn;
        scanf("%c %c %c %d %d",&artn,&albn,&sn,&d,&rt);
        add(start,artn,albn,sn,d,rt);
    }
    else if(query==2)
    {
        char artn;
        scanf("%c",&artn);
        delete(start,&artn);
    }
    else if(query==3)
    {
        printf("The list is ");
        print(start->next);
        printf("\n");
    }
}
};
就像我说的,我是新来的,所以饶了我吧。任何帮助都将不胜感激!谢谢大家!

struct MP3*数据

声明指向数据结构的指针,但不创建结构。因此,当您试图访问它时,您正在访问未分配的内存,这会导致分段错误。 您需要使用malloc分配内存:

struct MP3 *data; /* declares a pointer to a MP3 structure */
data = malloc(sizeof(MP3)); /* creates a new MP3 structure and makes the pointer point to it */
data->artistName = artistname; /* the structure can now be used */
结构MP3*数据

声明指向数据结构的指针,但不创建结构。因此,当您试图访问它时,您正在访问未分配的内存,这会导致分段错误。 您需要使用malloc分配内存:

struct MP3 *data; /* declares a pointer to a MP3 structure */
data = malloc(sizeof(MP3)); /* creates a new MP3 structure and makes the pointer point to it */
data->artistName = artistname; /* the structure can now be used */

你有问题吗

struct MP3 *data;
data->artistName = artistname;
 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
您声明了一个指向
MP3
的指针,但没有为其分配内存。使用
malloc
分配空间,或者只使用普通实例

你这里也有问题

struct MP3 *data;
data->artistName = artistname;
 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
这将分配内存并设置
指针->下一步
指向它。然后在下一行

 pointer->next = data;

重新分配
指针->下一个
,使前一行中分配的内存不再由任何变量存储其地址-结果是内存泄漏。

这里有一个问题

struct MP3 *data;
data->artistName = artistname;
 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
您声明了一个指向
MP3
的指针,但没有为其分配内存。使用
malloc
分配空间,或者只使用普通实例

你这里也有问题

struct MP3 *data;
data->artistName = artistname;
 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
这将分配内存并设置
指针->下一步
指向它。然后在下一行

 pointer->next = data;

您重新分配
指针->下一个
,使前一行中分配的内存不再由任何变量存储其地址-其结果是内存泄漏。

正如错误消息所说-
“'data'在此函数中未初始化”
-这显然是正确的。您声明了一个指向MP3结构的指针,称为“data”。您永远不会通过分配内存并初始化指向该内存的指针来初始化指针

正如错误消息所说-
“'data'在此函数中未初始化”
-这显然是正确的。您声明了一个指向MP3结构的指针,称为“data”。您永远不会通过分配内存并初始化指向该内存的指针来初始化指针

因此,在add下,我将其更改为struct MP3*data;数据=malloc(sizeof(struct MP3));数据->艺人名称=艺人名称;但是它仍然没有正常运行-uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我现在让代码实际运行并打印一个列表,但我们只能说这根本不可取。爆笑。感谢您迄今为止的帮助。@Nick您收到了故障或警告吗?无论你做什么,请不要编辑你的问题来添加新的错误-这会导致很多很多混乱。如果有很多错误,那么使用新的更新代码打开一个新问题可能是值得的。在你的代码中使用malloc是绝对必要的。我再也不会犯错误了。这只是一个立即接收数据并正确打印列表的问题。我比以前进步了很多,所以在add下我把它改成了struct MP3*data;数据=malloc(sizeof(struct MP3));数据->艺人名称=艺人名称;但是它仍然没有正常运行-uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我现在让代码实际运行并打印一个列表,但我们只能说这根本不可取。爆笑。感谢您迄今为止的帮助。@Nick您收到了故障或警告吗?无论你做什么,请不要编辑你的问题来添加新的错误-这会导致很多很多混乱。如果有很多错误,那么使用新的更新代码打开一个新问题可能是值得的。在你的代码中使用malloc是绝对必要的。我再也不会犯错误了。这只是一个立即接收数据并正确打印列表的问题。我比以前走得更远了。