C Program Using Heaps and Stacks to Manage A Printer Queue
C++ Program Using Heaps and Stacks to Manage A Printer Queue
******************************************************************************
Rules of Priority for Printer Queue
student jobs are done first in, first out. But all TA print jobs are done before any student print job, with the TA\'s print jobs being done first in, first out. And All instructor print jobs were done before any TA print job, with the instructor\'s print jobs being done first in, first out.
What Needs to Be Done
Write program to manage the printer queue. you must use Heaps and Priority Queues.
The program will display a menu and prompt the user to enter a choice:
Printer queue
=========
1. Add job
2. Print job
3 View jobs
4. Exit
Enter choice:
You may assume the user enters 1, 2, 3 or 4. No input validation is required.
If the user chooses 4. Exit, then the program ends. For Othe CHoices see below guide.
Add Job
If the user chooses Add job, then the program asks the user who is requesting the job:
Instructor (I or i)
TA (T or t)
Student (S or s)
You may assume that the the user enters I, i, T, t, S or s. No input validation is necessary.
The program then adds the print job to the priority queue. The data added reflects who the job is for (instructor, TA or student) and the number of the print job (an incrementing number, starting with 1, then 2, 3, 4, etc.).
Print Job
If the user chooses Print job, if there are any print jobs in the queue, then the program outputs, for the highest priority print job, who the job is for (instructor, TA or student) and the number of the print job. That print job also is removed from the priority queue. However, if there are no print jobs in the queue, then the program outputs: \"No print jobs in queue.\"
View Jobs
If the user chooses Print job, then the program outputs, for each print job in the queue, and in order of priority, who the job is for (instructor, TA or student) and the number of the print job. However, if there are no print jobs in the queue, then the program outputs: \"No print jobs in queue.\"
Number of Jobs
You could use a vector and just allocate elements dynamically. But if you want to use an array, you can choose a large number so you don\'t run out of job numbers.
Thanks in advance for your help with this.
Solution
#include<stdio.h>
#include<stdlib.h>
struct sNode
{
int data;
struct sNode *next;
};
/* Function to push an item to stack*/
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
/* structure of queue having two stacks */
struct queue
{
struct sNode *stack1;
struct sNode *stack2;
};
/* Function to enqueue an item to queue */
void enQueue(struct queue *q, int x)
{
push(&q->stack1, x);
}
/* Function to dequeue an item from queue */
int deQueue(struct queue *q)
{
int x;
if(q->stack1 == NULL && q->stack2 == NULL)
{
printf(\"Q is empty\");
getchar();
exit(0);
}
if(q->stack2 == NULL)
{
while(q->stack1 != NULL)
{
x = pop(&q->stack1);
push(&q->stack2, x);
}
}
x = pop(&q->stack2);
return x;
}
/* Function to push an item to stack*/
void push(struct sNode** top_ref, int new_data)
{
/* allocate node */
struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));
if(new_node == NULL)
{
printf(\"Stack overflow \ \");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
/* Function to pop an item from stack*/
int pop(struct sNode** top_ref)
{
int res;
struct sNode *top;
/*If stack is empty then error */
if(*top_ref == NULL)
{
printf(\"Stack overflow \ \");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
int main()
{
/* Create a queue with items A B C*/
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
q->stack1 = NULL;
q->stack2 = NULL;
enQueue(q, A);
enQueue(q, B);
enQueue(q, C);
/* Dequeue items */
printf(\"%d \", deQueue(q));
printf(\"%d \", deQueue(q));
printf(\"%d \", deQueue(q));
getchar();
}


