LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2011, 06:52 PM   #1
Blackened Justice
Member
 
Registered: Jun 2011
Posts: 82

Rep: Reputation: Disabled
Question about sorting and linked lists


Hey everyone. I'm working on a part of my programming project. Basically, I read data from a file, a list of airplanes and their properties: position, destination, where they come from, heading, etc., another file containing the properties of the arrival and departure gates and the runways. Then I must list the airplanes by destination, for those that have the same destination, by altitude, and for those that have the same destination and altitude, by distance to their destination.

Now, I'm having some trouble tackling this problem. I'm using a singly linked list, without sentinel nodes. I'm thinking whether I should insert the airplanes already in that order, or just insert them in the beginning/end of the list, and then find some way to print them out as described above. Does anyone have any suggestion?
 
Old 12-15-2011, 12:38 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by Blackened Justice View Post
Now, I'm having some trouble tackling this problem. I'm using a singly linked list, without sentinel nodes. I'm thinking whether I should insert the airplanes already in that order, or just insert them in the beginning/end of the list, and then find some way to print them out as described above. Does anyone have any suggestion?
Well, a quick but inefficient way to sort a linked list is a bubble sort, because it requires only exchanging pairs of adjacent elements. In case of singly linked list it is simpler to swap the data (or pointers to data) containing in list nodes, not the nodes itself.

You can also store your data in an array (not list) and use the `qsort()' function (assuming you program in C) to sort elements. See `man qsort' for details.

In any case you will need a function to compare two records. Here is a pseudo code:
Code:
int airplane_cmp(airplane A, airplane B)
{
    int c = strcmp(A.dest, B.dest);
    if(c) return c;   // if destinations different
    else if(A.alt != B.alt ) return (A.alt < B.alt)?  -1 : 1;  // if altitudes different
    else if(A.dist != B.dist) return (A.dist<B.dist)? -1 : 1;  // if distances different
    else return 0; // equal
}
Hope this helps.
 
Old 12-15-2011, 12:53 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by firstfire View Post
Well, a quick but inefficient way to sort a linked list is a bubble sort, because it requires only exchanging pairs of adjacent elements. In case of singly linked list it is simpler to swap the data (or pointers to data) containing in list nodes, not the nodes itself.
If you want a simple but inefficient sorting algorithm try Insertion or Selection sort, bubble sort is just too bad to ever be used.
 
Old 12-15-2011, 04:42 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Blackened Justice View Post
Then I must list the airplanes by destination, for those that have the same destination, by altitude, and for those that have the same destination and altitude, by distance to their destination.

Now, I'm having some trouble tackling this problem. I'm using a singly linked list, without sentinel nodes. I'm thinking whether I should insert the airplanes already in that order, or just insert them in the beginning/end of the list, and then find some way to print them out as described above. Does anyone have any suggestion?
It is usually simpler to insert the new node into the correct position, than to sort the list afterwards. Effectively, the insertion works like insertion sort (O(Nē) running time). For singly linked lists, merge sort is often a good choice, and will yield O(N log N) running time.

(If you are unfamiliar with the notation, O(Nē) means that doubling the size of the data will quadruple the running time. O(N log N) means that doubling the size of the data will slightly more than double the running time. O(N) means that doubling the size of the data would just double the running time. The notation does not say anything about which algorithm is faster with the same amount of data, only about how the running time changes for each algorithm when the amount of data changes.)

While insertion sort is slower than merge sort, inserting the nodes as soon as you can create them allows you to do meaningful work while still doing the I/O to obtain the data. When you have lots and lots of data, your process will be I/O bound; i.e., the CPU will be just waiting for work to do. Using insertion sort to insert the data immediately when obtained will often complete faster (wall clock time) while using more CPU time than sorting the data afterwards using a better algorithm such as merge sort, because the time used for the slower sorting would otherwise be spent waiting for I/O.

When using singly linked list without sentinels, temporary sentinels simplify the code a lot. Consider this C insertion example:
Code:
struct node {
    struct node *next;
    int          key; /* Sorted in ascending order */
    /* Other payload... */
};

/* Insert node(s) to list, and return the new list.
 * list has keys in ascending order; nodes is either a single node
 * or an unsorted list of nodes.
*/
struct node *insert_list(struct node *list, struct node *nodes)
{
    struct node  sentinel; /* Temporary sentinel node. Note: Not a pointer! */
    struct node *cursor, *newnode;

    while (nodes) {

        /* newnode is the new node to insert. */
        newnode = nodes;
        nodes = nodes->next;
        /* Note: newnode->next is considered NULL at this point. */

        /* Initialize the temporary sentinel. */
        sentinel.next = list;
        cursor = (struct node *)&sentinel;

        /* Advance, until the insert position is found.
         * The new node is inserted before cursor->next. */
        while (cursor->next && cursor->next->key < newnode->key)
            cursor = cursor->next;

        /* Insert. */
        newnode->next = cursor->next;
        cursor->next = newnode;

        /* The list root was maintained by the temporary sentinel. */
        list = sentinel.next;
    }

    return list;
}
Note that only the next pointer is used in the temporary sentinel; its key is never accessed.
 
Old 12-15-2011, 07:53 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I trust that this is purely an exercise ...
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
linked lists Natasl Programming 5 08-04-2006 12:02 PM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration