-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascending_priority_queue.c
93 lines (90 loc) · 1.97 KB
/
ascending_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
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 10
typedef struct
{
int front, rear;
int array[MAX_QUEUE_SIZE];
} Queue;
void display(Queue q)
{
if (q.front == -1 && q.rear == -1)
{
printf("\nThe queue is empty.");
}
else
{
printf("\n");
for (int i = q.front; i <= q.rear; i++)
{
printf(" %3d ", q.array[i]);
}
}
}
void push(Queue *q, int key)
{
if(q->rear == MAX_QUEUE_SIZE - 1)
printf("\nThe queue is full");
else
{
if (q->front == -1 && q->rear == -1)
q->front++;
int pos;
for(int i = q->front; i<=q->rear; i++)
{
if(q->array[i]<=key)//make this >= for descending order PQ
pos = i+1;
}
for(int i = q->rear; i>=pos; i--)
q->array[i+1] = q->array[i];
q->rear++;
q->array[pos] = key;
}
}
int pop(Queue *q)
{
int temp = q->array[q->front];
q->front++;
if (q->front > q->rear)
{
q->front = -1;
q->rear = -1;
}
return temp;
}
void main()
{
printf("Manoj M Mallya\n 200905130\n SECTION C2\n ROLL NO. 23\n\n\n");
Queue q;
q.front = -1;
q.rear = -1;
int choice = 0, ele;
do
{
printf("\n1: Display the Queue \n2: Pop \n3: Push an element \n4: Exit");
printf("\nEnter the operation to be done: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
display(q);
break;
case 2:
if (q.front == -1 && q.rear == -1)
printf("\nThe queue is empty");
else
{
ele = pop(&q);
printf("\nElement poppped is %d", ele);
}
break;
case 3:
printf("\nEnter the element to be pushed : ");
scanf("%d", &ele);
push(&q, ele);
break;
}
printf("\n");
}
while(choice!=4);
}