-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedEventList.java
More file actions
95 lines (78 loc) · 2.15 KB
/
LinkedEventList.java
File metadata and controls
95 lines (78 loc) · 2.15 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
/* Implements FutureEventList */
public class LinkedEventList implements FutureEventList {
private Node head;
private int size;
private int simulationTime;
public LinkedEventList(){
this.head = null;
this.size = 0;
this.simulationTime = 0;
}
//////////////////////////// Node class for LinkedEventList ///////////////////////////////////////
private class Node{
private Event event;
private Node next;
public Node(Event event){
this.event = null;
this.next = null;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public Event removeFirst() {
if(head == null){
return null;
}
Node removedNode = head;
head = head.next;
size--;
if(removedNode.event != null){
this.simulationTime = removedNode.event.getArrivalTime();
return removedNode.event;
} else{
return null;
}
}
@Override
public boolean remove(Event e) {
Node current = head;
Node prev = null;
while(current != null && current.event != e){
prev = current;
current = current.next;
}
if(current == null){
return false; // Event not found
}
if(prev == null){
head = current.next; // Remove first node
} else{
prev.next = current.next; // Remove node in the middle or end
}
size--;
if(current.event != null){
this.simulationTime = current.event.getInsertionTime();
}
return true;
}
@Override
public void insert(Event e) {
Node newNode = new Node(e);
newNode.next = head;
head = newNode;
size++;
this.simulationTime = Math.max(this.simulationTime, e.getArrivalTime());
}
@Override
public int size() {
return size;
}
@Override
public int capacity() {
return size;
}
@Override
public int getSimulationTime() {
return this.simulationTime;
}
}