Link list help

KOOL-AID

I Pitty Da Fool
Supporter
Joined
Aug 12, 2013
Messages
24,747
Reputation
16,358
Daps
69,937
need to sort link lists in descending order (greatest to least), from a input file. each entry represents a link list and the ordering should be based on their priority variable

typedef struct Student{
char* firstName;
char* lastName;
int priority;
int readingLevel;
struct Student* next;
}student;

the file is basically:
firstname, lastname, digit for priority level, digit for reading level.
ex-koolaid man 4 8
assume we dnt have a set number of entries, how would i go about sorting this. i saw that the merge is the best for link lists.
@gabbo @Mowgli @Matt504 @houston911 @SheWantTheD @The Wave etc.
 

Regular Developer

Supporter
Joined
Jun 2, 2012
Messages
9,176
Reputation
2,337
Daps
26,255
Reppin
NJ
need to sort link lists in descending order (greatest to least), from a input file. each entry represents a link list and the ordering should be based on their priority variable

typedef struct Student{
char* firstName;
char* lastName;
int priority;
int readingLevel;
struct Student* next;
}student;

the file is basically:
firstname, lastname, digit for priority level, digit for reading level.
ex-koolaid man 4 8
assume we dnt have a set number of entries, how would i go about sorting this. i saw that the merge is the best for link lists.
@gabbo @Mowgli @Matt504 @houston911 @SheWantTheD @The Wave etc.
If you don't know the length of the list, I think it would be hard to use a merge sort...

Have you tried drawing it out?
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,852
Reputation
25,540
Daps
132,000
I might be misunderstanding, but you're reading from a file and populating the list from scratch? If that's the case you shouldn't need merge sort, you'd just need to write your insertion method to where it inserts according to priority. Worst case on that is O(n), at least it should be.
 
Joined
Aug 9, 2014
Messages
424
Reputation
180
Daps
1,861
Do your googles for merge sorting a linked list. You'll have to have a function that "splits" the linked list. Essentially the split will return two pointers. The head of the linked list and the middle. The middle is returned by using the slow and fast pointer technique. After that it's pretty similar to a merge sort with an array.
 

Regular Developer

Supporter
Joined
Jun 2, 2012
Messages
9,176
Reputation
2,337
Daps
26,255
Reppin
NJ
Do your googles for merge sorting a linked list. You'll have to have a function that "splits" the linked list. Essentially the split will return two pointers. The head of the linked list and the middle. The middle is returned by using the slow and fast pointer technique. After that it's pretty similar to a merge sort with an array.
I never learned about the fast/slow pointer technique (or maybe I forgot all about that, lol). That shyt is clever, lol.
 
Top