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

Sorting a linked list

Hi,

in my application, I defined a linked list just as follows:

typedef struct _MYLIST{
int myval;
void *next;
}MYLIST;

MYLIST head;

So, the problem is the following: the value 'myval' located in each item of
a filled linked list contains a specific value - but not in the correct
order. so, what's the fastest way to sort the items in the list ascending by
their values in 'myval'.
How can this be done?

thanks a lot
Peter
Nov 17 '05 #1
4 2042
Peter Schmitz wrote:
Hi,

in my application, I defined a linked list just as follows:

typedef struct _MYLIST{
int myval;
void *next;
}MYLIST;

MYLIST head;

So, the problem is the following: the value 'myval' located in each
item of a filled linked list contains a specific value - but not in
the correct order. so, what's the fastest way to sort the items in
the list ascending by their values in 'myval'.
How can this be done?


#include <list>

std::list<int> mylist;

// insert items into mylist.

mylist.sort();

If you're dead-set against using the C++ standard library, to sort a linked
list you basically need to use the tehcniques that were used to sort files
on tapes in decades past. Heapsort and shellsort were both important
algorithms in that era.

Another technique is to insert pointers to your list intems into an array
(or vector), then sort that vector using an appropriate indirection in the
comparison step, then use the sorted vector to rebuild the linked list in
the desired order.

Yet another strategy is to never allow the list to be out of order - always
find the correct in-order insertion point as each item is added to the list.

-cd
Nov 17 '05 #2
Sun. Sep. 12, 2004 10:35 AM PT

Instead of sorting a list, specially, yours is a Single Linked List, then I
will suggest that you insert elements in the list in Sorted. Means, keep
your list in order while you insert an element, then Binary search will help
you to find a particular element. If you want me to give an algo. How to
insert elements in Order, pl. let me know.

Good Luck!

"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
Hi,

in my application, I defined a linked list just as follows:

typedef struct _MYLIST{
int myval;
void *next;
}MYLIST;

MYLIST head;

So, the problem is the following: the value 'myval' located in each item of a filled linked list contains a specific value - but not in the correct
order. so, what's the fastest way to sort the items in the list ascending by their values in 'myval'.
How can this be done?

thanks a lot
Peter

Nov 17 '05 #3
Carl Daniel [VC++ MVP] wrote:
Peter Schmitz wrote:
in my application, I defined a linked list just as follows:

typedef struct _MYLIST{
int myval;
void *next;
}MYLIST;

MYLIST head;

So, the problem is the following: the value 'myval' located in each
item of a filled linked list contains a specific value - but not in
the correct order. so, what's the fastest way to sort the items in
the list ascending by their values in 'myval'.
How can this be done?

#include <list>

std::list<int> mylist;

// insert items into mylist.

mylist.sort();


Since the data is just an int, std::vector<int> might be a better
container. std::set<int> might even work well depending on what is
being done with the data.

But in general I agree with Carl. Don't create your own linked list (or
any other container) unless there really is no other alternative.
If you're dead-set against using the C++ standard library, to sort a linked
list you basically need to use the tehcniques that were used to sort files
on tapes in decades past. Heapsort and shellsort were both important
algorithms in that era.


If I remember correctly, heapsort and shellsort both require swapping
and random access, which are problematic for singly linked lists. The
best sort algorithm for a singly linked list is merge sort.
--
David Olsen
qg********@yahoo.com
Nov 17 '05 #4
"David Olsen" <qg********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
If I remember correctly, heapsort and shellsort both require swapping
and random access, which are problematic for singly linked lists. The
best sort algorithm for a singly linked list is merge sort.


Actually, QuickSort is completely implementable on a Singly-linked list.
The only problem is that you'd need to use the first item on each sublist as
the pivot, so it may succumb to the "nearly-sorted" flaw.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 17 '05 #5

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

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
3
by: darth | last post by:
Does anyone have heap sorting with queue(doublely linked list) in C? I have heap sort with array but with queue, it's somewhat hard.
3
by: sugaray | last post by:
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...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
1
by: PhilB | last post by:
I have been having issues trying to merge sort a double linked list. (I would supply the code, but it is locked away on an inaccessable computer.) The list always results in an unsorted list, but...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.