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.
- 09-14-2005 #1Just 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...
:?
- 09-14-2005 #2Linux 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
- 09-14-2005 #3Linux Newbie
- Join Date
- Oct 2004
- Posts
- 158
try qsort:
Code:#include <stdlib.h> #define MAXQ 100 int P_qCount=0; typedef struct { int requestNo; char ownerIP[40]; char fileName[30]; int fileSize; int priority; } printQueue; printQueue P_queue[MAXQ]; int compar(const void *A, const void *A) { printQueue *a=(printQueue *)A; printQueue *b=(printQueue *)B; if(a->priority > b->priority) return 1; if(a->priority < b->priority) return -1; return 0; } printQueue *mysort(printQueue *p) { qsort(p,P_qCount,sizeof(printQueue),compar); return p; }
- 09-16-2005 #4
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.
- 09-19-2005 #5Linux User
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
:o
Originally Posted by scientica
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


Reply With Quote
