Develop a program to simulate a simple printer queue system in C

 #include <stdio.h>

#include <stdlib.h>


#define MAX_QUEUE_SIZE 10


// Structure to represent a print job

typedef struct {

    int job_id;

    int pages;

} PrintJob;


// Structure to represent the printer queue

typedef struct {

    PrintJob jobs[MAX_QUEUE_SIZE];

    int front, rear;

} PrinterQueue;


// Function to initialize the printer queue

void initializeQueue(PrinterQueue *queue) {

    queue->front = -1;

    queue->rear = -1;

}


// Function to check if the queue is empty

int isEmpty(PrinterQueue *queue) {

    return (queue->front == -1 && queue->rear == -1);

}


// Function to check if the queue is full

int isFull(PrinterQueue *queue) {

    return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front;

}


// Function to add a job to the queue (enqueue)

void enqueue(PrinterQueue *queue, PrintJob job) {

    if (isFull(queue)) {

        printf("Queue is full. Cannot add more jobs.\n");

        return;

    }


    if (isEmpty(queue)) {

        queue->front = 0;

        queue->rear = 0;

    } else {

        queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;

    }


    queue->jobs[queue->rear] = job;

    printf("Job %d with %d pages added to the queue.\n", job.job_id, job.pages);

}


// Function to remove a job from the queue (dequeue)

PrintJob dequeue(PrinterQueue *queue) {

    PrintJob job;


    if (isEmpty(queue)) {

        printf("Queue is empty. No jobs to dequeue.\n");

        job.job_id = -1; // Return a dummy job ID to indicate empty queue

        return job;

    }


    job = queue->jobs[queue->front];


    if (queue->front == queue->rear) {

        // Reset the queue when the last element is dequeued

        queue->front = -1;

        queue->rear = -1;

    } else {

        queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;

    }


    printf("Job %d with %d pages removed from the queue.\n", job.job_id, job.pages);

    return job;

}


// Function to display all jobs in the queue

void displayQueue(PrinterQueue *queue) {

    if (isEmpty(queue)) {

        printf("Queue is empty.\n");

        return;

    }


    printf("Jobs in the queue:\n");

    int i = queue->front;

    while (i != queue->rear) {

        printf("Job %d with %d pages\n", queue->jobs[i].job_id, queue->jobs[i].pages);

        i = (i + 1) % MAX_QUEUE_SIZE;

    }

    printf("Job %d with %d pages\n", queue->jobs[i].job_id, queue->jobs[i].pages);

}


int main() {

    PrinterQueue queue;

    initializeQueue(&queue);


    // Example: Adding jobs to the queue

    PrintJob job1 = {1, 5};

    PrintJob job2 = {2, 3};

    PrintJob job3 = {3, 7};


    enqueue(&queue, job1);

    enqueue(&queue, job2);

    enqueue(&queue, job3);


    // Display the current queue

    displayQueue(&queue);


    // Example: Removing a job from the queue

    dequeue(&queue);


    // Display the updated queue

    displayQueue(&queue);


    return 0;

}

Output:


Comments