Question
Complete the following code with C code using linked list. #include #include /* Define structures and global variables */ int n; // maximum number of
Complete the following code with C code using linked list.
#include
/* Define structures and global variables */
int n; // maximum number of processes struct node1 { int process; struct node1 * link; };
typedef strucut node1 linked_list_type;
struct node2 { int parent; linked_list_type *children; } *pcb = NULL;
void parameter(){ /* declare local vars */ /* for each PCB index i from 0 up to (but not including) maximum number*/ /* check if PCB[i] has a parent and children */ /* print message about children of the current PCB[i] */ /* intilize variable to head of list of children */ /* while the end of the linked list of children is not reached */ /* print message about current child process index */ /* move to next node in linked list */ } /* while */ /* print newline */ }/* if */ } /* for */ printf(" Enter maximum number of processes: "); scanf("%d", &n );
};
void procedure_1(){ /* PROCEDURE FOR OPTION 1 */ /* declare local vars */ /* prompt for maximum number of processes */ /* allocate memory for dynamic array of PCBs */ /* Define PCB[0] */ /* for-loop to intitialize all other indices' parent to -1 */
pcb = (linked_list_type*) malloc(n * sizeof(linked_list_type));
return; }
void procedure_2(){ /* PROCEDURE FOR OPTION #2 */ /* define local vars */ /* prompt for parent process index p */ /* search for first available index q without a parent in a while loop */ /* allocate memory for new child process, initilize fields */ /* record the parent's index p in PCB[q] */ /* initialize the list of children of PCB[q] as empty */ /* create a new link containing the child's index q and append the link to the end of the linked list of PCB[p] */ /* call procedure to print current hierachy of processes */
return; }
void destroy(parameter){ /* RECURSIVE PROCEDURE TO DESTROY CHILDREN PROCESSES */ /* declare local vars */ /* check if end of linked list--if so return */ /* else call self on next node in linked list */ /* set variable q to current node's process index field */ /* call self on children of PCB[q] */ /* free memory of paramter */ /* reset parent of PCB[q] to -1 */ /* set paramter to NULL */
} /* end of else */ return; }
void procedure_3(){ /* PROCEDURE FOR OPTION #3 */ /* declare local vars */ /* prompt for process index p */ /* call recursive procedure to destroy children of PCB[p] */ /* reset children of PCB[p] to NULL */ /* call procedure to print current hierarchy of processes */
return; }
void procedure_4(){ /* PROCEDURE FOR OPTION #4 */ /* check if PCB is non null) /* check if children of PCB[0] is not null */ /* call recursive procedure to destroy children of PCB[0] */
} /* if */ /* free memory of PCB */ } /* if */ return; }
int main() { /* declare local vars */ /* while user has not chosen to quit */ /* print menu of options */ /* prompt for menu selection */ /* call appropriate procedure based on choice--use switch statement or series of if, else if, else statements */
} /* while loop */
return 1; /* indicates success */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started