Can't insert linked list node alphabetically

Each node contains a title. I'm trying to insert nodes into a linked list alphabetically so that it is sorted by title. The function is called once for every song.I tried all day yesterday(7ish hours) and couldn't for the life of me figure it out. I would really appreciate some help.

1
2
3
4
5
6
7
8
9
10
  /////This is the list of songs before being loaded into the program///////////////////////
Break;Three Days Grace;Life Starts Now;3;13
Joker And The Thief;Wolfmother;Wolfmother;4;40
Satellite;Rise Against;Endgame;3;58
Come with Me Now;Kongos;Lunatic;3;31
I Am Machine;Three Days Grace;Human;3;20
Help Is on the Way;Rise Against;Endgame;3;57
Black Hole Sun;Soundgarden;Superunknown;5;18
mmigrant Song;Led Zeppelin;Led Zeppelin 3;2;26
////////////////////////////////////////////////////////////////// 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Songlist
{
public:
        Songlist();
        ~Songlist();


        void addsong(const Song& asong);
        void removesong(const Song& asong);
        void listAll() const;
        void loadsongs(const char fileName[]);
        void savesongs(const char fileName[]) const;
        int searchartist(const Song& asong);
        int searchalbum(const Song& asong);


private:
        struct Node
        {
                Node(const Song& asong)
                {
                        list=asong;
                        next=NULL;
                }

                Song    list;
                Node*   next;
        };

        Node*   head;
        int     size;

        void insertAtBeginning(const Song& asong);
        void append(const Song& asong);
        void insertbytitle(const Song& asong);
};


[/code]

1
2
3
4
5
Songlist::Songlist()
{
        head=NULL;
        size=0;
}


1
2
3
4
5
6
7
8
9
10
11
Songlist::~Songlist()
{
        Node* curr=head;

        while(curr)
        {
                head=curr->next;
                delete curr;
                curr=head;
        }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void Songlist::loadsongs(const char fileName[])
{
        char            buffer[MAX_CHAR];
        char            *pch;
        char            Title[MAX_CHAR];
        char            Artist[MAX_CHAR];
        char            Album[MAX_CHAR];
        float           Min;
        float           Sec;
        int             counter;
        int             length;
        Song            currsong;
        ifstream        in;

        in.open("songs.txt");

        if(!in)
        {
                exit(1);
        }

        while(in.getline(buffer, MAX_CHAR))
        {
                char temp[5][MAX_CHAR];
                counter=0;
                pch=strtok (buffer,";");

                while(pch!=nullptr)
                {
                        strcpy(temp[counter], pch);
                        pch=strtok(nullptr, ";");
                        counter++;
                }

                strcpy(Title, temp[0]);
                strcpy(Artist, temp[1]);
                strcpy(Album, temp[2]);
                Min=atoi(temp[3]);
                Sec=atoi(temp[4]);
                length=strlen(Title);

                currsong.titleset(Title);
                currsong.artistset(Artist);
                currsong.albumset(Album);
                currsong.minset(Min);
                currsong.secset(Sec);
                addsong(currsong);    //invokes insertbytitle function.

        }
        in.close();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
void Songlist::insertbytitle(const Song& asong)
{
        char    Title[MAX_CHAR];
        char    title[MAX_CHAR];
        char    titleprev[MAX_CHAR];
        int     compare;
        int     compare2;
        int     off;
        int     on;
    Node* newnode=new Node(asong); 
    Node* curr=head;
    Node* prev=NULL;

    while(off==0 && curr)   
    {
            if(curr->next==NULL)  
            {
                    prev=curr;        
                    newnode->list.titleget(Title);        
                    curr->list.titleget(title);           
                    compare=strcmp(Title,title);           

                    if(compare>0)
                    {
                            curr->next=newnode;    
                            off=1;
                    }
                    else
                    {
                            head=newnode;
                            head->next=newnode;
                            newnode->next=curr;
                            off=1;
                    }

            }
            else
            {

                    while(on==0 && curr->next!=NULL)
                    {
                            prev=curr;   
                            curr=curr->next;

                            newnode->list.titleget(Title);
                            curr->list.titleget(title);    
                            prev->list.titleget(titleprev); 
                            compare=strcmp(Title,titleprev);
                            compare2=strcmp(Title, title);

                            cout<<"newnode title is: "<<Title<<endl;
                            cout<<"next title is: "<<title<<endl;
                            cout<<"prev title is: "<<titleprev<<endl<<endl;

                            if(compare>0 && compare2<0)
                            {
                                    newnode->next=curr;
                                    curr=prev;
                                    curr->next=newnode;
                                    on=1;
                                    off=1;
                            }
                            else if(compare<0)
                            {
                                    head=newnode;
                                    on==1;
                                    off=1;
                            }
                    }
                    curr=curr->next;

            }
    }
    if(!prev)
    {
            head=newnode;       
    }
    else
    {
            prev->next=newnode;
    }

    size++;

}


1
2
3
////This is the list that the function gives me.//////////////////////////////////////////////////////////
Black Hole Sun;Soundgarden;Superunknown;5;18
Immigrant Song
Last edited on
Hello theleonicking,

I am not sure what you are doing wrong with your code tags, but this may help.
http://www.cplusplus.com/articles/z13hAqkS/

I did not get into the code much because of while(off==0 && curr). To look at it differently it might look like while(-858993460 == 0 && curr). The negative number may be different on your computer.

By not initializing your variables you are trying to use the garbage left in the memory when "off" was defined.

Always a good idea to initialize your variables.

Any how since the lhs is false the whole condition is evaluated to false and the while loop is bypassed. The same thing is happening with the next while loop, but since you will never reach that far it does not matter.

I am also wondering why you need to use a C style character array for your strings and not a "std::string"?

Andy
Thanks, I tried initializing off and on as off=0 and on=0, but It doesn't seem to change the output. In regards to using std::string, the project requires I use a C style char array.
Last edited on
You should be able to do it something like this (untested).
I called the song 'song' instead of 'list'.
The 'title()' function (or getTitle() if you must) should return a const char * of the title, not a copy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SongList::insertByTitle(const Song& s)
{
    auto nd = new Node(s);

    Node *curr{head}, *prev{};
    while (curr && strcmp(nd->song.title(), curr->song.title()) >= 0)
    {
        prev = curr;
        curr = curr->next;
    }

    if (prev)
    {
        nd->next = prev->next;
        prev->next = nd;
    }
    else
    {
        nd->next = head;
        head = nd;
    }
}

Last edited on
Topic archived. No new replies allowed.