forked from rjkalash/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueApp.java
More file actions
127 lines (96 loc) · 2.94 KB
/
PriorityQueueApp.java
File metadata and controls
127 lines (96 loc) · 2.94 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
package assignment;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PriorityQueueApp {
public static void main(String[] args) {
PriorityQueue x = new PriorityQueue();
x.enqueue("Lak", 2.63);
x.enqueue("Sam", 3.5);
x.enqueue("Mursit", 1.2);
x.enqueue("Ran", 4.9);
x.enqueue("Dock", 1.2);
x.enqueue("Anan", 0.98);
x.enqueue("Yello", 2);
x.display();
System.out.println();
try {
System.out.println(x.dequeue() + " ,student is removed from the list.");
} catch (Exception e) {
Logger.getLogger(PriorityQueueApp.class.getName()).log(Level.SEVERE, null, e);
}
System.out.println();
x.display();
}
}
class Student {
String studentName;
double gpa;
Student(String studentName, double gpa) {
//fill the body
this.studentName = studentName;
this.gpa = gpa;
}
}
class Node {
Student s;
Node next;
Node(Student s) {
this.s = s;
this.next = null;
}
}
class PriorityQueue {
private Node head;
private Node tail;
public PriorityQueue() {
this.head = null;
this.tail = null;
}
public void enqueue(String studentName, double gpa) {
if (head == null) {
Student student = new Student(studentName, gpa);
head = new Node(student);
tail = head;
return;
}
Student student = new Student(studentName, gpa);
head = insertNewNode(student, head);
}
private Node insertNewNode(Student student, Node currentNode) {
//class to inser new node
if (student.gpa < currentNode.s.gpa) {
Node newNode = new Node(student);
newNode.next = currentNode;
return newNode;
}
if (currentNode.next == null) {
currentNode.next = new Node(student);
this.tail = currentNode.next;
} else {
currentNode.next = insertNewNode(student, currentNode.next);
}
return currentNode;
}
public String dequeue() throws Exception {
/*remove the node of the front student and return the name of the student*/
if (this.head == null) {
throw new Exception("Queue is Empty");
}
String removedData = this.head.s.studentName;
this.head = this.head.next;
return removedData;
}
public String peek() throws Exception {
//return the name of the front student
return this.head.s.studentName;
}
public void display() {
//display all the student names in the queue
System.out.println("Studets list with GPA :");
Node currentNode = this.head;
while (currentNode != null) {
System.out.println(currentNode.s.gpa + " - " + currentNode.s.studentName);
currentNode = currentNode.next;
}
}
}