Here are all the functions that are involved with this error. I have changed searchAction so that now I don't get the same Overflow error. Instead, now when I edit a song and go back to main, the pointer for pList is no longer valid and the changes arent kept, so when I try to printList() after editing, the program crashes. do I need a (***) triple dereference operator for this or something? How can I keep the pointer from becoming invalid once I edit the song and the program returns back to main.
Code:
int main(void)
{
Node *pList = NULL;
int option;
do
{
option = displayMenu();
switch(option)
{
case 1://load
LoadSongs(&pList);
break;
case 2://store
storeSongs(pList);
break;
case 3://display
printList(pList);
break;
case 4://insert
insertSong(&pList, getInfo());
break;
case 5://delete
searchAction(pList, 1);//1 is for delete
break;
case 6://edit
searchAction(pList, 2);//2 is for edit
break;
case 7://sort
break;
case 8://rate
searchAction(pList, 3);//3 is for rate
break;
case 9://exit
option = 0;
break;
default:
printf("Invalid choice..\n");
option = 10;
getch();
}
}while(option != 0);
return 0;
}
Code:
void searchAction(Node *pList, int searchType)
{
char sArtist[128] = {0}, sSong[128] = {0};
printf("Search:\n");
scanf("%c");
printf("Artist: ");
fgets(sArtist, 128, stdin);
printf("Song: ");
fgets(sSong, 128, stdin);
removeNewline(sArtist);
removeNewline(sSong);
switch(searchType)
{
case 1://delete
deleteSong(sArtist, sSong, &pList);
break;
case 2://edit
if(editSong(sArtist, sSong, &pList)==1)
{
//insert new song
insertSong(&pList, getInfo());
printf("Edit Successful..");
getch();
}
break;
case 3://rate
rateSong(sArtist, sSong, &pList);
break;
}
}
Code:
int editSong(char sArtist[128], char sSong[128], Node **pList)
{
Node *pMem = *pList, *pTemp = NULL;
int check = 0;
while(pMem != NULL && check==0)
{
if(strcmp(sArtist, (pMem->data)->artist)==0 && strcmp(sSong, (pMem->data)->title)==0)
{
printf("-------------------------------------------\n");
printf("%s-%s\n", (pMem->data)->artist, (pMem->data)->title);
printf("Album: %s\tGenre: %s\n", (pMem->data)->album, (pMem->data)->genre);
printf("Song length: %d:%d\t", (pMem->data)->length.minutes, (pMem->data)->length.seconds);
printf("Times Played: %d\n", (pMem->data)->num_plays);
printf("Rating: %d\n", (pMem->data)->rating);
printf("-------------------------------------------\n\n");
//delete node
if(pMem->pNext != NULL && pMem->pPrev != NULL)
{
(pMem->pPrev)->pNext = pMem->pNext;
(pMem->pNext)->pPrev = pMem->pPrev;
}else if(pMem->pNext == NULL && pMem->pPrev != NULL)
{
(pMem->pPrev)->pNext = NULL;
}else if(pMem->pNext !=NULL & pMem->pPrev == NULL)
{
(*pList) = pMem->pNext;
(pMem->pNext)->pPrev = (*pList);
}
free(pMem);
check = 1;
}else{
pMem = pMem->pNext;
}
}
if(check == 0)
{
printf("Edit Failed..No song found");
}
return check;
}
Code:
Boolean insertSong(Node **pList, Song_struct *mySong)
{
Boolean status = FALSE;
Node *pMem = NULL, *tempPrev = NULL, *tempCur = NULL;
pMem = makeNode(mySong);
if(pMem != NULL)
{
status = TRUE;
if(*pList != NULL)//if list is not empty
{
tempCur = (*pList);
while(((tempCur)!=NULL) && strcmp((pMem->data)->artist, ((tempCur->data)->artist))>0)
{
tempPrev = tempCur;
tempCur = tempCur->pNext;
}
if(tempPrev == NULL)
{
pMem->pNext = tempCur;
pMem->pPrev = NULL;
*pList = pMem;
}else if(tempCur == NULL){
pMem->pNext = NULL;
pMem->pPrev = tempPrev;
tempPrev->pNext = pMem;
}else
{
pMem->pNext = tempCur;
pMem->pPrev = tempPrev;
tempPrev->pNext = pMem;
tempCur->pPrev = pMem;
}
}else{
(*pList) = pMem;
pMem->pNext = NULL;
pMem->pPrev = NULL;
}
}
return status;
}