-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.2.cpp
More file actions
93 lines (89 loc) · 2.21 KB
/
7.2.cpp
File metadata and controls
93 lines (89 loc) · 2.21 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
#include<iostream>
using namespace std;
void enqueue();
void dequeue();
void display();
int queue[1000],priority[1000],s,rear=-1,front=-1;
int main(){
int i,c=-1;
cout<<"enter the size of the queue"<<endl;
cin>>s;
while(c!=0)
{
cout<<"0.exit\n1.enqueue\n2.dequeue\n3.display\nenter your choice"<<endl;
cin>>c;
switch(c){
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
default: if(c!=0) cout<<"enter correct choice"<<endl;
}
}
return 0;
}
void enqueue()
{
int data,p;
if(front==0 && rear==s-1){
cout<<"queue is full no further element can be taken to perform enqueue operation"<<endl;
return;
}
else
{ cout<<"enter the data and priority"<<endl;
cin>>data>>p;
if(front==-1 && rear==-1){
front=0;
rear=0;
queue[rear]=data;
priority[rear]=p;
}
else{
rear++;
queue[rear]=data;
priority[rear]=p;
for(int i=front;i<=rear;++i)
{
for(int j=i+1;j<=rear;++j)
if(priority[i]<priority[j])
{
priority[j] = priority[i]+priority[j]-(priority[i]=priority[j]);
queue[j] = queue[i]+queue[j]-(queue[i]=queue[j]);
}
}
for (int i = front; i <= rear ; i++)
{
queue[i-front]=queue[i];
priority[i-front]=priority[i];
}
rear -=front;
front=0;
}
}
}
void dequeue()
{
if(front==-1)
cout<<"queue is completely empty no further element is present to perform dequeue operation"<<endl;
else if( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
}
void display()
{
if(front!=-1)
{
cout<<"queue elements are"<<endl;
for(int i = front;i<=rear;i++)
cout<<queue[i]<<" ";
cout<<endl;
}
else
cout<<"queue is empty"<<endl;
}