Thread: Priority Queues C problem

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    9

    Priority Queues C problem

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To compare strings, you need to use strcmp (assuming you want alphabetical, or at least ASCIIbetical -- if you need case insensitive, you'll have to work a little harder).

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    @tabstop: Someone else also said strcmp, but I am stuck at how to implement a strcm to do the job in this code.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In this instance, "implementing" strcmp involves hitting those six keys on your keyboard, in that order. Depending on what "any" actually works out to be as a type, you could use strcmp as the second parameter to new_bounded_pqueue as it stands.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    so I include #incude <string.h>

    and make a strcmp function,

    and then I use
    pf=new_bounded_pqueue(5,strcmp), just like the mo_track and le_track functions

    But How do I make a strcmp function like that.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    ok i made this function, for strcmp
    do you think this is correct

    Code:
    int strcmp(any i,any j){
       strcpy(((struct track*)i)->artist , "abcdef");
       strcpy(((struct track*)j)->artist, "ABCDEF");
    
    
       int ret = strcmp(((struct track*)i)->artist, ((struct track*)j)->artist);
    
    
       if(ret > 0)
       {
          return ((struct track*)j)->artist;
       }
       else if(ret < 0) 
       {
          return ((struct track*)i)->artist
       }
       else 
       {
          return ((struct track*)i)->artist,((struct track*)j)->artist
       }
       
       return(0);
    }
    
    Please let me know where I have gone wrong

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    I compiled it,
    the compiler says
    pfpq.c:21:5: error: conflicting types for ‘strcmp’
    pfpq.c: In function ‘strcmp’:
    pfpq.c:29:7: warning: return makes integer from pointer without a cast [enabled by default]
    pfpq.c:33:7: warning: return makes integer from pointer without a cast [enabled by default]
    pfpq.c:37:7: warning: return makes integer from pointer without a cast [enabled by default]

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Cprogrammes View Post
    But How do I make a strcmp function like that.
    You don't. strcmp is a builtin in the C library.

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    yeh your right , How did I forget that. but dont I need to add strcmp(any i, any j) like the le_track function. or does it return that the artist name automatically.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    strcmp() returns a value < 0, == 0, or > 0 depending if string1 is <, =, or > than string 2. You can create a function that calls strcmp() if you need a different type of return.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    I search for sorting alphabetically , I found the following function for strcmp
    Code:
    void sort_by_name(){
        int i, didSwap = 1, limit = numitems - 1;
        patient *temp;
    
        while (didSwap) {
            didSwap = 0;
            for (i = 0; i < limit; i++) {
                if (strcmp (pRecords[i]->name, pRecords[i+1]->name) > 0) {
                    temp          = pRecords[i];
                    pRecords[i]   = pRecords[i+1];
                    pRecords[i+1] = temp;
                    didSwap = 1;
                }
            }
            limit--;
        }
    }
    Will something like this work ? If yes what do I need to change in the above function .
    Please help

  12. #12
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    Guys please help!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Priority Queues
    By DeeMan in forum C Programming
    Replies: 4
    Last Post: 02-26-2013, 02:54 AM
  2. Priority queues
    By sigur47 in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2012, 07:22 PM
  3. Priority Queues
    By Korhedron in forum C++ Programming
    Replies: 10
    Last Post: 07-20-2010, 10:32 AM
  4. Applications of priority queues
    By C_Enthusiast in forum C Programming
    Replies: 2
    Last Post: 06-21-2010, 09:07 AM
  5. BST implementation of priority queues
    By jcleong in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 09:09 AM