c中的嵌套链接列表

时间:2013-10-15 00:29:06

标签: c

struct songs
{
    char* name[MAX];
    double length;
    struct songs *next;
};
typedef struct songs songs;

struct albums
{
    char* title[MAX];
    int year;
    char* singerName[MAX];
    songs songs;
    struct albums *next;
};

struct albums *head = NULL;
struct albums *curr = NULL;
struct songs *bas=NULL;
struct songs *current=NULL;

我定义了结构。第二部分包含所有专辑的列表。第一部分包含歌曲列表。专辑有歌曲。

void add(char albumTitle[],char singerName[], int releaseYear )
{
    struct albums *temp;
    temp=(struct albums *)malloc(sizeof(struct albums));
    strcpy( temp->title, albumTitle );
    temp->year=releaseYear;
    strcpy( temp->singerName, singerName );
    if (head== NULL)
    {
    curr=head=temp;
    head->next=NULL;
    curr->next=NULL;
    }
     else
    {
     curr->next=temp;
     curr=temp;
    }

    printf("Done\n");
}

此部分用于添加相册。最后,

void addSong(char albumTitle[],char songName[], double songLength )
{
    struct albums *temp;
    temp=(struct albums *)malloc(sizeof(struct albums));
    temp=head;

    struct songs *tempsong;
    tempsong=(struct songs *)malloc(sizeof(struct songs));
    tempsong=bas=current;
    while(temp!=NULL)
    {
        if(!(strcmp(temp->title, albumTitle)))
        {
            bas=temp->songs;                
            strcpy(tempsong->name,songName);
            tempsong->length=songLength;
                if (bas== NULL)
                {
                bas=tempsong;
                bas->next=NULL;
                current->next=NULL;
                }
                 else
                {
                 current->next=tempsong;
                 current=tempsong;
                }
            break;    
        }
        else
        {
            temp= temp->next;
        }
    }

}

这部分需要为专辑添加一首歌。我的问题在于这一部分。如何在大链接列表中添加歌曲? 非常感谢任何建议,

1 个答案:

答案 0 :(得分:1)

您可以使用与添加相册相同的技巧。没有区别。使用gdb或任何其他调试器逐步执行代码(gdb中的break mainrunnextstep命令),打印出您的状态数据(gdb中的print),看看有什么不对。

请参阅:https://sourceware.org/gdb/current/onlinedocs/gdb/