C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2013, 06:36 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
Sorting a linked list

i have a linked list of words, and i need to sort it alphabetically.
what whould you say the best way to do it?

Reply With Quote
  #2  
Old January 11th, 2013, 10:42 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 35 m 19 sec
Reputation Power: 1974
Two ways come immediately to mind.

1. Sort the actual list. This would involve a swap function that would take two nodes and swap their positions in the list. Of course, you would not be physically moving the nodes, but rather just manipulating the pointers.

2. Create a second index list. Each node in this index list would point to a node in the list you're sorting. Then you sort the index list. An advantage to index lists is that you could create more than one sorting based on different fields as well as to filter out some of the nodes in a kind of query. The parallels with working with databases should be apparent.

3. Create a second sorted list. Read through the original list and insert each node into the new list; you could either create a copy of each node or actually remove it from the original list and insert it into the sorted list -- the latter may be more of what you have in mind. At the end, the original list will no longer exist, so you can simply attach the new list to the original head pointer. It's kind of like adding or removing text from a text file: you open the original file for input and a second file for output, copy the original file to it inserting or deleting what you need to in the process, close the files, delete the original file, and rename the second file to the original's name.

You can use any number of sorting techniques, such as the insertion sort or the bucket sort (separate lists for each initial letter, a-z, each of which you insertion sort, then at the end concatenate the separate lists together; this lessens the search time for the insertion sorts because you're working with shorter lists) or whatever else catches your fancy -- in school, I loved to use sort trees, which is a form of insertion sort but based on a binary search instead of a linear one.

Those are what come immediately to mind off the top of my head. Off-hand, I think that the first may prove rather tricky, the second might not actually do what you want (but keep it in the back of your head for future projects), and the third seems like it would be easier to implement.

Reply With Quote
  #3  
Old January 11th, 2013, 11:04 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
Wow, thanks!
that was really helpful.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sorting a linked list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap