473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2340
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.l earn.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.0000 00
node -> scores.history is 1558316800.0000 00
node -> scores.history is 10910205952.000 000
node -> scores.history is 11304023040.000 000
node -> scores.history is 9889975296.0000 00
node -> scores.history is 5643980288.0000 00
node -> scores.history is 10205063168.000 000
node -> scores.history is 4162190592.0000 00
node -> scores.history is 9174709248.0000 00
node -> scores.history is 970891072.00000 0
node -> scores.history is 11570582528.000 000
node -> scores.history is 2910468608.0000 00
node -> scores.history is 3875984384.0000 00
node -> scores.history is 8482416128.0000 00
node -> scores.history is 4301542400.0000 00
node -> scores.history is 25585348.000000
node -> scores.history is 13060592640.000 000

Sorted order
node -> scores.history is 25585348.000000
node -> scores.history is 970891072.00000 0
node -> scores.history is 1558316800.0000 00
node -> scores.history is 2910468608.0000 00
node -> scores.history is 3875984384.0000 00
node -> scores.history is 4162190592.0000 00
node -> scores.history is 4301542400.0000 00
node -> scores.history is 4797963776.0000 00
node -> scores.history is 5643980288.0000 00
node -> scores.history is 8482416128.0000 00
node -> scores.history is 9174709248.0000 00
node -> scores.history is 9889975296.0000 00
node -> scores.history is 10205063168.000 000
node -> scores.history is 10910205952.000 000
node -> scores.history is 11304023040.000 000
node -> scores.history is 11570582528.000 000
node -> scores.history is 13060592640.000 000

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_ty pe *);

int main(void)
{
n_type *list;

list = l_make(NODES);
if (list == NULL) {
fputs("The list was not allocated.\n", stderr);
exit(EXIT_FAILU RE);
}
puts("BEGIN output from n_sort.c\n\nRan dom 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_ty pe *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
2589
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 DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, in Window2K server"). First of all, here are the two methods I have used in order to apply the sorting property to the DataView 1. Simply defining the sort order and colum DataView...
1
1730
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 on D table or R view individually, but some fields take a long time to sort even they are on non-cluster indexes. I can not sort on any field in table E, K, and L at all. Also I tried to save this query as a view in the local server. SQL Server...
8
3264
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 those words positioned according to my costume sorting order. Thank you for your help, Dilan
8
3539
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
2
1721
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, secured referential integrity). There´s one (probably more) report that doesn´t get the new name:
1
993
by: Nikolay Petrov | last post by:
Is it possible to sort datagrid from code and how? Tnx in advance
4
3159
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 hundreds (perhaps even an infinite loop...I have to break the system to stop the report generating) of pages. While I'm not 100% I think this may have something to do with the detail
1
2538
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 to a UNIQUE UPC. Youcan have multiple UPC's to each primary key from product. I need to sort PRODUCT so that it is sorted by product name, while also
7
4829
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
1
2183
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 (LoginName,UserName) 2. the datagridview sorting is set to automatic, so when i click on the column header is sorts well. 3. i put in an event handler for the CellEndEdit Event, so whenever the user of the program changes the content of a cell in the LoginName...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.