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

时间:2012-09-02 22:22:31

标签: c linked-list doubly-linked-list

我是网站的新手,我正在寻找一些关于双重​​链接列表的帮助,该列表存储Mp3的艺术家名称,歌曲名称,专辑名称,日期和运行时间。如果你可以帮助我,我会很感激。我在添加功能中根据GDB获得了段错误。

警告也如下:

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");
    }
}
};

头文件

#ifndef MP3_H_
#define MP3_H_

struct MP3{
char *artistName;
char *albumName;
char *songName;
int date;
int runTime;
struct MP3 *next;
struct MP3 *prev;

};


#endif /* MP3_H_ */
像我说的那样,我是新人,所以请怜悯我。任何帮助都将不胜感激!谢谢!

3 个答案:

答案 0 :(得分:2)

你这里有问题

struct MP3 *data;
data->artistName = artistname;

你声明了一个指向MP3的指针,但你没有为它分配内存。使用malloc来分配空间或仅使用普通实例。

你这里也有问题

 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));

这会分配内存并将pointer->next设置为指向它。然后在下一行

 pointer->next = data;

你重新分配pointer->next,以便上一行中分配的内存不再有任何变量存储的地址 - 结果是内存泄漏。

答案 1 :(得分:1)

struct MP3 * data;

声明指向数据结构的指针,但它不会创建结构。因此,当您尝试访问它时,您正在访问未分配的内存,这会导致分段错误。 您需要使用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 */

答案 2 :(得分:1)

正如错误消息所说 - "'data' is uninitialized in this function" - 这显然是正确的。您声明一个名为'data'的MP3结构指针。您永远不会通过分配内存并初始化指针指向该内存来初始化指针。