This is my code After inserting characters in the queue, If I pop the elements, the characters(name and priority) will display the latest data which I have entered, the previous data will be deleted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int NAME = 20;
int num = 1001;
typedef struct patient {
int age;
char *name;
char *priority;
struct patient* next;
} Patient;
Patient* newPatient(int age, char *p, char *name)
{
Patient* temp = (Patient*)malloc(sizeof(Patient));
temp->age = age;
temp->priority = p;
temp->next = NULL;
temp->name = name;
//strcpy(temp->name,name);
printf("name is%s",temp->name);
printf("priority is%s",temp->priority);
return temp;
}
void addPatient(Patient** head, int age, char *p, char *name)
{
Patient* start = (*head);
Patient* temp = newPatient(age, p, name);
if (atol((*head)->priority) < age) {
temp->next = *head;
(*head) = temp;
}
else {
while (start->next != NULL &&
atol(start->next->priority) < age) {
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
I'm new to coding please help me out
The problem is in Patient* newPatient(int age, char *p, char *name)
: You are not allocating new memory for priority
and name
. Either you always pass a newly allocated memory for p
and name
which is being pointed to by priority
and name
OR allocate new memory for priority
and name
in newPatient()
and then do strcpy()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.