-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.java
146 lines (131 loc) · 3.92 KB
/
DoublyLinkedList.java
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
public class DoublyLinkedList {
private Node head;
private Node tail;
private int size;
public DoublyLinkedList() {
this.size = 0;
this.head = null;
this.tail = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size() == 0;
}
public void addWrapper(Node data) {
synchronized(this) { /* Also synchronized(obj){ } structure is OK to use for protecting critical sections in your methods).*/
addLast(data);
}
}
public void addLast(Node data) {
if (isEmpty()) {
data.setPrev(null);
data.setNext(null);
head = tail = data;
} else {
data.setPrev(tail);
data.setNext(null);
tail.setNext(data);
tail = tail.getNext();
}
size++;
}
public void addFirst(Node data) {
if (isEmpty()) {
data.setPrev(null);
data.setNext(null);
head = tail = data;
} else {
data.setNext(head);
head.setPrev(data);
head = head.getPrev();
}
size++;
}
public Node removeFirstWrapper() {
synchronized(this) { /* Also synchronized(obj){ } structure is OK to use for protecting critical sections in your methods).*/
return removeFirst();
}
}
public Node removeFirst() {
if (isEmpty()) {
throw new RuntimeException("ERROR: Doubly Linked List is Empty!");
}
Node temp = head;
head = head.getNext();
size--;
if (isEmpty()) {
tail = null;
} else {
head.setPrev(null);
}
return temp;
}
public Node removeLast() {
if (isEmpty()) {
throw new RuntimeException("ERROR: Doubly Linked List is Empty!");
}
Node temp = tail;
tail = tail.getPrev();
size--;
if (isEmpty()) {
head = null;
} else {
tail.setNext(null);
}
return temp;
}
public Node removeHighestPRWrapper() {
synchronized(this) { /* Also synchronized(obj){ } structure is OK to use for protecting critical sections in your methods).*/
return removeHighestPR();
}
}
public Node removeMinCPUBurstWrapper() {
synchronized(this) { /* Also synchronized(obj){ } structure is OK to use for protecting critical sections in your methods).*/
return removeMinCPUBurst();
}
}
public Node removeMinCPUBurst() {
if (isEmpty()) {
throw new RuntimeException("ERROR: Doubly Linked List is Empty!");
}
Node toRemove = head;
Node temp = head;
int min = temp.getCpuBurst()[temp.getCpuIndex()];
while (temp != null) {
if (min > temp.getCpuBurst()[temp.getCpuIndex()]) {
min = temp.getCpuBurst()[temp.getCpuIndex()];
toRemove = temp;
}
temp = temp.getNext();
}
return removeSpecificNode(toRemove);
}
public Node removeHighestPR() {
if (isEmpty()) {
throw new RuntimeException("ERROR: Doubly Linked List is Empty!");
}
Node toRemove = head;
Node temp = head;
int max = temp.getPr();
while (temp != null) {
if (max < temp.getPr()) {
max = temp.getPr();
toRemove = temp;
}
temp = temp.getNext();
}
return removeSpecificNode(toRemove);
}
public Node removeSpecificNode(Node data) {
if (data.getPrev() == null) return removeFirst();
if (data.getNext() == null) return removeLast();
data.getNext().setPrev(data.getPrev());
data.getPrev().setNext(data.getNext());
data.setPrev(null);
data.setNext(null);
size--;
return data;
}
}