Please I need help on adding a node as the second node of the listand adding a n
ID: 3612937 • Letter: P
Question
Please I need help on adding a node as the second node of the listand adding a node somewhere else in the list and able to print itout. Below is my text file, I have 6 nodes in my text filePete Okay 45 345
Maud Okay 36 536
Caleb Dimitri 15 123
Cadrin Christopher 9 695
John Williams 12 542
James Evans 44 211
And below is the structure definition:
struct patient
{
char firstname[20];
char lastname[20];
int age;
int id;
struct patient *link;
};
struct clinic
{
char nameofclinic[30];
struct patient *linklist;
};
void patients(FILE *xfiles, struct patient *mems);
int main(void)
{
FILE*file;
struct clinicmembers;
struct patient *xpter
file openstatements..........
patients(file, xpter);
fclose(....);
}
void patients(FILE *xfiles, struct patient *mems)
{
struct patient*pter;
members->linklist =(struct patient *) malloc(1 * sizeof(struct patient);
pter =(struct patient *) malloc(1 * sizeof(struct patient);
fscanf(xfiles, "%s %s %d %d",members->linklist->firstname,members->linklist->lastname,&(members->linklist->age).........);
fscanf(xfiles, "%s %s %d %d",pter->firstname, pter->lastname,&(pter->age).........);
and I add a node to an empty listand add a node as the new head of the list like below
BUT I DON'T KNOW WHAT TO DO AFTER THIS SOPLEASE HELP
members->linklist->link = NULL;
pter->link =members->linklist;
members->linklist =pter;
help on adding a node as the second node of the list and adding anode somewhere else in the list and able to print it out.
Explanation / Answer
//you wll needtwo pointer for first and last, check the head node byfirst==NULL , in else case add to last. structpatient *first=NULL,*last=NULL;void patients(FILE *xfiles, structpatient *mems)
{
struct patient*pter;
members->linklist =(struct patient *) malloc(1 * sizeof(struct patient);
pter =(struct patient *) malloc(1 * sizeof(struct patient);
fscanf(xfiles, "%s %s %d %d",members->linklist->firstname,members->linklist->lastname,&(members->linklist->age).........);
fscanf(xfiles, "%s %s %d %d",pter->firstname, pter->lastname,&(pter->age).........);
if(first==NULL) { members->linklist->link = NULL;
last = first =members->linklist;
}else { members->linklist->link = NULL;
last ->link = members->linklist;
last =last->link; } } structpatient *first=NULL,*last=NULL;
void patients(FILE *xfiles, structpatient *mems)
{
struct patient*pter;
members->linklist =(struct patient *) malloc(1 * sizeof(struct patient);
pter =(struct patient *) malloc(1 * sizeof(struct patient);
fscanf(xfiles, "%s %s %d %d",members->linklist->firstname,members->linklist->lastname,&(members->linklist->age).........);
fscanf(xfiles, "%s %s %d %d",pter->firstname, pter->lastname,&(pter->age).........);
if(first==NULL) { members->linklist->link = NULL;
last = first =members->linklist;
}else { members->linklist->link = NULL;
last ->link = members->linklist;
last =last->link; } }