Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions