Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C-C++

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Sort Linked List

By shabbir shabbir is offline

On 7th June, 2007
Wink Sort Linked List

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Sort Linked List link

Author

shabbir ( Go4Expert Founder )

Shabbir is a developer in the field of Applications, web as well as database designing and is devoted to the optimization and usability of the code. He maintains Programming forum and is a C++ addict.


All articles By shabbir

Recent Articles

Similar Articles

After having so many articles on Linked List
Basic operations in Linked List
Swap two nodes of a linked list
Move forward a node in linked list
Insert a node in a linked list
Double linked list
Double circular linked list
Circular linked list
Priority Queue implementation using Linked List
Queue implementation through linked list
I thought why not also have the one which sorts the linked list as well. I did not submit it because I thought the above list is sufficient for sorting as well but because of lots of query regarding the same I thought lets have it online as well.

Note: I had a major bug pointed out by RiOrius and is rectified now. Thanks for letting me know about it.

Code: Cpp
#include <iostream>
using namespace std;

#define NULL 0

class LinkList
{
private:
    struct Node
    {
        Node* prev;
        int value;
        Node *next;
    };
    Node *first;
   
public :
   
    LinkList()
    {
        Node *t1 = new Node();
        t1->prev = NULL;
        t1->value = 4;
        t1->next = NULL;
        first = t1;
       
        Node *t2 = new Node();
        t2->prev = t1;
        t2->value = 6;
        t2->next = NULL;
        t1->next = t2;
       
        Node *t3 = new Node();
        t3->prev = t2;
        t3->value = 1;
        t3->next = NULL;
        t2->next = t3;
       
        Node *t4 = new Node();
        t4->prev = t3;
        t4->value = 5;
        t4->next = NULL;
        t3->next = t4;

        Node *t5 = new Node();
        t5->prev = t4;
        t5->value = 8;
        t5->next = NULL;
        t4->next = t5;

        Node *t6 = new Node();
        t6->prev = t5;
        t6->value = 1;
        t6->next = NULL;
        t5->next = t6;

        Node *t7 = new Node();
        t7->prev = t6;
        t7->value = 0;
        t7->next = NULL;
        t6->next = t7;
    }
   
    ~LinkList()
    {
        Node *temp = first, *current = first;
        while(current != NULL)
        {
            temp = current->next;
            delete current;
            current = temp;
        }
       
    }
   
    void Display()
    {
        Node *temp;
        for(temp = first; temp != NULL; temp = temp->next)
        {
            cout<<temp->value<<" , ";
        }
        cout<<endl;
    }
   
   
    void Sort()
    {
        Node *current, *cur;

        for(current = first; current->next != NULL; current = current->next)
        {
            Node *minimum = current;
            for(cur = current ; cur != NULL; cur = cur->next)
            {
                if(minimum->value > cur->value)
                {
                    minimum = cur;
                }
            }
            if(minimum != current)
            {
                Node *current_previous, *current_next, *min_previous, *min_next;

                // Initialize them
                current_next = current->next;
                min_previous = minimum->prev;
                min_next = minimum->next;
                current_previous = current->prev;

                if(current_previous == NULL)
                {
                    // Change the First Node
                    first = minimum;
                }
                if(current->next == minimum)
                {
                    // Nodes are Adjacent
                    minimum->prev = current_previous;
                    minimum->next = current;

                    current->prev = minimum;
                    current->next = min_next;

                    if(min_next)
                    {
                        min_next->prev = current;
                    }
                    if(current_previous)
                    {
                        current_previous->next = minimum;
                    }
                }
                else
                {
                    minimum->prev = current_previous;
                    minimum->next = current_next;

                    current->prev = min_previous;
                    current->next = min_next;

                    if(current_next)
                    {
                        current_next->prev = minimum;
                    }
                    if(min_previous)
                    {
                        min_previous->next = current;
                    }
                    if(min_next)
                    {
                        min_next->prev = current;
                    }
                    if(current_previous)
                    {
                        current_previous->next = minimum;
                    }
                }
                current = minimum;
            }
        }

    }
};

void main()
{
    LinkList list;
   
    cout<<"LinkList = ";
   
    list.Display();
   
    cout<<"\n\nSorting \n\n";
   
    list.Sort();
   
    cout<<"LinkList = ";
   
    list.Display();
   
}
Old 07-27-2007, 01:22 PM   #2
Shishir191
Go4Expert Member
 
Join Date: Jul 2007
Location: Delhi
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Shishir191 is on a distinguished road
Thumbs up

Re: Sort Linked List


