Question: Create a process control block using this singly linked list below and c++. PCB must contain int pid, pid meaning process identifier, char[20] name, and
Create a process control block using this singly linked list below and c++. PCB must contain int pid, pid meaning process identifier, char[20] name, and int state. Define INIT, READY, BLOCKED, and RUNNING. Please show example in main of each of these working. Thanks!
#include
using namespace std;
// creating node object with variable data struct node{
int data; // creating a pointer to next node in chain node *next; };
// for displaying output void display(node* head);
// for inserting node, pointer to head and integer value, d. node* insert(node* head, int d){
// allocate piece of memory, to create new node struct node *node = (struct node*)malloc(sizeof(struct node));
// make head a pointer to last, only singly linked list. struct node *last = head;
// for inserting integer in main function node-> data = d; // set next equal to null to avoid garbage data node-> next = NULL; // if head if(head == NULL){ // let node equal head head = node; //if list empty, update head as first element
return head; //return head
} while(last-> next != NULL) // grab what last's next is, set equal to last last = last->next;
last->next = node; //else update new element as next to last element
return head;
}
node* remove(node* head, int d){ // create pointer to head and temp pointer struct node *p = head, *temp; // while p's next is null and p's next node contains no data while(p-> next != NULL && p-> next -> data != d)
p = p -> next; //traverse list until element not found // value does not exist if(p -> next == NULL){ cout << "Key not found "; // end of list return NULL; } // keep traversing list temp = p -> next; // p -> next = p-> next -> next; //updates link
// so we don't have any memory leaks. free(temp);
return head; }
int main(){
// pointer to head node *head; // testing numbers <=3 for (int i=1; i<=3; i++)
// insert numbers into head head = insert(head, i);
cout << "Original List : ";
display(head);
cout << "List after deletion : "; // remove number 2 in chain head = remove(head, 2);
display(head);
return 0;
}
void display(node* head){
// create new pointer temp and point it to last node
node * temp = head;
// while nodes are present
while(temp != NULL){ cout << temp -> data << " -> "; //iterate temp across the next node temp = temp -> next; } cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
