// 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 )
{
	
lab12p3-1.gif (4390 bytes)
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;
lab12p3-2.gif (5552 bytes)

			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 )

{
lab12p3-1.gif (4390 bytes)
	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;
lab12p3-3.gif (7612 bytes)
	while ( current != NULL ) {
		if ( strcmp( current->people.lname, lname ) == 0 ) {
			return prev;
		} else {
			prev = current;
			current = prev->next;
lab12p3-4.gif (7860 bytes)
// 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 );
lab12p3-5.gif (4949 bytes)

			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;
lab12p3-6.gif (7640 bytes)
	prev->next = current->next;
	delete current;
lab12p3-7.gif (7822 bytes)

}
// returning to searchDelete - which returns also returns the following
lab12p3-8.gif (3597 bytes)

// NOW show what would happen when deleting the third node


 

Node* search_Delete( Node *list )

{
lab12p3-1.gif (4390 bytes)

	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;
lab12p3-9.gif (7568 bytes)
	while ( current != NULL ) {
		if ( strcmp( current->people.lname, lname ) == 0 ) {
			return prev;
		} else {
			prev = current;
			current = prev->next;
lab12p3-10.gif (7628 bytes)

//Now do the loop again
	while ( current != NULL ) {
		if ( strcmp( current->people.lname, lname ) == 0 ) {
			return prev;
		} else {
			prev = current;
			current = prev->next;
lab12p3-11.gif (8018 bytes)

// 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 );
lab12p3-12.gif (5100 bytes)

			if ( prev != NULL )
				deleteAfter( prev );

// NOW call deleteAfter


 

void deleteAfter( Node *prev )
{
	Node *current = prev->next;
lab12p3-13.gif (5570 bytes)
	prev->next = current->next;
	delete current;
lab12p3-14.gif (5696 bytes)

}

// Now return to searchDelete


lab12p3-15.gif (4036 bytes)