How sort and do a list after giving struct?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DOSASIS09
    New Member
    • Feb 2010
    • 1

    How sort and do a list after giving struct?

    Hello, im not an inexpert guy programming and I need to do an application in c, that I declare using a structure 3or4 structures predefined. After that I need to sort this 3 or 4 strucutres “tasks” depends on the parameter of the priority in each structure. I would like to read the structures and then do a sort list with the ID of the task, but depending on the PRIORITY of each task. I don’t know how many pointers I should use.I understand how to sort a list with a array of an integer values…but I don’t know how to do this. Objective is sort at the begging that tasks, but later function sort should be able to add more times same task, during program execution. Any help, is welcome, I am lost...
    File.h
    typedef struct Task_Struct
    {
    unsigned char TASK_ID;
    unsigned char PRIORITY;
    unsigned char PREEMPTION;
    unsigned char STATUS;
    struct Task_Struct *next;
    };


    File.c
    #define NR_TASKS 3
    struct Task_Struct Task[NR_TASKS]

    define_t{

    Task[0].TASK_ID=1;
    Task[0].PRIORITY=3;
    Task[0].PREEMPTION=1;
    Task[0].STATUS=0;

    Task[1].TASK_ID=2;
    Task[1].PRIORITY=2;
    Task[1].PREEMPTION=1;
    Task[1].STATUS=0;

    Task[2].TASK_ID=3;
    Task[2].PRIORITY=4;
    Task[2].PREEMPTION=1;
    Task[2].STATUS=0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    If you have an array of structures then you should take a look at the qsort function in the Standard Library. However, this function won't work for you if instead you have a linked list of structures.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Your essential problem is writing a function that can compare two of your struct variables and determine which is less than the other.

      You have 4 keys: TASK_ID, PRIORITY, PREEMPTION, STATUS.

      I eould expect you could sort: ASCENDING by PRIORITY, DESCENDING by TASK_ID within PRIORITY, as an example.

      What you do is build a sort handle for each of your struct variables and then sort the handles rather than the struct variables.

      The sort handle would look like:

      Key1
      Sequence for Key 1
      Key 2
      Sequence for Key 2
      Key 3
      Sequence for Key 3
      Key 4
      Sequence for Key 4
      etc...

      In this case Key1 is the PRIORITY value of the struct variable. The Sequence for Key 1 is maybe 1 for ASCENDING and 0 for DESCENDING.
      The same for Key2. It has the TASK_ID of the struct variable and the sequence indicator for that Key.

      Now you create an array of sort handles that each contain a pointer to the original varible. All you have to do now is sort the handles.

      Make a pass and sort according to Key 1.

      Noae make a pass an sort by Key 2. Note that you sort by Key2 so long as Key 1 is unchanged. This is because Key2 is within Key1. When Key 1 chnages, you stop the pass. Then resume the pass so ong as the new Key1 value is unchnaged. Eventually, you will reach the end of the handles.

      Ditto for keys 3 and 4.

      I think you can use qsort and pass in the address of your comparison function along with the address of the array of sort handles.

      Comment

      Working...