-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueDS.java
More file actions
163 lines (152 loc) · 4.08 KB
/
PriorityQueueDS.java
File metadata and controls
163 lines (152 loc) · 4.08 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.util.*;
class PriorityQueueDS
{
int a[]=new int[100];
int size=0;
public void insert(int x)
{
if(size>=a.length)
{
System.out.println("Priority Queue is full!");
return;
}
a[size++]=x;
ShiftUp(size-1);
}
public void ShiftUp(int n)
{
while (n>0 && a[parent(n)]<a[n])
{
swap(parent(n),n);
n=parent(n);
}
}
public int parent(int n)
{
return(n-1)/2;
}
void swap(int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public void display()
{
if (size<=0)
{
System.out.println("Priority Queue is empty!");
return;
}
System.out.print("Priority Queue: ");
for (int i=0;i<size;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public void remove(int y)
{
int idx=-1;
for (int i=0;i<size;i++)
{
if (a[i]==y)
{
idx=i;
break;
}
}
if (idx==-1)
{
System.out.println("Element not found!");
return;
}
a[idx]=a[size-1];
size--;
ShiftUp(idx);
ShiftDown(idx);
}
public void ShiftDown(int n)
{
while (2*n+1<size)
{
int j=2*n+1;
if(j+1<size && a[j]<a[j+1])
j++;
if (a[n]>=a[j])
break;
swap(n,j);
n=j;
}
}
public void ChangePriority(int oldV, int newV)
{
int idx=-1;
for (int i=0;i<size;i++)
{
if(a[i]==oldV)
{
idx=i;
break;
}
}
if (idx==-1)
{
System.out.println("Element not found!");
return;
}
a[idx]=newV;
if(newV>oldV)
ShiftUp(idx);
else
ShiftDown(idx);
}
public static void main(String[] args)
{
Scanner sr=new Scanner(System.in);
PriorityQueue p=new PriorityQueue();
while(true)
{
System.out.print("Menu: \n1. Insert \n2. Display \n3. Remove \n4. Change Priority \n5. Max value \n0. Exit \nEnter choice: ");
switch(sr.nextInt())
{
case 1:
System.out.print("Enter the element you want to insert: ");
p.insert(sr.nextInt());
System.out.println("Element inserted successfully.");
break;
case 2:
p.display();
break;
case 3:
if (p.size<=0)
{
System.out.println("Priority Queue is empty!");
break;
}
System.out.print("Enter the element you want to delete: ");
p.remove(sr.nextInt());
System.out.println("Element removed successfully.");
break;
case 4:
System.out.println("Enter old value: ");
int oldV=sr.nextInt();
System.out.println("Enter new value: ");
int newV=sr.nextInt();
p.ChangePriority(oldV,newV);
System.out.println("Priority changed successfully.");
break;
case 5:
System.out.println("Max element: "+p.a[0]);
break;
case 0:
System.out.println("Program Ended !");
sr.close();
return;
default:
System.out.println("Invalid choice !");
break;
}
}
}
}