01.2 (13 marks) Consider the following program: #include #include #include typed
ID: 3920936 • Letter: 0
Question
01.2 (13 marks) Consider the following program: #include #include #include typedef struct family char name: int age: struct family younger: struct family "older FAMILY: void insert_ node (FAMILY temp_ age) myptr, char temp_ name, int void inorder (FAMILY myptr) if (myptr -NULL) inorder (myptr->older): printfedmyptr //printf(" * %d * "myptr->age); r-name, myptr->age); inorder (myptr->younger) int main int check-1; char temp name [100]: int temp_ age FAMILY "ptr-NULL printf ("Creating family tree...In") printf(".**In) while (check) t printf("Please insert name: "): scanf ("%s", tempname) ; printf("Please insert age:") scanf ("id", &temp;_age) : - insert_node (&ptr;, temp_name, temp_age) : printf ("Any more family members? Y/N (1/0):) scanf ("%d", ✓) ; inorder (ptr): return 0:Explanation / Answer
void insert_node(FAMILY **myptr,char *temp_name,int temp_age)
{
FAMILY *temp = (FAMILY *)malloc(sizeof(FAMILY);//allocates memory to new node dynamically temp->name = temp_name; //assigning name temp->age = temp_age; //assigning age temp->younger=NULL; temp->older=NULL;
if(*(myptr)==NULL) myptr=&temp;
else { FAMILY **x=myptr; while(*x!=NULL) //to find the node where given is to be inserted {
if(*x->age>temp_age) //if younger go to younger side *x=*x->younger;
else //else older *x=*x->older;
}
x=&temp;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.