C中的字符串链表

时间:2011-11-20 22:53:28

标签: c singly-linked-list

我是C中链接列表的新手,我遇到的问题是我正在尝试创建一个字符串的链表,但是当我尝试打印该列表时,它会打印出来自两个不同字符串的第一个字符。我想我搞砸了一些指针。有什么帮助吗? 这是我的代码......

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


int main()
{
    typedef struct _song{char *songTitle; char *songAuthor; char *songNote; struct _song *next;}SONG;

    int songCount =4;


    char SongTitle[songCount];
    char AuthorName[songCount];
    char SongNotes[songCount];


    char songTitle0[21] = "19 problems";
    char songArtist0[21]="JayZ";
    char songNotes0[81]="JiggaWhoJiggaWhat";
    SongTitle[0]=*songTitle0;//points at string songTitle0
    AuthorName[0]=*songArtist0;
    SongNotes[0]=*songNotes0;

    char songTitle1[21] = "Cig Poppa";
    char songArtist1[21]="Biggie Smalls";
    char songNotes1[81]="I Luv it When you call me big poppa";
    SongTitle[1]=*songTitle1;
    AuthorName[1]=*songArtist1;
    SongNotes[1]=*songNotes1;

    SONG *CurrentSong, *header, *tail;

    int tempCount=0;
    header = NULL;

    for(tempCount=0;tempCount<songCount;tempCount++)
    {

        CurrentSong = malloc(sizeof(struct _song));
        CurrentSong->songTitle= &SongTitle[tempCount];
        CurrentSong->songAuthor=&AuthorName[tempCount];
        CurrentSong->songNote=&SongNotes[tempCount];

        if(header == NULL)
        {
            header=CurrentSong;//head points to first thing in memory
        }
        else
        {
            tail->next=CurrentSong;
        }
        tail = CurrentSong;//always the last thing in the list 
        tail->next=NULL;//the next pointer is null always

    }
    tempCount =0;
    for(CurrentSong=header; CurrentSong!=NULL; CurrentSong=CurrentSong->next)
                    {
                        printf("\n%d: ", tempCount);
                            printf("Title: %s ",CurrentSong->songTitle);


                        printf("Author: %s ",CurrentSong->songAuthor);
                        tempCount++;
                    }


    return 0;
}

3 个答案:

答案 0 :(得分:0)

SongTitle[0]=*songTitle0;//points at string songTitle0

评论不正确。您将第一个字符songTitle0复制到SongTitle的第一个位置。

您的设置太复杂了。您只想将songTitle0 *&songTitle分配给列表第一个链接的char*元素;两者都是SongTitle类型,因此它只是一个指针副本。跳过AuthorNameSongNotes和{{1}}变量,它们没有用处。

答案 1 :(得分:0)

三个变量SongTitle,AuthorName和SongNotes是char的数组,而不是string的数组。您需要将其声明更改为:

char* SongTitle[songCount];
char* AuthorName[songCount];
char* SongNotes[songCount];

然后,您需要像这样更新它们:

SongTitle[0] = songTitle0;//points at string songTitle0
AuthorName[0] = songArtist0;
SongNotes[0] = songNotes0;

当您将它们存储在链接列表中时:

    CurrentSong = malloc(sizeof(struct _song));
    CurrentSong->songTitle = SongTitle[tempCount];
    CurrentSong->songAuthor = AuthorName[tempCount];
    CurrentSong->songNote = SongNotes[tempCount];

答案 2 :(得分:0)

这不是你应该如何使用链接列表。

这是

typedef struct list {
    void *data;
    struct list *next;
}
SONG *s = (SONG *)songList->data;

同样,要克隆字符串,您需要使用strdup

E.g。

s->songTitle = strdup(SongTitle);
s->songAuthor = strdup(AuthorName);
s->songNote = strdup(SongNotes);

完成后,不要忘记free字符串。