473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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;
}
Feb 4 '10 #1
2 2613
donbock
2,426 Expert 2GB
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.
Feb 4 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 6 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joel | last post by:
I am having some problems compiling my code on Mandrake 10 with g++ (GCC 3.3.2). The problem seems to be in that I try to define a functor that compares two pointer objects, and use that functor to...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
2
by: Foodbank | last post by:
Hi everyone, I'm having trouble implementing a supposedly simple Least Significant Digit first Radix Sort. The book I'm using gave me somewhat of a template that I touched up (below), but I'm...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
9
by: incredible | last post by:
how to sort link list of string
5
by: Deadlocker | last post by:
Hallo, Ive already found an answer : http://www.thescripts.com/forum/thread59807.html But it doesn't work.. here is the error message I get : C:\Dokumente und Einstellungen\piouf.cpp(121)...
9
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list that is populated with a structure. I would like to be able to sort on values in the structure. I have public struct mergoutstruct { public Int32 projectnumber; public...
5
by: tienlx | last post by:
Hi all, I want to sort an array of struct _line: .... typedef struct _line { int x1; int x2; } line;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.