Array of Linked-lists?

Hi, so I'm working on a class project where the user can create up to 5 playlists by choosing songs from a master list.

Each playlist is going to be it's own linked-list. How do I correctly make an array of linked-lists? I thought I did it right, but my program keeps crashing when I try to assign the values from the master list to the first linked-list in the array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Music_Info {	
	string Song_Name;	
	string Artist;
	int Ranking;
	int Release_Year;
	int Decade;
	char Performer_Type;
	string Genre;
	Music_Info *nextaddr;	
};
// So each playlist is getting the struct Music_Info
Music_Info *Playlist[5];
int PLCnt = 1;     // This is used to keep track which playlist we're working on

Playlist[PLCnt]->Song_Name = current_song->Song_Name;  // Current_Song is the master linked-list. My program keeps crashing here so I think I made the array of linked-lists incorrect

Last edited on
Music_Info *Playlist[5];

This creates an array of pointers. Each of those pointers is NOT pointing at a Music_Info object. This is because you haven't made any Music_Info objects. Each of those pointers is pointing into random memory somewhere, so when you try to write to that random memory, things go very wrong.

You need to create Music_Info objects for these pointers to point at.
So how would you have it to point to a Music_Info object? Just initialize each spot in the array to with a Music_Info object?

Presumably your Music_Info* is a node in your linked list, right?

How would you manage your linked list if it was not in an array?

1
2
3
  Music_Info* list = NULL;
  ...
  list = new_node( ..., list );


Doing the same thing with an element of an array:

1
2
3
  Music_Info* lists[ N ] = { NULL };
  ...
  lists[ 0 ] = new_node( ..., lists[ 0 ] );

Hope this helps.
Topic archived. No new replies allowed.