Answer to Question #258604 in C for Pragadeesh

Question #258604

In the situation where there are multiple users or a networked computer system, you probably share a printer with other users. When you request to print a file, your request is added to the print buffer. When your request reaches the front of the print buffer, your file is printed. This ensures that only one person at a time has access to the printer and that this access is given on a first-come, first-served basis. Write a C program to implement the above scenario.


1
Expert's answer
2021-10-29T11:55:09-0400


#include <stdio.h>
#include <stdlib.h>
 


struct print_buffer {
    int data;
    struct QNode* nextNode;
};
 


struct Queue {
    struct print_buffer *front, *rear;
};


struct print_buffer* newNode(int val)
{
    struct print_buffer* temp = (struct print_buffer*)malloc(sizeof(struct print_buffer));
    temp->data = val;
    temp->nextNode = NULL;
    return temp;
}




struct Queue* createQueue()
{
    struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
    q->front = q->rear = NULL;
    return q;
}
 


void insert(struct Queue* q, int k)
{
    struct QNode* temp = newNode(k);
 
    
    if (q->rear == NULL) {
        q->front = q->rear = temp;
        return;
    }
 
    
    q->rear->nextNode = temp;
    q->rear = temp;
}
 


void dequeue(struct Queue* q)
{
   
    if (q->front == NULL)
        return;
 
    struct QNode* temp = q->front;
 
    q->front = q->front->nextNode;
 
    if (q->front == NULL)
        q->rear = NULL;
 
    free(temp);
}
 
int main()
{
    struct Queue* q = createQueue();
    insert(q, 1);
    insert(q, 2);
   
    
    insert(q, 3);
    insert(q, 4);
    insert(q, 5);
    insert(q, 6);
    printf("File to be printed next : %d \n", q->front->data);
    printf("File to be printed last: %d", q->rear->data);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS