// Assume we have read in the same three nodes as before
// and we want to delete the first one
// I only show what happens in the function search_Delete
/**
* Function: search_Delete new!
* Description: This function will ask the user to input a last name
* and delete the node that matches the last name
* Params: list - the header pointer of the linked list
* Returns: the header pointer of the list
*/
Node* search_Delete( Node *list )
{
char lastname[MAXNAME];
cout << endl << "Please input the last name of the people you want to delete:"; cin.get( lastname, MAXNAME ); cin.ignore( 200, '\n' );
if ( list != NULL ) {
// first compare the data with the header node
if ( strcmp(list->people.lname, lastname) == 0 ) {
// delete the header node
Node *newheader = list->next;
delete list;
return newheader;
} else {
Node *prev = searchNode( list, lastname );
if ( prev != NULL )
deleteAfter( prev );
return list;
}
}
return list; }
//NOW look at deleting the second node
Node* search_Delete( Node *list )
{
char lastname[MAXNAME];
cout << endl << "Please input the last name of the people you want to delete:"; cin.get( lastname, MAXNAME ); cin.ignore( 200, '\n' );
if ( list != NULL ) {
// first compare the data with the header node
if ( strcmp(list->people.lname, lastname) == 0 ) {
// delete the header node
Node *newheader = list->next;
delete list;
return newheader;
} else {
Node *prev = searchNode( list, lastname );
//Now call searchNode
/**
* Function: searchNode new!
* Description: This function will try to find a certain node whose
* data matchs the string given, and returns a pointer to the
* PREVIOUS node, or otherwise a NULL is returned
* Params: list - the header pointer of the linked list
* lname - the string of last name
* Returns: if found, a pointer to the previous node, or NULL otherwise
*/
Node* searchNode( Node *list, char lname[] )
{
Node *prev = NULL, *current = list;
while ( current != NULL ) {
if ( strcmp( current->people.lname, lname ) == 0 ) {
return prev;
} else {
prev = current;
current = prev->next;
// Return to while loop (repeated below) which will return 'prev' while ( current != NULL ) { if ( strcmp( current->people.lname, lname ) == 0 ) { return prev;
// Now return to searchDelete
Node* search_Delete( Node *list )
{
char lastname[MAXNAME];
cout << endl << "Please input the last name of the people you want to delete:"; cin.get( lastname, MAXNAME ); cin.ignore( 200, '\n' );
if ( list != NULL ) {
// first compare the data with the header node
if ( strcmp(list->people.lname, lastname) == 0 ) {
// delete the header node
Node *newheader = list->next;
delete list;
return newheader;
} else {
Node *prev = searchNode( list, lastname );
if ( prev != NULL ) deleteAfter( prev );
// call deleteAfter
/**
* Function: deleteAfter new!
* Description: This function will delete the node that follows the given
* node.
* Params: prev - a pointer to the previous node of the node to delete
* Returns: none
*/
void deleteAfter( Node *prev )
{
Node *current = prev->next;
prev->next = current->next; delete current;
}
// returning to searchDelete - which returns also returns the following
// NOW show what would happen when deleting the third node
Node* search_Delete( Node *list )
{
cout << endl << "Please input the last name of the people you want to delete:"; cin.get( lastname, MAXNAME ); cin.ignore( 200, '\n' );
if ( list != NULL ) {
// first compare the data with the header node
if ( strcmp(list->people.lname, lastname) == 0 ) {
// delete the header node
Node *newheader = list->next;
delete list;
return newheader;
} else {
Node *prev = searchNode( list, lastname );
//Now call searchNode
Node* searchNode( Node *list, char lname[] )
{
Node *prev = NULL, *current = list;
while ( current != NULL ) {
if ( strcmp( current->people.lname, lname ) == 0 ) {
return prev;
} else {
prev = current;
current = prev->next;
//Now do the loop again
while ( current != NULL ) {
if ( strcmp( current->people.lname, lname ) == 0 ) {
return prev;
} else {
prev = current;
current = prev->next;
// Now return to searchDelete
Node* search_Delete( Node *list )
{
char lastname[MAXNAME];
cout << endl << "Please input the last name of the people you want to delete:";
cin.get( lastname, MAXNAME );
cin.ignore( 200, '\n' );
if ( list != NULL ) {
// first compare the data with the header node
if ( strcmp(list->people.lname, lastname) == 0 ) {
// delete the header node
Node *newheader = list->next;
delete list;
return newheader;
} else {
Node *prev = searchNode( list, lastname );
if ( prev != NULL ) deleteAfter( prev );
// NOW call deleteAfter
void deleteAfter( Node *prev )
{
Node *current = prev->next;
prev->next = current->next; delete current;
}
// Now return to searchDelete
