Queues are common in our daily lives. When we go to bank, we stand in a queue fo
ID: 3686321 • Letter: Q
Question
Queues are common in our daily lives. When we go to bank, we stand in a queue for service. When we go to Registration to enroll in classes, we stand or wait in a queue. Another good example is when you send multiple print jobs to a printer; the "print spooler" is responsible in managing the print jobs and sending them turn by turn to the printer. The Print Spooler uses a queue to line up the print jobs and manage them. Generally, there are different kinds of queue. The one that is an interest in this project is "Priority Queue". The objects are inserted in the queue based on their priority - the object with the highest priority is inserted at the front while the one with lowest in the rear. Design a simple queue based on an integer data type with first-come-first-serve order. Dosing a priority queue where insertion into the queue is performed based on a priority value. A queue could be implemented using an array, and in some cases, that is adequate enough; however, such implementation would suffer from lack of flexibility. In this project, v/e instead use "Linked List". A linked list is a set of independent objects chained together in sequential order. It may be accessed from either end of the list, but no random access. What makes a linked list so appealing is dynamically the list can grow or shrink as needed. In terms of a diagram, a single linked list looks like as shown belowExplanation / Answer
1.code:
2.priority queue(code)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int priority;
int info;
struct node *link;
}*front=NULL;
void insert(int item, int item_priority);
int del();
void display();
int isEmpty();
main()
{
int choice,item,item_priority;
while(1)
{
printf(“1.Insert ”);
printf(“2.Delete ”);
printf(“3.Display ”);
printf(“4.Quit ”);
printf(“Enter your choice : “);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
printf(“Input the item to be added in the queue : “);
scanf(“%d”,&item);
printf(“Enter its priority : “);
scanf(“%d”,&item_priority);
insert(item, item_priority);
break;
case 2:
printf(“Deleted item is %d ”,del());
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf(“Wrong choice ”);
}
}
}
void insert(int item,int item_priority)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf(“Memory not available ”);
return;
}
tmp->info=item;
tmp->priority=item_priority;
if( isEmpty() || item_priority < front->priority )
{
tmp->link=front;
front=tmp;
}
else
{
p = front;
while( p->link!=NULL && p->link->priority<=item_priority )
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
}
int del()
{
struct node *tmp;
int item;
if( isEmpty() )
{
printf(“Queue Underflow ”);
exit(1);
}
else
{
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
}
return item;
}
int isEmpty()
{
if( front == NULL )
return 1;
else
return 0;
}
void display()
{
struct node *ptr;
ptr=front;
if( isEmpty() )
printf(“Queue is empty ”);
else
{ printf(“Queue is : ”);
printf(“Priority Item ”);
while(ptr!=NULL)
{
printf(“%5d,%5d ”,ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.