I have been trying to learn how FIFO diagrams are made in this code and how queues are used but I don't get it.
Here is the problem: Assume there are several jobs and each job has priority values of 1, 2, 3, 4, etc. Write a program that receives the job descriptions and priorities. Create as many queues as the number of priorities and queue the jobs in the appropriate queues.
And this is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the 'job' structure that contains a description and a priority
struct job {
char description[10]; // The description of the job
int priority; // The priority of the job
};
// Define the 'node' structure that contains a job and a pointer to the next node
struct node {
struct job job; // The job in this node
struct node* next; // Pointer to the next node in the queue
};
// Define the 'queue' structure that contains pointers to the front and the end of the queue
struct queue {
struct node* front; // Pointer to the node at the front of the queue
struct node* end; // Pointer to the node at the end of the queue
};
// Function to create a new queue
struct queue* create_queue() {
struct queue* queue = (struct queue*)malloc(sizeof(struct queue)); // Allocate memory for the new queue
queue->front = queue->end = NULL; // Initialize the front and end of the queue as NULL
return queue; // Return the pointer to the new queue
}
// Function to add a job to the queue
void enqueue(struct queue* queue, struct job* job) {
struct node* temp = (struct node*)malloc(sizeof(struct node)); // Allocate memory for the new node
temp->job = *job; // Copy the job to the new node
temp->next = NULL; // Initialize the pointer to the next node as NULL
// If the queue is empty, the new node becomes the front and end of the queue
if (queue->end == NULL) {
queue->front = queue->end = temp;
return;
}
// If the queue is not empty, the new node is added to the end of the queue
queue->end->next = temp;
queue->end = temp;
}
// Function to print the queue
void print_queue(struct queue* queue) {
struct node* temp = queue->front; // Initialize a temporary pointer at the front of the queue
while (temp != NULL) { // While the temporary pointer is not NULL
printf("%s, %d\t", temp->job.description, temp->job.priority); // Print the description and priority of the job in the current node
temp = temp->next; // Advance the temporary pointer to the next node
}
printf("\n"); // Print a new line at the end
}
// Main function
int main() {
// Create 4 queues
struct queue* queues[4];
for (int i = 0; i < 4; i++) {
queues[i] = create_queue(); // Call the create_queue function to create a new queue
}
// Define some jobs with descriptions and priorities
struct job jobs[] = {
{"ABC", 1},
{"DEF", 1},
{"GHI", 1},
{"JKL", 2},
{"MNO", 2},
{"PQR", 3},
{"STU", 4}
};
// Calculate the number of jobs
int num_jobs = sizeof(jobs) / sizeof(jobs[0]);
// Add the jobs to the corresponding queues according to their priority
for (int i = 0; i < num_jobs; i++) {
enqueue(queues[jobs[i].priority - 1], &jobs[i]); // Call the enqueue function to add a job to the queue
}
// Print the queues in order of priority
printf("Queues in order of priority:\n");
for (int i = 0; i < 4; i++) {
printf("Q%d: ", i + 1);
print_queue(queues[i]); // Call the print_queue function to print the queue
}
return 0;
}