-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPriorityQueue.c
95 lines (95 loc) · 2.54 KB
/
PriorityQueue.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<stdbool.h>
int Msize=30,f=-1,r=-1; //Settting max size and front and rear
int A[30]; //Declaring array for queue
bool isEmpty(){ //Function to check if queue is empty
return (f==-1 || f>r);
}
bool isFull(){ //Function to checek if queue is full
return (r==Msize-1);
}
void deQueue(){
if(isEmpty()){ //Underflow condition
printf("Queue Underflow\n");
}
else{
printf("Dequeued element is %d",A[f++]);//printing dequeued element and incrimenting front
}
}
void enQueue(){
int key;
if(isEmpty()){ //First element case
printf("Enter elements :");
scanf("%d",&A[++r]);
f++;
}
else if(isFull()){ //Overflow condition
printf("Queue Overflow\n");
}
else{
printf("Enter elements : ");
scanf("%d",&A[++r]);//incrimenting rear and adding element into rear position
key = A[r];
for (int i=r-1;i>=f;i--){//inserting to correct position
if (key<A[i]){
A[i+1]=A[i];
A[i]=key;
}
else{
break;
}
}
}
}
void size(){//Function to return size
if(isEmpty()){
printf("Size is %d\n",0);
}
else{
printf("Size is %d\n",r-f+1);
}
}
void display(){//To display
if(isEmpty()){//Underflow condition
printf("Queue Underflow\n");
}
else{
printf("The queue is :\n");
for (int i=f;i<=r;i++){//printing each element one by one in correct order
printf("%d ",A[i]);
}
printf("\n");
}
}
int main(){
int ch,c=0;
printf("ASCENDING ORDER PRIORITY QUEUE\n");
while (true){//menu driven
printf("1. Display\n");
printf("2. Enqueue\n");
printf("3. Dequeue\n");
printf("4. Size\n");
printf("5. Exit\n\n");
printf("Enter choice :");//Inputing choice
scanf("%d",&ch);
//Evaluating choice
switch (ch){
case 1 :display();break;
case 2 :enQueue();break;
case 3 :deQueue();break;
case 4 :size();break;
case 5 :return 0;break;
default:printf("Invalid Choice\n");
}
printf("\n");
if(f==Msize){//Queue ending function i.e, disadvantage of normal queue
printf("Queue Ended \n");
return 0 ;
}
c=c+1;
if(c==200){//Condition to prevent loop from running till infinity
printf("Limit reached\n");
return 0 ;
}
}
}