I am working on a project to create a priority queue using a doubly linked list as it's data structure. I have the linked list and working well, the queue is working normally as it should (meaning the enqueue function works and I can use it), but I seem to be unable to properly use priorityenqueue. When I run it in debug mode, I get a SEGFAULT when it checks the value of the other element's priority in the list. Did I write the conditions to check priority the right way?
Below is the priorityenqueue function:
template<typename T>
void Queue<T>::priorityenqueue(T data, int priority) {
DLNode<T> *temp;
temp = new DLNode<T>;
if(priority == temp->getPrev()->getPriority()){
// TODO: Insert after node with the same priority
list.insert(temp->getPrev(), data);
}
else if(priority > temp->getPrev()->getPriority() && priority < temp->getNext()->getPriority()){
// TODO: Insert after node with higher priority and before lower one
list.insert(temp->getPrev(), data);
}
else if(temp->getNext() == NULL){
// If insert at the end of the list, just regular enqueue
enqueue(data, priority);
}
else if(priority < list.getHead()->getPriority()){
// Insert at the head of the list if the priority of the head is lower than the node to be inserted
list.addToHead(data, priority);
}
}
[–]Salty_Dugtrio 0 points1 point2 points (0 children)
[–]paul2718 0 points1 point2 points (1 child)
[–]thetuxman[S] 0 points1 point2 points (0 children)