fj8283888
asked on
A struct question
The question I found it from the text as following:
You are required to design, implement and test a function, sort_list(.), which will sort such a list according to the priority value computed by whatever function is passed as its second argument. The first argument is to be a pointer to the head of the list. Since the OS will have other structures with pointers to IORB's, the list must be sorted in place.
typedef struct iorb {
short base_pri;
struct iorb *link;
char filler[110];
} IORB;
void sort_list(IORB **list,int(*newpr)(short)) ;
int newp(short base)
{
return base;
}
just want to know how to build up a list and how to insert a value.
Thanks,
Frank
You are required to design, implement and test a function, sort_list(.), which will sort such a list according to the priority value computed by whatever function is passed as its second argument. The first argument is to be a pointer to the head of the list. Since the OS will have other structures with pointers to IORB's, the list must be sorted in place.
typedef struct iorb {
short base_pri;
struct iorb *link;
char filler[110];
} IORB;
void sort_list(IORB **list,int(*newpr)(short))
int newp(short base)
{
return base;
}
just want to know how to build up a list and how to insert a value.
Thanks,
Frank
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To build a list,you need a pointer for the start node.
Lets say the start node is p;
The first node insertion:
Allocate memory for node
//p=malloc(sizeof(struct iorb));
//Insert data into the node.
p->data=//ur data here
//Set next pointer to NULL for now.
p->link=NULL;
The next time around,you allocate memory to append to this list.
i.e. create a new node,set the link pointer of the prev. node to point to this new node'
Lets say the start node is p;
The first node insertion:
Allocate memory for node
//p=malloc(sizeof(struct iorb));
//Insert data into the node.
p->data=//ur data here
//Set next pointer to NULL for now.
p->link=NULL;
The next time around,you allocate memory to append to this list.
i.e. create a new node,set the link pointer of the prev. node to point to this new node'
step 1. traverse the list (arg 1 ) ... for each element, get priority using function given as arg 2 ... store this value in the node's base_pri field
step2: sort the list in place