0

私はCのリンクリストを初めて使用します。問題は、文字列のリンクリストを作成しようとしていることですが、そのリストを印刷しようとすると、2つの異なる文字列から最初の文字が印刷されます。私はいくつかの指針を台無しにしていると思います。何か助けてください。これが私のコードです...

#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;
}
4

3 に答える 3

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

コメントは真実ではありません。songTitle0最初の文字を の最初の位置にコピーしていますSongTitle

設定が複雑すぎます。リストの最初のリンクの要素に、またはをsongTitle0使用せずに を割り当てるだけで済みます。両方とも typeであるため、これは単なるポインターのコピーです。と変数をスキップしてください。それらは何の役にも立ちません。*&songTitlechar*SongTitleAuthorNameSongNotes

于 2011-11-20T22:57:31.747 に答える
0

3 つの変数 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];
于 2011-11-20T22:59:39.577 に答える
0

それは、リンクされたリストを使用することになっている方法ではありません。

これは

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

同様に、文字列のクローンを作成するには、strdup.

例えば

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

free使い終わったら、弦を忘れないでください。

于 2011-11-20T23:00:01.343 に答える