Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Convert code into a priority queue #include #include #include queue.h Queue *init(){ Queue *toReturn = malloc(sizeof(Queue)); if(toReturn == NULL){ perror(error allocating queue); exit(1); } toReturn->head
Convert code into a priority queue #include#include #include "queue.h" Queue *init(){ Queue *toReturn = malloc(sizeof(Queue)); if(toReturn == NULL){ perror("error allocating queue"); exit(1); } toReturn->head = NULL; toReturn->tail = NULL; return toReturn; } void destroy(Queue *q){ while(!isEmpty(q)){ dequeue(q); } free(q); } void enqueue(Queue *q, int data){ //write this function } int dequeue(Queue *q){ if(isEmpty(q)){ fprintf(stderr,"tried dequeueing an empty queue! "); exit(0); } Node *toFree = q->head; int toReturn = q->head->data; if(q->head == q->tail){ //only one node in queue q->head = NULL; q->tail = NULL; }else{ q->head = q->head->next; } free(toFree); return toReturn; } int peek(Queue *q){ if(isEmpty(q)){ fprintf(stderr,"tried peeking an empty queue! "); exit(0); } return q->head->data; } int isEmpty(Queue *q){ return q->head == NULL; } void enqueue(Queue *q, int data) { Node *newNode = malloc(sizeof(Node)); if(newNode == NULL) { perror("error allocating node"); exit(1); } newNode->data = data; newNode->next = NULL; if(q->head == NULL) { q->head = newNode; q->tail = newNode; } else { q->tail->next = newNode; q->tail = newNode; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started