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.
#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;
}
Comments
Leave a comment