Basically, im creating a linked list, i am trying to include a traverse function to allow me to add nodes to after a certain point
("void insertIn(PCB_Ptr current): insert a new PCB (current) node before the first node whose CPU burst time is greater or equal to the CPU burst time of current")
I feel that i am so close to getting this achieved, however i have just encountered an issue,
Basically all the code compiles, However since adding my code of:
"
Code:
void insertIn (PCB_Ptr CPU_Burst) {
PCB_NodePtr newNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
/* case 1 empty list */
if (head == NULL)
{
head = newNode;
}
else
{
PCB_NodePtr currNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
PCB_NodePtr trailNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
currNode = head;
trailNode = NULL;
/*traversal list to find insert location */
while(currNode !=NULL)
{
if(currNode->PCB_Data->CPU_Burst >= newNode->PCB_Data->CPU_Burst)
{
break;
}
else
{
trailNode = currNode;
currNode = currNode->next;
}
}
/*Case 2 insert at head (not empty) */
if(currNode == head)
{
newNode->next = head;
head = newNode;
}
else
{
/* case 3 insert after head ( not empty */
newNode->next = currNode;
trailNode->next = newNode;
}
}
}
"
The output has started being blocked. e.g. it wont print all the output it want it display.
I was wondering if anyone could point out why?
My whole code is
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct PCB* PCB_Ptr;
typedef struct PCB_Node* PCB_NodePtr;
void insertLast(PCB_Ptr );
void insertFirst(PCB_Ptr );
void insertIn(PCB_Ptr);
void removeFirst();
void removeLast();
void kill(int );
void printList( PCB_NodePtr);
struct PCB
{
int PCB_ID;
int ArrivalTime;
int CPU_Burst;
};
struct PCB_Node
{
struct PCB* PCB_Data;
struct PCB_Node* next;
};
PCB_NodePtr head,tail;
int main()
{
PCB_Ptr job1,job2,job3,job4,job5,job6,job7;
job1=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job1==NULL)
{
printf("\n not enough Memory to create job1....");
exit(0);
};
job2=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job2==NULL)
{
printf("\n not enough Memory to create job2....");
exit(0);
};
job3=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job3==NULL)
{
printf("\n not enough Memory to create job3....");
exit(0);
};
job4=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job4==NULL)
{
printf("\n not enough Memory to create job4....");
exit(0);
};
job5=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job5==NULL)
{
printf("\n not enough Memory to create job5....");
exit(0);
};
job6=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job6==NULL)
{
printf("\n not enough Memory to create job6....");
exit(0);
};
job7=(PCB_Ptr)malloc(sizeof(struct PCB));
if(job7==NULL)
{
printf("\n not enough Memory to create job7....");
exit(0);
};
/* create 5 PCB structs */
job1->PCB_ID=1111;job1->ArrivalTime=0;job1->CPU_Burst=0;
job2->PCB_ID=2222;job2->ArrivalTime=2;job2->CPU_Burst=1;
job3->PCB_ID=3333;job3->ArrivalTime=3;job3->CPU_Burst=2;
job4->PCB_ID=4444;job4->ArrivalTime=8;job4->CPU_Burst=3;
job5->PCB_ID=5555;job5->ArrivalTime=12;job5->CPU_Burst=7;
job6->PCB_ID=6666;job6->ArrivalTime=15;job6->CPU_Burst=10;
job7->PCB_ID=7777;job7->ArrivalTime=17;job7->CPU_Burst=4;
insertLast(job1);
insertLast(job2);
insertLast(job3);
insertLast(job4);
insertLast(job5);
insertLast(job6);
insertLast(job7);
printf("\t \n the list after 4 structs insertion...");
printList(head );
printf("\t Removing the last node of the list generates :\n");
removeLast();
printList(head );
printf("\t inserting a new head of the list generates :\n");
insertFirst(job6);
printList(head );
printf("\t Removing the head of the list generates :\n");
removeFirst();
printList(head );
printf("\t \n insert Job 7 :\n");
insertIn(job7);
printList(head );
/*
printf("\t \n kill a process :\n");
kill(3);
printList(head );
*/
return 0;
}
/*1- void insertFirst(PCB_Ptr current):
insert a new PCB (current) node at the front of the queue*/
void insertFirst (PCB_Ptr curr){
PCB_NodePtr currNode=(PCB_NodePtr)malloc(sizeof(struct PCB_Node));
currNode->PCB_Data=curr;
currNode->next=NULL;
if(head==NULL)
{
head=currNode;
tail=currNode;
}
else
{
currNode->next = head;
head=currNode;
}
}
/* void removeFirst(): remove the first PCB in the queue*/
void removeFirst() {
if (head == NULL)
return;
else {
free(head); /* Deallocates memory from the current head */
if (head == tail) /*checks if the node being removed is the only node*/
{
head = NULL;
tail = NULL;
}
else {
head = head->next; /*turn the new head into the node that was next to the head*/
}
}
}
/* insert a node at the end of the queue...*/
void insertLast(PCB_Ptr curr) {
PCB_NodePtr currNode=(PCB_NodePtr)malloc(sizeof(struct PCB));
currNode->PCB_Data=curr;
currNode->next=NULL;
if(head==NULL)
{
head=currNode;
tail=currNode;
}
else
{
tail->next=currNode;
tail=currNode;
}
}
void insertIn (PCB_Ptr CPU_Burst) {
PCB_NodePtr newNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
/* case 1 empty list */
if (head == NULL)
{
head = newNode;
}
else
{
PCB_NodePtr currNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
PCB_NodePtr trailNode=(PCB_NodePtr)(PCB_NodePtr)malloc(sizeof(struct PCB));
currNode = head;
trailNode = NULL;
/*traversal list to find insert location */
while(currNode !=NULL)
{
if(currNode->PCB_Data->CPU_Burst >= newNode->PCB_Data->CPU_Burst)
{
break;
}
else
{
trailNode = currNode;
currNode = currNode->next;
}
}
/*Case 2 insert at head (not empty) */
if(currNode == head)
{
newNode->next = head;
head = newNode;
}
else
{
/* case 3 insert after head ( not empty */
newNode->next = currNode;
trailNode->next = newNode;
}
}
}
/* remove the last node in the queue...*/
void removeLast() {
if (tail == NULL)
return;
else {
free(tail);
if (head == tail) {
head = NULL;
tail = NULL;
} else {
PCB_NodePtr previousToTail;
previousToTail = head;
while (previousToTail->next != tail)
previousToTail = previousToTail->next;
tail = previousToTail;
tail->next = NULL;
}
}
}
/* print the queue nodes */
void printList( PCB_NodePtr head ) {
PCB_NodePtr temp = head;
printf ("Current node list:\n");
if (temp==NULL)
{
printf ("\t The List is Empty :(\n");
return;
}
while (temp != NULL) {
printf ("\t PCB_ID: %d", temp->PCB_Data->PCB_ID);
printf ("\t Arrival Time: %d", temp->PCB_Data->ArrivalTime);
printf ("\t CPU_Burst: %d\n", temp->PCB_Data->CPU_Burst);
temp = temp->next;
}
printf ("<<<<<<<<<<<<<<<< The End >>>>>>>>>>>>>>>>>>\n\n");
}
I would really appreciate any help,
from what i know, the only part that is blocking the output being displayed is the part i first posted, "Void insertIn"
I want to make it clear that I dont want a more efficient or better way to do it, or a revamp on the code i've done, I'm happy with it, it took me a long time to do, and its part of my uni coursework.
I would jsut appreciate if someone could point and help me understand why the output has stopped being displayed by the first quoted piece of code!
Much appreciated!
Fahkinsupah