// // CD Organizer // #include using namespace std ; #include "cdorganizer.h" // Constructor // Pre - None // Post - headptr_ListByArtistAndAlbum = null // headptr_ListByYearAndAlbum = null cdorganizer::cdorganizer(void){ headptr_ListByArtistAndAlbum = (singlylinkednode*) NULL ; headptr_ListByYearAndAlbum = (doublylinkednode*) NULL ; } // Desctructor // Pre - None // Post - free memory of headptr_ListByArtistAndAlbum // free memory of headptr_ListByYearAndAlbum cdorganizer::~cdorganizer(void){ singlylinkednode * current = headptr_ListByArtistAndAlbum ; singlylinkednode * previous = headptr_ListByArtistAndAlbum ; while(current != NULL){ previous = current ; current = current->next ; delete previous ; } } // IsEmpty // Pre - None // Post - returns true if the lists are empty, false if they are not bool cdorganizer::IsEmpty(void){ } // AddCD // Pre - None // Post - Add 'amusiccd' to headptr_ListByArtistAndAlbum // Add 'amusiccd' to headptr_ListByYearAndAlbum // Unless amusiccd already exists, then simply return false bool cdorganizer::AddCD(musiccd amusiccd){ musiccd * newdata = new musiccd(amusiccd) ; singlylinkednode * newbyartistandalbumnode = new singlylinkednode() ; doublylinkednode * newbysongcountandalbumnode = new doublylinkednode() ; newbyartistandalbumnode->data = newdata ; newbysongcountandalbumnode->data = newdata ; if(headptr_ListByArtistAndAlbum == NULL) { // BY ARTIST & ALBUM, empty list, make it the first (null terminated) headptr_ListByArtistAndAlbum = newbyartistandalbumnode ; headptr_ListByArtistAndAlbum->next = (singlylinkednode*) NULL ; // BY SONG COUNT & ALBUM, empty list, make it the first (circular) headptr_ListByYearAndAlbum = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; return true ; } else { // BY ARTIST & ALBUM, find it singlylinkednode * previous = (singlylinkednode*) NULL ; singlylinkednode * current = headptr_ListByArtistAndAlbum ; while(current != NULL){ if(amusiccd.GetArtistName() == current->data->GetArtistName()) { // same artist name, check album while(current != NULL && amusiccd.GetArtistName() == current->data->GetArtistName()){ if(amusiccd.GetAlbumTitle() == current->data->GetAlbumTitle()) { // album already exists, don't insert // free memory delete newdata ; delete newbyartistandalbumnode ; newdata = (musiccd*) NULL ; return false ; } else if(amusiccd.GetAlbumTitle() > current->data->GetAlbumTitle()) { // new one is bigger, continue previous = current ; current = current->next ; } else { // new is smaller, exit this loop break ; } } // we're gonna insert, exit this loop break ; } else if(amusiccd.GetArtistName() > current->data->GetArtistName()) { // new one is bigger, move on previous = current ; current = current->next ; } else { // new on is smaller, exit this loop break ; } } // do the insert if(current == headptr_ListByArtistAndAlbum) { // new headptr, new.next = HEADPTR, HEADPTR = new newbyartistandalbumnode->next = headptr_ListByArtistAndAlbum ; headptr_ListByArtistAndAlbum = newbyartistandalbumnode ; } else { // middle or end of list, prev.next = NEW, new.next = CURRENT previous->next = newbyartistandalbumnode ; newbyartistandalbumnode->next = current ; } // Don't do the insert if we didn't do it on the first list if(newdata != NULL) { // BY SONG COUNT & ALBUM, find it doublylinkednode * previous_node = (doublylinkednode*) NULL ; if(headptr_ListByYearAndAlbum != NULL) { previous_node = headptr_ListByYearAndAlbum->prev ; } doublylinkednode * current_node = headptr_ListByYearAndAlbum ; if(current_node != NULL && current_node == previous_node) { // 1 node lists are a special case if(amusiccd.GetAlbumYear() == current_node->data->GetAlbumYear()) { if(amusiccd.GetArtistName() == current_node->data->GetArtistName() && amusiccd.GetAlbumTitle() == current_node->data->GetAlbumTitle()) { // Duplicate already exists delete newdata ; delete newbysongcountandalbumnode ; return false ; } else if(amusiccd.GetAlbumTitle() == current_node->data->GetAlbumTitle()) { // New Node is TAIL headptr_ListByYearAndAlbum->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; newbysongcountandalbumnode->prev = headptr_ListByYearAndAlbum ; newbysongcountandalbumnode->next = headptr_ListByYearAndAlbum ; return true ; } else { // New Node is HEADPTR newbysongcountandalbumnode->next = headptr_ListByYearAndAlbum ; headptr_ListByYearAndAlbum->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; newbysongcountandalbumnode->prev = headptr_ListByYearAndAlbum ; headptr_ListByYearAndAlbum = newbysongcountandalbumnode ; return true ; } } else if(amusiccd.GetAlbumYear() > current_node->data->GetAlbumYear()) { // New Node is TAIL headptr_ListByYearAndAlbum->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; newbysongcountandalbumnode->prev = headptr_ListByYearAndAlbum ; newbysongcountandalbumnode->next = headptr_ListByYearAndAlbum ; return true ; } else { // New Node is HEADPTR newbysongcountandalbumnode->next = headptr_ListByYearAndAlbum ; headptr_ListByYearAndAlbum->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; newbysongcountandalbumnode->prev = headptr_ListByYearAndAlbum ; headptr_ListByYearAndAlbum = newbysongcountandalbumnode ; return true ; } } else { do { if(current_node != NULL) { if(amusiccd.GetAlbumYear() == current_node->data->GetAlbumYear()) { // same year, check album while(current_node != NULL && amusiccd.GetAlbumYear() == current_node->data->GetAlbumYear()){ if(amusiccd.GetAlbumTitle() == current_node->data->GetAlbumTitle() && amusiccd.GetArtistName() == current_node->data->GetArtistName()) { // album already exists, don't insert // free memory delete newdata ; delete newbysongcountandalbumnode ; return false ; } else if(amusiccd.GetAlbumTitle() > current_node->data->GetAlbumTitle()) { // new one is bigger, continue previous_node = current_node ; current_node = current_node->next ; } else { // new is smaller, exit this loop break ; } } // we're gonna insert, exit this loop break ; } else if(amusiccd.GetAlbumYear() > current_node->data->GetAlbumYear()) { // new one is bigger, move on previous_node = current_node ; current_node = current_node->next ; } else { // new on is smaller, exit this loop break ; } } }while(current_node != NULL && current_node->next != headptr_ListByYearAndAlbum) ; // do the insert if(current_node == headptr_ListByYearAndAlbum) { // new headptr, new.prev = HEADPTR.prev, new.next = HEADPTR, HEADPTR.prev.next = new, HEADPTR.prev = new, HEADPTR = new newbysongcountandalbumnode->next = headptr_ListByYearAndAlbum ; newbysongcountandalbumnode->prev = headptr_ListByYearAndAlbum->prev ; headptr_ListByYearAndAlbum->prev->next = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum->prev = newbysongcountandalbumnode ; headptr_ListByYearAndAlbum = newbysongcountandalbumnode ; } else { // middle or end of list, prev.next = CURRENT, cur.prev = NEW, new.prev = PREVIOUS, new.next = CURRENT previous_node->next = newbysongcountandalbumnode ; current_node->prev = newbysongcountandalbumnode ; newbysongcountandalbumnode->prev = previous_node ; newbysongcountandalbumnode->next = current_node ; } } return true ; } else { // was a duplicate in the first list return false ; } } } // RemoveCD // Pre - None // Post - Find MusicCD and remove from headptr_ListByArtistAndAlbum // Find MusicCD and remove from headptr_ListByYearAndAlbum // Unless not found, then simply return false bool cdorganizer::RemoveCD(musiccd thecd){ } // PrintCDsByArtistAndAlbum // Pre - None // Post - Print Music CDs using overloaded << using headptr_ListByArtistAndAlbum void cdorganizer::PrintCDsByArtistAndAlbum(void){ singlylinkednode * current = headptr_ListByArtistAndAlbum ; cout << "-----------------------" << endl ; cout << "CDs By Artist And Album" << endl ; cout << "-----------------------" << endl ; int counter = 1 ; while(current != NULL){ cout << counter << ".)" ; current->data->PrintMusicCD() ; cout << endl ; current = current->next ; counter++ ; } } // PrintCDsByYearASCAndAlbum // Pre - None // Post - Print Music CDs using headptr_ListByYearAndAlbum void cdorganizer::PrintCDsByYearASCAndAlbum(void){ doublylinkednode * current = headptr_ListByYearAndAlbum ; cout << "-------------------------------" << endl ; cout << "CDs By Year ASC And Album" << endl ; cout << "-------------------------------" << endl ; int counter = 1 ; do { if(current != NULL) { cout << counter << ".)" ; current->data->PrintMusicCD() ; cout << endl ; current = current->next ; counter++ ; } } while(current != NULL && current != headptr_ListByYearAndAlbum) ; } // PrintCDsByYearDESCAndAlbum // Pre - None // Post - Print Music CDs using using headptr_ListByYearAndAlbum, but go backwards! void cdorganizer::PrintCDsByYearDESCAndAlbum(void){ doublylinkednode * current = (doublylinkednode*) NULL ; cout << "--------------------------------" << endl ; cout << "CDs By Year DESC And Album" << endl ; cout << "--------------------------------" << endl ; int counter = 1 ; if(headptr_ListByYearAndAlbum != NULL) { current = headptr_ListByYearAndAlbum->prev ; } do { if(current != NULL) { cout << counter << ".)" ; current->data->PrintMusicCD() ; cout << endl ; current = current->prev ; counter++ ; } } while(current != NULL && current != headptr_ListByYearAndAlbum->prev) ; }