-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventQueue.cpp
147 lines (134 loc) · 3.08 KB
/
eventQueue.cpp
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
/*
* PA4_the_murder - Jacob Tutlis - jjtutlis - James Taylor - jrtaylor
* Created by jrtaylor on 2/11/18.
*/
#include "eventQueue.h"
#include "event.h"
event *eventQueue::getFirst() {
return head->data;
}
/**
* for getting an event at the given index
* @param index
* @return
*/
event *eventQueue::getIndex(int index) {
node *cur = new node;
cur = head;
for (int i = 0; i < index; ++i) {
cur = cur->next;
}
return cur->data;
}
/*
* creates a new event node
* @param event new event
* @return void
*/
void eventQueue::createNode(event *e) {
node *temp = new node;
temp->data = e;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
temp = NULL;
} else {
tail->next = temp;
tail = temp;
}
}
/* displays all the nodes in linked list
* @return void
*/
void eventQueue::display() {
node *temp;
temp = new node;
temp = head;
while (temp != NULL) {
cout << temp->data->getArrTime() << "\t";
temp = temp->next;
}
}
/* adds event to the start of the linked list
* @param value event being added
* @return void
*/
void eventQueue::insert_start(event *value) {
node *temp;
temp = new node;
temp->data = value;
temp->next = head;
head = temp;
}
/* adds event to the correct position of the linked list
* @param value event being added
* @return void
*/
void eventQueue::priorityAdd(event *value) {
node *temp, *temp1, *temp2;
temp = new node;
temp1 = new node;
temp2 = new node;
temp->data = value;
temp->arrTime = value->getArrTime(); // Priority
if (head == NULL || value->getArrTime() < head->arrTime) {
temp->next = head;
head = temp;
} else {
temp1 = head;
//q->link->priority <= priority)
// TODO: Solve infinite loop here
while (temp1->next != NULL && temp1->data->getArrTime() <= value->getArrTime()) {
// checks if the input value's time is less than the next and greater than the previous
if(temp->data->getArrTime() >= temp1->data->getArrTime()){
// path for setting a new tail
if(temp1->next == NULL){
temp->next = NULL;
temp1->next = temp;
tail = temp;
}
// path for inserting within the list
else{
temp2 = temp1->next;
temp->next = temp2;
temp1->next = temp;
}
break;
}
temp1 = temp1->next;
temp->next = temp1->next;
temp1->next = temp;
}
}
}
/* adds event to a certain position of the linked list
* @param pos desired pos to be added
* @param value event being added
* @return void
*/
void eventQueue::insert_position(int pos, event *value) {
node *pre;
node *cur;
node *temp;
pre = new node;
cur = new node;
temp = new node;
cur = head;
for (int i = 1; i < pos; ++i) {
pre = cur;
cur = cur->next;
}
temp->data = value;
pre->next = temp;
temp->next = cur;
}
/* removes head of the linked list
* @return void
*/
void eventQueue::delete_first() {
node *temp;
temp = head;
head = head->next;
delete temp;
}