-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority_queue.c
117 lines (103 loc) · 2.03 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//PRIORITY QUEUE
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int front=-1,rear=-1;
int i,j,max;
struct Elements
{
int value;
int priority;
}E[10],T;
void Enqueue();
void Dequeue();
void Sort();
void Display();
int main()
{
char c,ch;
int opt;
printf("Enter the Maxsize of Priority Queue : ");
scanf("%d",&max);
do
{
printf("\n1 - Insert an element into queue");
printf("\n2 - Delete an element from queue");
printf("\n3 - Display Queue");
printf("\n0 - Exit\n");
printf("\nEnter your Choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1: Enqueue();Display();
break;
case 2: Dequeue();
break;
case 3: Display();
break;
case 0: exit(0);
default:printf("\nInvalid Input");
}
printf("\n\nDo you want to continue(y/n): ");
c=getchar();
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
return 0;
}
void Enqueue()
{
if(rear==max-1)
{
printf("\nQueue is full\n");
}
else
{
if(front==-1)
{
front=0;
}
rear++;
printf("\nEnter value and priority\n");
printf("Value : ");
scanf("%d",&E[rear].value);
printf("Priority : ");
scanf("%d",&E[rear].priority);
Sort();
}
}
void Dequeue()
{
if(front==-1)
printf("\nQueue is empty\n");
else
{
printf("Deleted \n value = %d\tpriority = %d",E[front].value,E[front].priority);
if(front==rear)
front =rear= -1;
else
front++;
}
}
void Sort()
{
for(i=front;i<rear;i++)
{
for(j=front;j<rear;j++)
{
if(E[j].priority>E[j+1].priority)
{
T=E[j];
E[j]=E[j+1];
E[j+1]=T;
}
}
}
}
void Display()
{ printf("\nPriority Queue\n");
for(i=front;i<=rear;i++)
{
printf("\nValue = %d\t",E[i].value);
printf("Priority = %d",E[i].priority);
}
}