bool CountryList::deleteNode(Country &nodeData) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node // Initialize nodePtr to head of list nodePtr = head->next; previousNode = head; // Skip all nodes whose code is not equal to the code pointed by pDeleteCode. while (nodePtr != NULL && strcmp(nodePtr->country.getCode(), nodeData.getCode()) != 0) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If node-to-delete not found OR no nodes if (!nodePtr) return false; nodeData = nodePtr->country; // return the deleted data previousNode->next = nodePtr->next; delete nodePtr; count--; return true; }
//************************************************** // The searchNode function searches for a node * // with nodeData.code. If the node was found * // then true is returned and the Country data of * // node found is returned in nodeData. If the node * // was not found, then false is returned and * // nodeData reference variable is unchanged. * //************************************************** bool CountryList::searchNode(Country &nodeData) { ListNode *nodePtr; // To traverse the list // Position nodePtr at the head of list. nodePtr = head->next; // Skip all nodes that doesn't matches code of nodeData while (nodePtr != NULL && strcmp(nodePtr->country.getCode(), nodeData.getCode()) != 0) { // Move to the next node nodePtr = nodePtr->next; } // If nodePtr is NULL (not found) if (!nodePtr) return false; // Load nodeData with data from the found node nodeData = nodePtr->country; return true; }
void CountryList::insertNode(Country countryIn) { ListNode *newNode; // A new node ListNode *nodePtr; // To traverse the list ListNode *previousNode = head; // The previous node // Allocate a new node and store countryIn there. newNode = new ListNode; newNode->country = countryIn; // Position nodePtr at the head of list. nodePtr = head->next; // Skip all nodes whose value is less than code. while (nodePtr != NULL && strcmp(nodePtr->country.getCode(), countryIn.getCode())<0) { previousNode = nodePtr; nodePtr = nodePtr->next; } previousNode->next = newNode; newNode->next = nodePtr; count++; }