-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueUsingReferenceVariable.c
More file actions
81 lines (76 loc) · 1.83 KB
/
PriorityQueueUsingReferenceVariable.c
File metadata and controls
81 lines (76 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define Queue_size 5
//c function to insert an item at the correct place in priority
void insert_item(int *item,int q[],int *r)
{
int x=*item;
if(*r==Queue_size-1)
{
printf(" Queue is full\n");
return;
}
//find appropriate position to make room for inserting an item based on priority
while(((*r)>=0) && x<=q[*r])
{
q[(*r)+1]=q[*r];
*r=*r-1;
}
q[(*r)+1]=x;
*r=*r+1;
}
// c function to delete an integer item from front using reference variable
void delete_front(int q[],int *f,int *r)
{
if(*f>*r)
{
printf(" Queue is empty \n"); //underflow
return;
}
printf(" Item deleted from queue %d\n",q[(*f)++]);
if(*f>*r)
{
*f=0;
*r=-1;
}
}
//after reading an item ,it may so happen that queue may be empty so immediately after deleting an element if q is empty initialize f=0 , r=-1
void display(int q[],int f,int r)
// c function to display queue contents using reference variables
{
int i;
if(f>r)
{
printf(" Queue is empty\n");
return;
}
printf(" The contents of queue are \n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
void main()
{
int choice,item,f,r,q[5];
f=0;
r=-1;
for(;;)
{
printf(" 1.Insert 2.Delete 3.Display 4.Exit \n Enter choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf(" Enter item to be inserted\n");
scanf("%d",&item);
insert_item(&item,q,&r);
break;
case 2:delete_front(q,&f,&r);
break;
case 3:display(q,f,r);
break;
default: exit(0);
} // end of switch
} // end of for loop
} // end of main