Find the answer to your Linux question:
Results 1 to 5 of 5
C language #define MAXQ 100 typedef struct { int requestNo; char ownerIP[40]; char fileName[30]; int fileSize; int priority; } printQueue; printQueue P_queue[MAXQ]; int P_qCount; //counter used to store the number ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Location
    Sri Lanka
    Posts
    4

    NEED HELP >> How Do I Sort a Structure Array???? C language


    C language

    #define MAXQ 100

    typedef struct
    {
    int requestNo;
    char ownerIP[40];
    char fileName[30];
    int fileSize;
    int priority;
    } printQueue;

    printQueue P_queue[MAXQ];

    int P_qCount; //counter used to store the number of srtucture eliments int the array


    I need to sort the array 'P_queue' by P_queue->priority in assending order.
    How do I do this.
    Please post any solution.

    This is Very urgent...
    :?

  2. #2
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Sorry, no code...
    But, this is what you can do....

    What you can do is maintain an additional array as an indexing structure.
    This array would hold the indices of the main structure array in the proper sorted order of priority.

    What I'm trying here is to eliminate the overhead of the swapping of the struct elements in the main array - which would definitely outweigh the main algo overheads.

    The only additional overhead now is that of the indirection from the index array into the main array. But sorting is now simplified and any algo that is optimized for sorting an array of integers can be used.

    However, the most logical solution for a general print queue would be to use a doubly-linked list as a queue (FIFO) structure rather than an array.
    The Unforgiven
    Registered Linux User #358564

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    try qsort:


    Code:
    #include <stdlib.h>
    #define MAXQ 100 
    
    int P_qCount=0;
    
    typedef struct 
    &#123; 
    int requestNo; 
    char ownerIP&#91;40&#93;; 
    char fileName&#91;30&#93;; 
    int fileSize; 
    int priority; 
    &#125; printQueue; 
    
    printQueue P_queue&#91;MAXQ&#93;; 
    
    int compar&#40;const void *A, const void *A&#41;
    &#123;
        printQueue *a=&#40;printQueue *&#41;A;
        printQueue *b=&#40;printQueue *&#41;B;    
        if&#40;a->priority > b->priority&#41; return 1;
        if&#40;a->priority < b->priority&#41; return -1;
        return 0;
    &#125;
    
    printQueue *mysort&#40;printQueue *p&#41;
    &#123;
    	qsort&#40;p,P_qCount,sizeof&#40;printQueue&#41;,compar&#41;;
    	return p;
    &#125;

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast scientica's Avatar
    Join Date
    Sep 2003
    Location
    South- or "Mid-" Sweden
    Posts
    742
    hmm... I think you might find the solution on this page:
    http://www.linuxforums.org/rules.php

    under the section "Dont give us YOUR homework questions", also, for aditional reading, please read "Language" under "Forum signatures".
    Regards Scienitca (registered user #335819 - http://counter.li.org )
    --
    A master is nothing more than a student who knows something of which he can teach to other students.

  6. #5
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by scientica
    also, for aditional reading, please read "Language" under "Forum signatures".
    :o
    As far as I understand, his signature is nothing but his name in plain english - it's not an alien language....
    The Unforgiven
    Registered Linux User #358564

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •