-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queueArray.cpp
131 lines (123 loc) · 2.07 KB
/
priority_queueArray.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
//Array Queue
# include <bits/stdc++.h>
# define max 20
using namespace std;
int front = 0, rear = 0,ele,pq[max], i,j,temp,del;
int loc = 0,flag=0;
void Enqueue(void);
void Dequeue(void);
void Display(void);
int Full(void);
int Empty(void);
void findMin();
int main()
{
int ch;
while(1)
{
cout<<"Choose Option \n";
cout<<"1. Insert\n2. Delete\n3. Find Min\n4. Display\n5. Exit\n";
scanf("%d",&ch);
switch (ch) {
case 1:Enqueue();
break;
case 2:Dequeue();
break;
case 3:findMin();
break;
case 4:Display();
break;
case 5:exit(0);
break;
default:printf("Invalid option\n");
break;
}
}
return 0;
}
int Full()
{
if(rear == max)
return 1;
else
return 0;
}
int Empty()
{
if(rear == front)
return 1;
else
return 0;
}
void Enqueue() // Complexity O(n)
{
if(Full())
printf("Queue is Full\n");
else
{
printf("Enter Queue data\n");
scanf("%d",&ele);
if( front == rear )
{
pq[front] = ele;
}
else if( front == rear - 1 )
{
if ( pq[front] > ele )
{
pq[rear] = pq[front];
pq[front] = ele;
}
else
pq[rear] = ele;
}
else if( ele >= pq[rear-1] )
{
pq[rear] = ele;
}
else
{
for ( i = front; i < rear; ++ i )
{
if ( ele <= pq[i] )
{
for ( j = rear - 1; j >= i; -- j )
pq[j+1] = pq[j];
pq[i] = ele;
break;
}
}
}
cout<<"Data "<<ele<<" Inserted successfully\n";
rear ++;
}
}
void Dequeue() // complexity O(1)
{
if(Empty())
printf("queue is Empty\n");
else
{
front ++;
}
}
void Display()
{
if(Empty())
printf("Queue is Empty\n");
else
{
for (i = front ; i<rear ; i++)
{
printf("%d ",pq[i]);
}
}
cout<<"\n";
}
void findMin() //complexity O(1)
{
if(!Empty())
cout<<"Min is "<<pq[front]<<"\n";
else
cout<<"Queue is empty\n";
}