Advertisement

03.10.2004 at 07:22PM PST, ID: 20914849
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

sorting a linked list
Tags: list, linked, sort
Hey guys...  I need a way to sort a linked list into decending order.  

i have:

typedef struct person{
char name[20];
int age;
char sex;
int birthday;
struct person *next;
}p;

I want to sort this linked list from the oldest to youngest.  Please help out any way possible thanks =)
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: iamnamja
Solution Provided By: ankuratvb
Participating Experts: 5
Solution Grade: A
Views: 388
Translate:
Loading Advertisement...
03.10.2004 at 08:01PM PST, ID: 10567574

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 08:31PM PST, ID: 10567679

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.10.2004 at 09:05PM PST, ID: 10567845

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.11.2004 at 05:19AM PST, ID: 10570847

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.11.2004 at 10:43AM PST, ID: 10574043

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.01.2004 at 08:21PM PST, ID: 12469989

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.10.2004 at 08:01PM PST, ID: 10567574

Rank: Master

It's very simple.
Decide which sorting technique you want to use.
Bubble sort is the most simple one.
Also decide on which KEY you want to sort. (If you mean by the statement "oldest to youngest", the key as age, then use this in comparison)
Then start two loops one inside the other.
Use a pointer to traverse the list. In the first list, pointer starts from the begining of the list and goes till end.
In the second loop, pointer starts from where the current pointer of first loop is present and goes till end.
Inside the innermost loop compare the keys of 2 immedieate nodes. If the first one is less than the 2nd one, then exchange the contents of the nodes.
By the time you come out of both the loops, your link will be sorted.

Here is a step by step algorithm, which you can extend to code:
1. p *ptr1, *ptr2.
2. ptr1 = ptr2 = head of linked list.
3. While (ptr1 != NULL)
4. Do
5. ptr2 = ptr1
6. While (ptr2 != NULL)
7. Do
8. if (ptr1->age < ptr2->age) exchange the data stored in ptr1 and ptr2.
9. ptr2 = ptr2->next.
10. End of While //Inner loop
11. ptr1 = ptr1->next
12. End of While // Outer loop.
//Here the list is sorted.

-ssnkumar
Assisted Solution
 
03.10.2004 at 08:31PM PST, ID: 10567679

Rank: Guru

Hi,

http://www.funducode.com/freec/datastructure/datastruct6/datastruct6.htm

This page explains sorting linked lists and has the code as well.

Since u need ur list sorted in descending order,in the code given in the link just change the
'>' sign to '<' where the data in the nodes is being compared.

Also,since u need to sort on birthday,u need to decide how u store the birthday.Since u have
declared it as int,i believe u r storing the age.

If u want to store it as an actual date,u'll have to write a fn. that compares dates as to which
comes before.

HTH.
Accepted Solution
 
03.10.2004 at 09:05PM PST, ID: 10567845

Rank: Master

Since u have decided to use a linked list, u should choose a sorting scheme, that doesnt require indexed access of elements...
like in qsort, while sorting, we need to access elements by their index in the list.. hence for that array is better..

also in your sorting scheme, it might be required to move back and forth in the list.. So if you are comfortable with this, go for doubly linked list instead of singly linked list.

ssnkumar: wont that be too slow O(n2)!
I think merge sort is better suited for the purpose here..

also if u can afford extra memory and this is not homework assignment to sorting implementation,
then there can be another quicker way using the stdlib's qsort method,
1. make one pass on ur linked list, and store the address of each node in an array.
like
p*[SOME_GOOD_ENOUGH_SIZE];
2. also define a comparator, which would behave as if comparing two nodes of the list
something like this

int pers_compare(void *v1,void *v2){
return ((p*)v2->age - (p*)v1->age); //descending.. to change the order.. change order of substraction
}

3. now tricky part is what to pass to the qsort

qsort(p,LIST_LENGTH,sizeof(p*),pers_compare);
note that, i am sorting the array having addresses.. and sizeof the element to be sorted is .. sizeof person pointer.. and LENGTH= number of nodes in the list..

above will sort the array of pointers, in the desired order.. then u can just walk through the array to print the elements in sorted order..
good things here are
1. that original list remains untouched.. and whenever u wish u can traverse it in the original order..
2. very less piece of code, using power of exisiting qsort function
bad thing..
1. list is not sorted in true sense.
2. extra memory is required

good luck
akshay

Assisted Solution
 
03.11.2004 at 05:19AM PST, ID: 10570847

Rank: Sage

Hi iamnamja,

Sorting a doubly-linked list is a piece of cake.  You can move up and down the list at will, just like you can with an array.

Sorting a singly-linked list is a bit more complicated.  The links move forward through the list, but you often need to move backwards through the list.  There are several practical ways to do this:  recursion, rescanning the list, and breaking/rebuilding the list.  Because this sounds like classwork, the third option is probably not going to be acceptable (though it the most efficient for very large lists).

Consider a list that consists of the even numbers from 2 to 20.  For illustration purposes, this list is ascending.

2 -> 4 -> 6 -> 8 -> 10 -> 12 -> 14 -> 16 -> 18 -> 20

Now add a node at the end that contains 17

2 -> 4 -> 6 -> 8 -> 10 -> 12 -> 14 -> 16 -> 18 -> 20 -> 19

As we scan down the list comparing adjacent nodes, we see things in order until we get to the last node

2 < 4
4 < 6
6 < 8
...
18 < 20
20 !< 19  <<--  need to move this node!

To swap two nodes, we actually need the address of three nodes.  In this case, to swap nodes 19 and 20 we also need the address of 18.  Node 18 points to 20 as the next link in the chain and it will need to point to 19.  Node 19 will need to point to node 20, and node 20 will need to point to NULL to mark the end of the list.

Keeping track of three nodes isn't so bad, but look what happens if the last node is 17.  The node needs to be moved back two places, so we need the address of the last 4 nodes.

If the last node contains a 1, we need to have the address of every node to bubble it back up the list.

There are two ways around this.  You can march through the list recursively, with each successive call advancing one position in the list.  When a node is swapped, the function returns FALSE and the calling node checks to see if the change affects the two nodes that it is comparing.  When the function hits the end of list it returns TRUE, which is returned all the way back to the base node.  A recursive solution is fairly efficient on a small list.  But it's not practical with a large list because stack sizes are ususally quite small.

The non-recursive variant will work on very large lists, but is not particularly efficient.  Every time you swap two nodes you simply restart the sort.  A slighltly better version is to work the entire list each pass.  In this case you would restart the sort if any swaps were performed.


Better than any of the sorts mentioned here is to sort the list by deleting and adding.

1) Walk through the list one time comparing adjacent nodes.  (A and B in this example.)
2) If the two nodes are in the correct order advance one location and retest.
3) If the nodes are out of order, delete node B by linking around it.  (Make sure that you saved a pointer to node B!)
4) Now call your ADD() function to add node B back into the list in it's correct location.  (By definition, node B will be inserted into the list in the already-sorted region that you've already tested.)
5) Resume comparing nodes from where you left off.



Good Luck,
Kent

Assisted Solution
 
03.11.2004 at 10:43AM PST, ID: 10574043
Thanks guys~ I was able to get it to work with your help~ I'll split the points since all of you guys helped me a lot.  Thanks again!
 
11.01.2004 at 08:21PM PST, ID: 12469989
Better to have birthday as 3 integer variables as dd for day mm for month and yy for year. So u can compare easily and sort the linked list.

wrtie one fuction separately to compare 2 dates by passing dd,mm,yy values.

While sorting linked list to comapre you just use those functions.

This will really work out.
 
 
20080236-EE-VQP-29