Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,324 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,101 people online right now. Registration is fast and FREE... Join Now!




Sort a Doubly-linked and circular list

 
Reply to this topicStart new topic

Sort a Doubly-linked and circular list, data structure fun :)

kyrotomia
post 2 Apr, 2008 - 02:57 PM
Post #1


New D.I.C Head

*
Joined: 5 May, 2007
Posts: 45



Thanked 1 times
My Contributions


Hi. I am currently working on a doubly-linked and circular list (is that term correct? , I am working all of this in french blink.gif ). I am done on every part (inserting, removing) and now I'm trying to add a sorting method to my class. For now, I'm trying a simple bubble sort algorithm to at least have something working.

I though of some easy ways of getting it done, but since I am a very stubbord person (sometimes), I'd like to do it a certain way. I am trying to sort directly into my list, permuting element as the sorting goes. I am aware my code is probably something worth laughing at, but Hey!, I'm starting and trying to learn.

Here is my code so far :
CODE

                node * temp = head;
        for (int cpt = 1;cpt <= nbrElements * ((nbrElements - 1) / 2);cpt++)
        {                
            
            if (temp->data > temp->next->data)
                permute(temp,temp->data);
            temp = temp->next;            
        }
        if ((cpt + 1) % nbrElements == 0)
            temp = head;


My for condition is something I found about bubble sorting, which represent the maximum number of comparaisons the algorithm has to do. This code succeeded in sorting a 3 element list. However, it gives some weird stuff when I throw something bigger. Since my list is circular, I thought I could do it this way by somehow I was not able to achieve it yet.

So I'm here begging for mighty help! Thanks a lot smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:42AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month