-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
102 lines (100 loc) · 1.56 KB
/
Copy pathpriorityqueue.cpp
File metadata and controls
102 lines (100 loc) · 1.56 KB
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
#include<iostream>
using namespace std;
class priorityqueue
{
int queue[5],i=0,rear=-1,front=-1,max=5;
public:
void insert();
void dlt();
void display();
void ins(int);
};
void priorityqueue::insert()
{
int item;
if(rear==max-1)
cout<<"overflow condition"<<endl;
else
{
cout<<"enter the element"<<endl;
cin>>item;
if(rear==-1 && front==-1)
{
rear=front=0;
queue[rear]=item;
}
else
{
rear++;
ins(item);
}
}
}
void priorityqueue::ins(int item)
{
int loc=rear;
for(int j=front;j<rear;j++)
{
if(item<queue[j])
{
loc=j;
break;
}
}
if(loc==rear)
queue[rear]=item;
else
{
for(int j=rear;j>=loc;j--)
{
queue[j+1]=queue[j];
}
queue[loc]=item;
}
}
void priorityqueue::dlt()
{
int j;
if(front==-1)
cout<<"underflow condition"<<endl;
else
cout<<"enter priority of the element to be deleted"<<endl;
cin>>j;
for (int k=j;k<rear;k++)
{
queue[k]=queue[k+1];
}
rear--;
}
void priorityqueue::display()
{
if(front==-1)
cout<<"empty queue"<<endl;
else
for (int k=front;k<rear+1;k++)
{
cout<<queue[k]<<"\t";
}
cout<<"\n";
}
int main()
{
priorityqueue que;
int choice;
option: cout<<"enter the number against operation to be performed on priority queue\n1. insert an element\t2. delete an element\t3. display queue\t4. exit"<<endl;
cin>>choice;
switch(choice)
{
case 1: que.insert();
goto option;
case 2: que.dlt();
goto option;
case 3: que.display();
goto option;
case 4: cout<<"exiting.."<<endl;
break;
default: cout<<"invalid option"<<endl;
break;
}
return 0;
}