Create a process control block using this singly linked list below and c++. PCB
ID: 3890878 • 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. 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<string.h>
#include<stdlib.h>
using namespace std;
#define INIT 1
#define READY 2
#define RUNNING 3
#define BLOCKED 4
// 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);
// for inserting node, pointer to head and integer value, d.
node* insert(node* head, int pi, char *nm, int st){
// 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 = pi;
strcpy(node->name,nm);
node->state = st;
// 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){
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;
head = NULL;
// testing data <=3
for (int i=1; i<=3; i++)
// insert data into head
head = insert(head, i,(char *)"name",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->pid << " " << temp->name << " " <<temp->state << endl;;
//iterate temp across the next node
temp = temp -> next;
}
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.