Hi,
The code you have written is good but according to me the sorting function should be small, its too complex.
__________________
Thanks and Regards

Shishir
Shishir191 is offline   Reply With Quote
Old 07-27-2007, 07:08 PM   #3
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Sort Linked List


Quote:
Originally Posted by Shishir191
Hi,
The code you have written is good but according to me the sorting function should be small, its too complex.
I am sure if we try we will have something better and shorter.
shabbir is offline   Reply With Quote
Old 07-31-2007, 07:40 PM   #4
seeguna
Go4Expert Member
 
Join Date: Jun 2007
Location: Chennai
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
seeguna is on a distinguished road
Thumbs up

Re: Sort Linked List(Simple coding for sorting)


Code: Cpp
#include<stdio.h>
#include<stdlib.h>
void create();
void show();
void sort();
struct node
{
    int d;
    struct node *link;
}*head=0,*current,*prev;
int num_nodes=0;
void main()
{
    int choice;
    while(1)
    {
        printf("\n1.Create\n2.Display\n3.Sort\n4.Exit\n");
        printf("Enter ur choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            create();
            break;
        case 2:
            show();
            break;
        case 3:
            sort();
            break;
        case 4:
            exit(0);
        }
    }
}
void create()
{
    int data;
    current=(struct node*)malloc(sizeof(struct node));
    printf("Enter the data:");
    scanf("%d",&data);
    num_nodes++;
    if(head==0)
    {
        head=current;
        current->d=data;
        current->link=NULL;
        prev=current;
       
    }
    else{
        current->d=data;
        current->link=NULL;
        prev->link=current;
        prev=current;
    }
}
void show()
{
    current=head;
    while(current!=NULL)
    {
        printf("->%d",current->d);
        current=current->link;
    }
   
}
void sort()
{
    // current ptr is used to point the current node in the list
    //prev ptr is used to point the previous node of current node
    //next ptr is used to point the next node of current node
    int i,j;
    struct node *prev=0,*next=0;
    //Initially current node and previous node should be the same(first) node in the list
    current=head;
    prev=head;
    next=head->link;
    for(i=0;i<num_nodes-1;i++)
    {
        for(j=0;j<num_nodes-i-1;j++)
        {
            if(current->d>next->d)
            {
                current->link=next->link;
                next->link=current;
                if(current==head)
                {
                    head=next;prev=next;
                }
                else
                {
                    prev->link=next;prev=next;
                }
                if(next!=NULL)//check whether final node is reached
                    next=current->link;
               
            }
            else//just moved each node ptr by one position
            {
                prev=current;
                current=next;
                next=current->link;
            }
           
        }
        //For next iteration make the initial settings again
        current=head;
        prev=head;
        next=current->link;
    }
   
    //For display the sorted numbers
    current=head;
    while(current!=NULL)
    {
        printf("->%d",current->d);
        current=current->link;
    }
}

Last edited by shabbir; 07-31-2007 at 07:49 PM. Reason: Code block
seeguna is offline   Reply With Quote
Old 07-31-2007, 07:51 PM   #5
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Sort Linked List


Thats a good one. Yours is a Linear sort and I have used bubble sort but I am sure with bubble sort also some code blocks can be made shorter.
shabbir is offline   Reply With Quote
Old 07-31-2007, 10:11 PM   #6
Shishir191
Go4Expert Member
 
Join Date: Jul 2007
Location: Delhi
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Shishir191 is on a distinguished road
Thumbs up

Re: Sort Linked List


Quote:
Originally Posted by shabbir
Thats a good one. Yours is a Linear sort and I have used bubble sort but I am sure with bubble sort also some code blocks can be made shorter.
Hi Shabbir,

I think it is much better than pervious one.
__________________
Thanks and Regards

Shishir
Shishir191 is offline   Reply With Quote
Old 08-01-2007, 08:52 AM   #7
seeguna
Go4Expert Member
 
Join Date: Jun 2007
Location: Chennai
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
seeguna is on a distinguished road
Thumbs up

Re: Sort Linked List


Thanks shabbir
seeguna is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

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 On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Huffman Encoding in C (Minimum Variance Encoding) rai_gandalf C-C++ 9 01-14-2008 11:10 AM
Basic operations in Linked List shabbir C-C++ 2 10-30-2007 06:20 PM
Sorting Algorithms saomcol Programming 5 09-30-2007 01:50 PM
Doubly Linked List Implementation in C# Sanskruti C# 0 03-13-2007 11:03 AM

 

All times are GMT +5.5. The time now is 05:31 AM.