473,408 Members | 2,399 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,408 software developers and data experts.

Problem with sorting linked-list

hi, i have to build a linked-list which has another sturcture _score
as it's data entry, so how can i sort such linked-list based on, let say,
history score into proper order (descending/ascending). thanx for your help !

struct _score {
float literature;
float history;
float sociology;
};

struct _node {
struct _score scores;
struct _node *next;
};
Nov 14 '05 #1
3 2305
sugaray wrote:
hi, i have to build a linked-list which has another sturcture _score
as it's data entry, so how can i sort such linked-list based on, let say,
history score into proper order (descending/ascending). thanx for your help !

struct _score {
float literature;
float history;
float sociology;
};

struct _node {
struct _score scores;
struct _node *next;
};


Pass a pointer to your sorting function that compares
two "scores" and returns true if the first should come
before the second.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #2
sugaray wrote:
hi, i have to build a linked-list which has another sturcture _score
as it's data entry, so how can i sort such linked-list based on, let say,
history score into proper order (descending/ascending). thanx for your help !

struct _score {
float literature;
float history;
float sociology;
};

struct _node {
struct _score scores;
struct _node *next;
};


Merge sort is, IMHO, the most attractive method for
sorting linked lists. The Standard C library doesn't
provide an implementation, but it's easy to write.

--
Er*********@sun.com

Nov 14 '05 #3
sugaray wrote:

hi, i have to build a linked-list which has another sturcture _score
as it's data entry, so how can i sort such linked-list based on, let say,
history score into proper order (descending/ascending). thanx for your help !

struct _score {
float literature;
float history;
float sociology;
};

struct _node {
struct _score scores;
struct _node *next;
};


BEGIN output from n_sort.c

Random order
node -> scores.history is 4797963776.000000
node -> scores.history is 1558316800.000000
node -> scores.history is 10910205952.000000
node -> scores.history is 11304023040.000000
node -> scores.history is 9889975296.000000
node -> scores.history is 5643980288.000000
node -> scores.history is 10205063168.000000
node -> scores.history is 4162190592.000000
node -> scores.history is 9174709248.000000
node -> scores.history is 970891072.000000
node -> scores.history is 11570582528.000000
node -> scores.history is 2910468608.000000
node -> scores.history is 3875984384.000000
node -> scores.history is 8482416128.000000
node -> scores.history is 4301542400.000000
node -> scores.history is 25585348.000000
node -> scores.history is 13060592640.000000

Sorted order
node -> scores.history is 25585348.000000
node -> scores.history is 970891072.000000
node -> scores.history is 1558316800.000000
node -> scores.history is 2910468608.000000
node -> scores.history is 3875984384.000000
node -> scores.history is 4162190592.000000
node -> scores.history is 4301542400.000000
node -> scores.history is 4797963776.000000
node -> scores.history is 5643980288.000000
node -> scores.history is 8482416128.000000
node -> scores.history is 9174709248.000000
node -> scores.history is 9889975296.000000
node -> scores.history is 10205063168.000000
node -> scores.history is 10910205952.000000
node -> scores.history is 11304023040.000000
node -> scores.history is 11570582528.000000
node -> scores.history is 13060592640.000000

END output from n_sort.c
/* BEGIN n_sort.c */

#include <stdio.h>
#include <stdlib.h>

#define NODES 17
#define N_TYPE \
struct node { \
struct node *next; \
struct score scores;\
}
#define GT(A, B) ((A) -> scores.history > (B) -> scores.history)
#define NEXT next
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffff)
#define str(s) # s
#define xstr(s) str(s)

struct score {
float literature;
float history;
float sociology;
};
typedef N_TYPE n_type;

void l_free(n_type *);
n_type *l_make(size_t);
void l_init(n_type *, long unsigned);
void l_print(n_type *);
n_type *n_sort(n_type *, size_t);
n_type *list_sort(n_type *);

int main(void)
{
n_type *list;

list = l_make(NODES);
if (list == NULL) {
fputs("The list was not allocated.\n", stderr);
exit(EXIT_FAILURE);
}
puts("BEGIN output from n_sort.c\n\nRandom order");
l_init(list, LU_RAND_SEED);
l_print(list);
puts("Sorted order");
list = list_sort(list);
l_print(list);
puts("END output from n_sort.c\n");
l_free(list);
return 0;
}

void l_free(n_type *node)
{
n_type *next;

do {
next = node -> NEXT;
free(node);
node = next;
} while (node != NULL);
}

n_type *l_make(size_t count)
{
n_type *node, *list;

list = count ? malloc(sizeof *list) : NULL;
if (list != NULL) {
node = list;
while (--count != 0) {
node -> NEXT = malloc(sizeof *node -> NEXT);
if (node -> NEXT == NULL) {
l_free(list);
return NULL;
} else {
node = node -> NEXT;
}
}
node -> NEXT = NULL;
}
return list;
}

void l_init(n_type *node, long unsigned seed)
{
do {
seed = LU_RAND(seed);
node -> scores.history = 3.14159265f * seed;
node = node -> NEXT;
} while (node != NULL);
}

void l_print(n_type *node)
{
do {
printf("node -> scores.history is %f\n",
node -> scores.history);
node = node -> NEXT;
} while (node != NULL);
putchar('\n');
}

n_type *list_sort(n_type *list)
{
n_type *node;
size_t count;

count = 1;
node = list -> NEXT;
while (node != NULL) {
++count;
node = node -> NEXT;
}
return n_sort(list, count);
}

n_type *n_sort(n_type *list, size_t count)
{
size_t half, other_half;
n_type *head, *tail, *sorted, **node;

if (count > 1) {
head = list;
other_half = half = count / 2;
while (--other_half != 0) {
head = head -> NEXT;
}
tail = head -> NEXT;
head -> NEXT = NULL;
tail = n_sort(tail, count - half);
head = n_sort(list, half);
node = GT(head, tail) ? &tail : &head;
sorted = list = *node;
*node = sorted -> NEXT;
while (*node != NULL) {
node = GT(head, tail) ? &tail : &head;
sorted = sorted -> NEXT = *node;
*node = sorted -> NEXT;
}
sorted -> NEXT = head != NULL ? head : tail;
}
return list;
}

/* END n_sort.c */

--
pete
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
1
by: js | last post by:
I am using SQL Server 2000. I am getting the following error when executing the following query. The query joins a view .dbx.dbo.vwreports that resides on a linked server. I can sort on fields...
8
by: D. Roshani | last post by:
Hello Do you have any idea how I can create a Macro in Access which can do costume alphabetic sorting of only one column with foreign words (ISO-8859-1), I would like to define how I want...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
2
by: Florifulgurator | last post by:
Hello, trying to clean up other´s mess... I´ve replaced some tables (previously linked to another Access DB) with renamed tables linked to a PostgeSQL DB (removed spaces and Ümlauts in names,...
1
by: Nikolay Petrov | last post by:
Is it possible to sort datagrid from code and how? Tnx in advance
4
by: Mal Reeve | last post by:
Hello, I have a report that has only 2 levels of grouping. The detail section is simply 1 large block for a memo field. I am finding that on some occasions the report errors and generates...
1
by: Jeweladdict | last post by:
I have 2 linked tables, The first table (PRODUCT) has product name, pricing, size, etc with an autonumber primary key. The second table is a UPC table with the primary key from PRODUCT linked...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.