Thread: Sorting a linked list.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Sorting a linked list.

    Trying to sort a linked list. I have two classes.
    class listNode {
    public:
    int coef;
    int expo;
    listNode *next;
    };


    class LList {
    private:
    listNode *head;
    listNode *tail;
    public:
    void read()
    void sort();

    };

    I read in the two numbers coef and expo. I need to sort it in accending order by expo. My sort function is
    void LList::sort() {

    listNode *temp;
    listNode *temp2;

    temp = head;
    temp2 = temp->next;
    int CC = 0;
    // counter is global and is size of list
    while(CC != counter) {

    if(temp->expo <= temp2->expo) {
    head = temp2;
    head->next = temp;
    }

    CC++;
    }

    }

    It goes into an infinite loop then crashes.
    Anyone offer advice on the problem? Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    By using the STL list template object it is much easier to do this:
    Code:
    class listNode {
    public:
        int coef;
        int expo;
        bool operator<(const listNode& rhs)
        {
            return expo < rhs.expo;
        }
    };
    Create a linked-list of listNode objects called MyList (for instance) by saying list<listNode> MyList;. The < operator for the class is used by the sort function built into the list template. To sort the list just say something like MyList.sort(). You may want to add some other things to the class like constructors and what not but in any case, this way is much easier and less error prone.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I think the problem lies here:
    Code:
    while(CC != counter) {
    
       if(temp->expo <= temp2->expo) {
          head = temp2;               //temp2 never changes
          head->next = temp;      //temp never changes
       }
       CC++;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    hk_mp5kpdw I did that with list. But when I go to print it out I get the same list printed. Here is my method

    void LList:rint() {
    listNode *temp;
    temp = head;

    while(temp != NULL) {
    std::cout << temp->coef << " " << temp->expo << " ";
    temp = temp->next;
    } // end while

    cout << endl << endl;
    } // end function

    I am thinking this has something to do with head and tail in the LList class not being changed. Offer anymore advice on how to do this? Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM