Hi Guys
I am also stuck on this question.
write a program, pfpq.c, that maintains a priority queue of music tracks (songs) and ratings. The struct is given by:
Code:
struct track
{
char artist[20];
char title[20];
int rating;
};
The program is only designed to demonstrate the concept so get the user to
enter a small number of tracks and create a priority queue. (Use an empty
string title or an empty string artist to end the program). Print the priority
queue as it grows. Your program should initially ask the user if they wish to
order the priority queue by rating in ascending order, or by rating in descending
order, or if they wish to order the priority queue by artist name. Then build
the priority queue accordingly.
This is How much I have done, and I am stuck only in the last case, "if they wish to order the priority queue by artist name." which is case 3. How do I sort the priority queue by artist name using in the following code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pqueue.h"
struct track
{
char artist[20];
char title[20];
int rating;
};
void print_track(any i)
{
printf("(%s, %s, %i)",
((struct track*)i)->artist,
((struct track*)i)->title,
((struct track*)i)->rating);
}
int le_track(any i, any j)
{
return ((struct track*)i)->artist <= ((struct track*)j)->artist;
}
int mo_track(any i,any j)
{
return ((struct track*)i)->artist > ((struct track*)j)->artist;
}
int main(int argc, char **argv)
{
int choice,i;
char order;
struct track *tr;
pqueue *pf;
while(1)
{
printf("Enter your choice, In which order do you want the priority queue\n");
printf("1. Ascending Order\n");
printf("2. Descending Order\n");
printf("3. Artist Name\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
pf = new_bounded_pqueue(5,le_track);
for(i=0; i<5; i++) {
tr = (struct track*)malloc(sizeof(struct track));
printf("key data? "); scanf(" %s %s %i", tr->artist, tr->title, &(tr->rating));
pqueue_insert(pf,tr);
pqueue_print(pf,print_track);
printf("\n\n");
}
for(i=0; i<5; i++) {
pqueue_dequeue(pf);
pqueue_print(pf,print_track);
printf("\n\n");
}
break;
case 2:
pf = new_bounded_pqueue(5,mo_track);
for(i=0; i<5; i++) {
tr = (struct track*)malloc(sizeof(struct track));
printf("key data? "); scanf(" %s %s %i", tr->artist, tr->title, &(tr->rating));
pqueue_insert(pf,tr);
pqueue_print(pf,print_track);
printf("\n\n");
}
for(i=0; i<5; i++) {
pqueue_dequeue(pf);
pqueue_print(pf,print_track);
printf("\n\n");
}
break;
case 3:
default:
printf("SWITCH COMPLETED");
}
}
}
This is the library file included pqueue.h:
Code:
#ifndef PQUEUE_H
#define PQUEUE_H
#include "any.h"
typedef struct pqueue_implementation pqueue;
typedef int (*le_fun)(any a, any b);
pqueue * new_bounded_pqueue(int max, le_fun le);
pqueue * new_unbounded_pqueue(le_fun le);
int pqueue_isempty(pqueue *q);
int pqueue_isfull(pqueue *q);
int pqueue_size(pqueue *q);
void pqueue_insert(pqueue *q, any x);
any pqueue_dequeue(pqueue *q);
any pqueue_front(pqueue *q);
void pqueue_print(pqueue *q, void (* item_print)(any item));
void pqueue_release(pqueue *q);
#endif
Your help is much appreciated.