-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPriorityQueue314.java
More file actions
109 lines (98 loc) · 2.96 KB
/
PriorityQueue314.java
File metadata and controls
109 lines (98 loc) · 2.96 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
/* Student information for assignment:
*
* On OUR honor, Rakesh and Milan, this programming assignment is OUR own work
* and WE have not provided this code to any other student.
*
* Number of slip days used: 1
*
* Student 1 (Student whose Canvas account is being used)
* UTEID:mpd2292
* email address: mdhaduk7@gmail.com
* Grader name: Casey
*
* Student 2
* UTEID: rps2439
* email address: rakesh.p.singh.college@gmail.com
*
*/
import java.util.LinkedList;
public class PriorityQueue314<E extends Comparable<E>> {
// internal storage for PriorityQueue314
private final LinkedList<E> STORAGE;
/**
* Initialize the inner storage container
* pre: none
* post: initialized STORAGE
*/
public PriorityQueue314() {
STORAGE = new LinkedList<>();
}
/**
* Adds newNode to STORAGE while maintaining the natural ordering of the list
* pre: newNode != null
* post: newNode is not changed. If newNode has the highest natural ordering out
* of the
* current list, it is added to the end of the list, and if element(s) with same
* prioritization already exist, it is added right before the element with the
* next
* highest priority
*
* @param newNode is a non null element
* @return true if element is added. Always return true.
*/
public boolean enqueue(E newNode) {
// check precondition
if (newNode == null) {
throw new IllegalArgumentException("the entered element" +
"cannot be null");
}
// add newNode to the end of the list, if the list is empty or if
// the priority of newNode is greater than all other elements in the List
if (STORAGE.isEmpty() || newNode.compareTo(STORAGE.getLast()) >= 0) {
STORAGE.add(newNode);
return true;
}
// otherwise, add newNode right before the element with the next highest
// priority in the list
for (int i = 0; i < STORAGE.size(); i++) {
if (STORAGE.get(i).compareTo(newNode) > 0) {
STORAGE.add(i, newNode);
return true;
}
}
return true;
}
/**
* Removes and gets the first element in the List
*
* @return the first element of the List
*/
public E dequeue() {
return STORAGE.removeFirst();
}
/**
* Gets the size of this List
*
* @return the size of this List
*/
public int size() {
return STORAGE.size();
}
/**
* Gets a string representation of this List
*
* @return a string representation of this List
*/
public String toString() {
return STORAGE.toString();
}
/**
* Gets the internal storage container of this List
*
* @return the LinkedList which acts as the internal storage
* container of this list
*/
public LinkedList<E> getStorage() {
return STORAGE;
}
}