Deleting specific nodes in a linked list I am trying to implement a function in
ID: 3550945 • Letter: D
Question
Deleting specific nodes in a linked list
I am trying to implement a function in a linked list based priority queue where i find the node with the lowest value, return the value and then delete it from the linked list. This is all i have so far. As it is right now, it's not returning the right value. I think i'm in the right direction though. My main concern is being able to refer to the value's index.
class priorityQueueLL
{
private:
class node
{
public:
int data;
node* next;
};
node* pFront;
node* pRear;
int count;
public:
priorityQueueLL(); //constructor
//remove and return smallest item
int extractMin();
};
///////////////////////////////////////////////////THIS CODE HERE/////////////////////////////////////////////////////
priorityQueueLL::priorityQueueLL(){
count = 0;
pFront = pRear = NULL;
}
int priorityQueueLL::extractMin(){
int minValue = pFront->data;
node* pCurrent = pFront;
while (pCurrent != NULL)
{
pCurrent = pCurrent->next;
if (pCurrent->data < minValue)
{
pCurrent->data = minValue;
}
}
return minValue;
}
Explanation / Answer
Only one line (the commented line) has to be changed in the extractMin function, the corrected function is as follows:
int priorityQueueLL::extractMin(){
int minValue = pFront->data;
node* pCurrent = pFront;
while (pCurrent != NULL)
{
pCurrent = pCurrent->next;
if (pCurrent->data < minValue)
{
minValue=pCurrent->data; // this line u had written opposite
}
}
return minValue;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.