/************************************************************************** parameters: int return value: bool Since a paintinglist will never be empty, since it is only created when the user inputs a painting and an artist, we can always check the first item in the paintinglist. If the head of the list is the node to remove, then store the value at head in a temporary pointer and move the head to the next node in the list. If the next node in the list is NULL remove the temporary pointer and return true (denoting that the artist should be deleted too, since he has no paintings). Otherwise, delete the temporary variable, and its' next is the new head of the list. If the node to delete is not the first in the list, search for it. If it does not equal to NULL grab its' parent and child. If the child is NULL then set the tail of the list to the parent (since the node to delete is the last in the list), and then set the parent's next to NULL. Otherwise set the parents next to the child, and delete the node. **************************************************************************/ bool PaintingList::removePainting(int temp_id) { if(head->idMatch(temp_id)) { Painting * temp = head; head = head->getNext(); if(NULL == head) { delete(temp); temp->setNext(NULL); head = NULL; tail = NULL; return true; } else { delete(temp); temp->setNext(NULL); return false; } } Painting * pNodeParent = findPaintingParent(temp_id); if(NULL != pNodeParent) { Painting * pNode = pNodeParent->getNext(); Painting * pNodeChild = pNode->getNext(); delete(pNode); pNode->setNext(NULL); if(NULL == pNodeChild) { tail = pNodeParent; pNodeParent->setNext(NULL); } else pNodeParent->setNext(pNodeChild); } else cout << endl << "ERROR: Could not find painting with id: " << temp_id << endl; return false; }
/************************************************************************** parameters: - return value: - Starting from the head, while the current node does not equal null, output the nodes contents. **************************************************************************/ void PaintingList::printAll() { Painting * curr = head; while(NULL != curr) { curr->print(); curr = curr->getNext(); } }
/************************************************************************** parameters: string return value: Painting * Starting from the head, while the current node does not equal NULL, compare the passed in title to the current node's title. If it matches return current node. Otherwise continue. If current node is equal to NULL return NULL (since no matching title was found). **************************************************************************/ Painting * PaintingList::findPainting(int temp_id) { Painting * curr = head; while(NULL != curr) { if(curr->idMatch(temp_id)) return curr; curr = curr->getNext(); } return NULL; }
/************************************************************************** parameters: - return value: - Destructor, deletes all dynamically allocated parts of an instance of the class. As well as, the instance. **************************************************************************/ Artist::Artist(Artist& a) { cout << "Please enter a new first name: "; getline(cin, firstName); lastName = a.getLastName(); numberOfPaintings = a.getNumberOfPaintings(); pList = new PaintingList(); next = NULL; int i; PaintingList * pl = a.getPaintingList(); Painting * p = pl->getHead(); for(i = 0; i < numberOfPaintings; i++, p = p->getNext()) { pList->appendToTail(p->copy()); } }