Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a process control block using this singly linked list below and c++. PCB

ID: 3869703 • Letter: C

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 int state. Define INIT, READY, BLOCKED, and RUNNING. Please show example in main of each of these working. Thanks!

#include <iostream>
#include <cstdlib>

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;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <cstring>

enum state
{
   INIT,
   READY,
   BLOCKED,
   RUNNING
};

std::string prit_state(int i)
{
   switch(i)
   {
       case INIT:
           return "INIT";
           break;
       case READY:
           return "READY";
           break;
       case BLOCKED:
           return "BLOCKED";
           break;
       case RUNNING:
           return "RUNNING";
           break;
   }
}

// creating node object with variable data
struct node{
   int pid;
   char name[20];
   int state;
   // creating a pointer to next node in chain
   node *next;
};

// for displaying output
void display(node* head){
   // while nodes are present
   while(head){
        std::cout << "PID: " << head->pid
                   << ", Name: " << head->name
                   << ", State: " << prit_state(head->state)
                   << std::endl << " | v" << std::endl;
   //iterate temp across the next node
        head = head -> next;
   }
    std::cout << " NULL " << std::endl;
}


// for inserting node, pointer to head and integer value, d.
node* insert(node* head, int id, std::string name, int state){

   // 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 -> pid = id;
   strcpy(node->name,name.c_str());
   node -> state = state;
   // 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 -> pid != d)
       p = p -> next; //traverse list until element not found
  
   // value does not exist
   if(p -> next == NULL){
        std::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
   struct node *head = NULL;
   // testing numbers <=3

   head = insert(head, 11, "ls", READY);
   head = insert(head, 1, "memcpy", INIT);
   head = insert(head, 21, "a.out", BLOCKED);
   head = insert(head, 3, "rmdir", BLOCKED);
   head = insert(head, 4, "rm", RUNNING);

   std::cout << "Original List: "
           <<"======================== ";

   display(head);
   std::cout << "List after deletion: "
       << "=========================== ";
   // remove number 3 in chain
   head = remove(head, 3);
   display(head);
   return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote