Question
(Write a program, and show the output too ) C programming Print Unique Names Program 1: Modify the program not to include duplicate names. If
(Write a program, and show the output too) C programming
Print Unique Names
Program 1:
Modify the program not to include duplicate names.
If a user enters a duplicate name, do not insert
#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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started