#include "pqueue.h"   #include   #include "string.h"   #include   typedef char *string;   typedef double priority; //priority here is double   //There are some errors to be improved. I have already highlighted out. If there is any other errors, please tell points out   typedef struct node {   string data;   priority prior;   struct node* next;   } NodeT;   typedef struct PriorityQueueRepr{   NodeT* top;   int element; //number of element in queue   }PriorityQueueRepr;       pqueue pqueue_create() {   pqueue pq = (pqueue)malloc(sizeof(PriorityQueueRepr));   if (pq == NULL) {   return NULL;   }   pq->element = 0;   pq->top = NULL;   return pq;   }   void pqueue_destroy(pqueue pq) {   while (!pqueue_empty(pq)) {   string s = pqueue_leave(pq);   free(s);   }   free(pq);   }     void pqueue_join(pqueue pq, string dat, priority prio) {   NodeT* newNode = (NodeT*)malloc(sizeof(NodeT));   newNode->data = (string)malloc(strlen(dat) + 1);   //error is here how to improve it   newNode->prior = (priority)malloc(sizeof(prio) + 1);   strcpy(newNode->data, dat);   newNode->next = NULL;   // If the queue is empty, add the new node as the first element.   if (pq->top == NULL) {   pq->top = newNode;   }   else {   NodeT* current = pq->top;   NodeT* previous = NULL;   // Find the position for the new node based on its priority.   //error is here   while (current != NULL && strcmp(current->prior, prio) < 0) {   previous = current;   current = current->next;   }   // If the new node should be at the beginning of the queue.   if (previous == NULL) {   newNode->next = pq->top;   pq->top = newNode;   }   else {   newNode->next = current;   previous->next = newNode;   }   }   pq->element++;   }   string pqueue_leave(pqueue pq) {   if (pqueue_empty(pq)) {   return NULL;   }   NodeT *temp = pq->top;   string data = temp->data;   pq->top = temp->next;   //error is here   free(temp->prior);   free(temp);   pq->element--;   return data;     }   string pqueue_peek(pqueue pq) {   if (pqueue_empty(pq)) {   return NULL;   }   return pq->top->data;   }   bool pqueue_empty(pqueue pq) {   return pq->top == NULL;   }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

#include "pqueue.h"

 

#include <assert.h>

 

#include "string.h"

 

#include <stdlib.h>

 

typedef char *string;

 

typedef double priority; //priority here is double

 

//There are some errors to be improved. I have already highlighted out. If there is any other errors, please tell points out

 

typedef struct node {

 

string data;

 

priority prior;

 

struct node* next;

 

} NodeT;

 

typedef struct PriorityQueueRepr{

 

NodeT* top;

 

int element; //number of element in queue

 

}PriorityQueueRepr;

 

 

 

pqueue pqueue_create() {

 

pqueue pq = (pqueue)malloc(sizeof(PriorityQueueRepr));

 

if (pq == NULL) {

 

return NULL;

 

}

 

pq->element = 0;

 

pq->top = NULL;

 

return pq;

 

}

 

void pqueue_destroy(pqueue pq) {

 

while (!pqueue_empty(pq)) {

 

string s = pqueue_leave(pq);

 

free(s);

 

}

 

free(pq);

 

}

 

 

void pqueue_join(pqueue pq, string dat, priority prio) {

 

NodeT* newNode = (NodeT*)malloc(sizeof(NodeT));

 

newNode->data = (string)malloc(strlen(dat) + 1);

 

//error is here how to improve it

 

newNode->prior = (priority)malloc(sizeof(prio) + 1);

 

strcpy(newNode->data, dat);

 

newNode->next = NULL;

 

// If the queue is empty, add the new node as the first element.

 

if (pq->top == NULL) {

 

pq->top = newNode;

 

}

 

else {

 

NodeT* current = pq->top;

 

NodeT* previous = NULL;

 

// Find the position for the new node based on its priority.

 

//error is here

 

while (current != NULL && strcmp(current->prior, prio) < 0) {

 

previous = current;

 

current = current->next;

 

}

 

// If the new node should be at the beginning of the queue.

 

if (previous == NULL) {

 

newNode->next = pq->top;

 

pq->top = newNode;

 

}

 

else {

 

newNode->next = current;

 

previous->next = newNode;

 

}

 

}

 

pq->element++;

 

}

 

string pqueue_leave(pqueue pq) {

 

if (pqueue_empty(pq)) {

 

return NULL;

 

}

 

NodeT *temp = pq->top;

 

string data = temp->data;

 

pq->top = temp->next;

 

//error is here

 

free(temp->prior);

 

free(temp);

 

pq->element--;

 

return data;

 

 

}

 

string pqueue_peek(pqueue pq) {

 

if (pqueue_empty(pq)) {

 

return NULL;

 

}

 

return pq->top->data;

 

}

 

bool pqueue_empty(pqueue pq) {

 

return pq->top == NULL;

 

 

 

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education