Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write iterative function 5. Priority Queue (4 points) Suppose we define a priori

ID: 3855916 • Letter: W

Question


write iterative function

5. Priority Queue (4 points) Suppose we define a priority queue using a circular linked list as follows: typedef struct f int info int priority struct node *pNext / Linked list node/ /info stored in this node /priority stored in this node / Pointer to the next node in linked list * t node typedef struct /*Priority queue / /*Pointer to the last node in the LL /*Its pNext pointer points to the front of the LL / node *pRear; pq Write an iterative function to find and return a pointer to the first node in the priority queue with a info value equal to the parameter val. return back to pRear node* find ( pq queue, int val )f

Explanation / Answer

Here is the code for the function:

node* find(pq queue, int val)

{

node* ptr //pointer to traverse the circular linked list

int flag=0; //to keep check if the desired element was found or not

ptr=pq.pRear; //assigning the first element to our pointer variable

if(ptr->info==val)

flag=1; //element found at the first check itself

else

{

ptr=ptr->pNext; /*now start traversing the list if first element is not desired element, till you find the element or the list ends, that is ptr again becomes equal to pRear*/

while(ptr!=pq->pRear)

{

if(ptr->info==val)

{

flag=1; //element found

break; //now end loop

}

else

ptr=ptr->pNext; //go to next element

}

}

if(flag==1)

return ptr; //the element pointer

else

return NULL;

}