Question: Modify the following program to not include duplicate names. If a user enters a duplicate name, do not insert . CODE: #include #include #include #include
Modify the following program to not include duplicate names.
If a user enters a duplicate name, do not insert .
CODE:
#include
typedef struct _user { char name [ 12 ] ; int age; struct _user * next; } User_t ; // is a type , alias to struct _user
void Print ( User_t *ptr ) { while ( ptr != NULL ) { printf ("name=%s age=%d ", ptr->name, ptr->age ); ptr = ptr->next ; // KEY POINT IN TRAVERSING LINKED LIST } }
User_t * CreateNode ( char *name, int age) { User_t *ptr = ( User_t * ) malloc ( sizeof ( User_t ) ) ;
if ( ptr != NULL ) { strcpy ( ptr->name, name); ptr->age = age; ptr->next = NULL ; return ptr; } return NULL; }
// TASK 1: Change this function so you add only if there // name is not already there.
void AddANode ( User_t *hptr, char *name, int age ) { User_t *temp = CreateNode ( name, age); // CREATE
while ( hptr->next != NULL ) // GO TO THE END OF THE LINKED LIST hptr = hptr->next ;
hptr->next = temp ; // ADD THE NEW STRUCTURE TO THE END
}
main ( )
{
User_t *currPtr = NULL , *backUpPtr = NULL ; currPtr = backUpPtr = NULL ; backUpPtr = currPtr = CreateNode ( "John", 10 );
AddANode (currPtr, "Sam", 20);
currPtr = backUpPtr; AddANode (currPtr, "Sam", 30); // THIS NAME SHOULD NOT BE ADDED
// ADD A FUNCTION TO DELETE
currPtr = backUpPtr; // NEVER CHANGE YOUR BACKUP POINTER Print( currPtr);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
