-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority_Queue.c
95 lines (95 loc) · 1.76 KB
/
Priority_Queue.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
int Q[20],Pr[20];
int r=-1,f=-1,N;
void enqueue(int data,int p)
{ int i;
if((f==0)&&(r==N-1))
printf("Queue is full");
else
{ if(f==-1)
{ f=r=0;
Q[r]=data;
Pr[r]=p;
}
else if(r == N-1)
{ for(i=f;i<=r;i++)
{ Q[i-f]=Q[i];
Pr[i-f]=Pr[i];
r=r-f;
f = 0;
for(i=r;i>f;i--)
{ if(p>Pr[i])
{ Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
Q[i+1] = data;
Pr[i+1] = p;
r++;
}
}
}
else
{ for(i=r;i>=f;i--)
{ if(p>Pr[i])
{ Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
}
Q[i+1] = data;
Pr[i+1] = p;
r++;
}
}
}
void print()
{
int i;
for(i=f;i<=r;i++)
{ printf("\nElement = %d\tPriority = %d",Q[i],Pr[i]);
}
}
int dequeue()
{ if(f == -1)
{ printf("Queue is Empty");
}
else
{ printf("Deleted Element = %d\t Its Priority = %d",Q[f],Pr[f]);
if(f==r)
f=r=-1;
else
f++;
}
}
int main()
{ int op,n,i,data,p;
printf("Enter Your Choice:-");
do
{ printf("\n\n1 Insert Data in Queue\n2 Display Data\n3 Delete data from the Queue\n4 Exit");
scanf("%d",&op);
switch(op)
{ case 1: printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data and Priority of data");
i=0;
while(i<n)
{ scanf("%d %d",&data,&p);
enqueue(data,p);
i++;
}
break;
case 2: print();
break;
case 3: dequeue();
break;
case 4: exit(0);
break;
default:printf("\nIncorrect Choice");
}
}while(op!=0);
return 0;
}