C Structures and Linked Lists.
Hi,
I want to create a C program that will do two functions as a part of a hospital waiting list.
Data storage must be dynamic, so linked lists are the way to go.
typedef structure
{
int rank;
char name;
structure *next;
} PATIENTS;
1: Record Patient.
This is accept one interger (the rank) and one string (the name).
The string is added to the list in priority order. '1' being the highest. You can have multiple people on the same rank.
2: Consult Patient.
This will accept a single interger (the rank).
This will remove the patient from the list with the highest priority value from the list. When there are more then one patient with the same rank, the earliest patient to arrive will be consulted.
Eg: each rank will act like FIFO list.
The function will return a patient's name.
I hope someone can help me.
C structures and linked list
hello again:
typedef structure
{
int rank;
char name[40];
structure *next;
} PATIENTS;
I have some knowledge of linked lists, but I can't get the int (rank) passed properly and correctly entered into the structure.
How do you pass a interger into a structure?
enter patient(int *rank, char[] )
{
PATIENTS current
current.rank->rank
current.name->name;
}
Re: prototype and structure
Quote:
Originally posted by bobthebuilder20
hello quzah,
my structure is as follows:
struct list
{
int rank;
char *string;
struct list *prev;
struct list *prev_rank;
struct list *next;
struct list *next_rank;
} LIST;
prototype:
LIST *all_mem(int, char *, LIST **head);
Cheers.
Previously, you had done the following:
Code:
newmember->rank = (int)malloc(sizeof(num));
newmember->rank = num;
Since in your function, 'rank' is not a pointer, but is a standard member of said structure, you do not allocate space for it. You only allocate space for items you're dynamicly creating. That is to say, if you have a pointer, and you want it to point to something that hasn't been created, you allocate for it.
Since every single list structure you create has an integer set aside called rank, you do not allocate space for it. The structure does that automaticly since rank is not a pointer.
Thus, all you have to do is:
Code:
newmember->rank = num;
For the second answer, I've already given it. Since you're using a pointer to a pointer to update the head of your list, all you do is, in this order:
Code:
newmember->next = *listhead;
*listhead = newmember;
What this does is first, it makes the next member of the new node point to whatever node is the top of the list.
Second, it sets the top of the list to be the new node.
You have to do those two steps in that order. If you don't, this will happen:
Code:
*listhead = newmember;
newmember->next = *list;
This makes the top of the list be the new node. Since you didn't keep track of what used to be the top first, the old list is lost.
Second, it then makes the next member of the new node point back at itself.
That is what you were doing. That is why your list was disapearing.
Quzah.