我被卡住了,无法弄清楚为什么会出现分段错误。当我将第一个添加添加到播放列表时,一切都很好。然后,当我打印播放列表然后尝试向播放列表添加更多内容时,我会遇到分段错误。gdb 在这里报告错误if( (curr_playlist -> album) == NULL && (curr_playlist -> track_num) == NULL){
。谁能帮我找到问题?打印功能是否在修改链接列表?
struct playlist_ {
int album;
int track_num;
struct playlist_ *next;
};
typedef struct playlist_ playlists;
struct users_ {
int user_ID;
struct playlist_ *playlist;
struct users_ *next;
};
typedef struct users_ users;
/*This is how I created the user list*/
fscanf(transaction_file,"%d\n",&account_number);
/*Checks for empty list, if true creates the first user*/
if( head_users == NULL){
p_users = malloc(sizeof(users ));
p_users -> user_ID = account_number;
head_users = p_users;
curr_users = p_users;
head_users -> next = NULL;
users_pointer = head_users;
/*If list is not empty create new user and puts it in front of list*/
}else{
p_users = malloc(sizeof(users));
p_users -> user_ID = account_number;
curr_users = p_users;
curr_users -> next = head_users;
head_users = curr_users;
users_pointer = head_users;
}
/*Create an empty playlist for user and set everything to null, empty playlist*/
p_playlists = malloc(sizeof(playlists));
curr_playlists = p_playlists;
curr_playlists -> album = FALSE;
curr_playlists -> track_num = FALSE;
curr_playlists -> next = NULL;
curr_users -> playlist = p_playlists;
int add_playlist(users *user_pointer,album *all_albums,int user_ID,int album_ID,int track_num){
playlists *head_playlist,*tail_playlist,*curr_playlist,*p_playlist;
users *curr_users;
curr_users = user_pointer;
while(curr_users){
/* Find this user in link list */
if(curr_users -> user_ID == user_ID){
curr_playlist = curr_users -> playlist;
/* Check if playlist is empty for this user if so, update the empty list*/
if( (curr_playlist -> album) == NULL && (curr_playlist -> track_num) == NULL){
tail_playlist = curr_users -> playlist;
curr_playlist -> album = album_ID;
curr_playlist -> track_num = track_num;
curr_playlist -> next = NULL;
}else{
/*Creates a new playlist node with the request album and track number*/
p_playlist = (playlists *)malloc(sizeof(playlists ));
p_playlist -> album = album_ID;
p_playlist -> track_num = track_num;
p_playlist -> next = NULL;
/*Traverse Playlist, find the tail node*/
curr_playlist = curr_users -> playlist;
while(curr_playlist ){
tail_playlist = curr_playlist;
curr_playlist = curr_playlist -> next;
}
/* Adds the new playlist at the end */
tail_playlist -> next = p_playlist;
}
}
curr_users = curr_users -> next;
}
/*Updates the playlist hits for each new playlist node*/
*all_albums[album_ID].playlist_hits[track_num] = *all_albums[album_ID].playlist_hits[track_num] + 1;
}
int print_account(album *all_albums,int account_number,users *user_pointer){
users *curr_users;
playlists *curr_playlist;
int i;
curr_users = user_pointer;
/*Check playlist for each user if NULL, playlist is empty*/
while (curr_users){
if(curr_users -> user_ID == account_number){
printf("ACCOUNT %d\n",curr_users->user_ID);
if(curr_users -> playlist -> album == -100 && curr_users -> playlist -> track_num == -100){
printf(" EMPTY Playlist\n");
}else{
/*Goes through the user playlist and prints out the information*/
for(curr_playlist = curr_users -> playlist; curr_playlist; curr_playlist = curr_playlist -> next){
printf(" ALBUM %d: %s\n",curr_users -> playlist -> album,
all_albums[curr_users -> playlist -> album].tracks[curr_users -> playlist -> track_num]);
curr_users -> playlist = curr_users -> playlist -> next;
}
}
}
curr_users = curr_users -> next;
}
}