Thread: Linked list

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

    Linked list

    I have this sort, can someone tell me why the code it not sorted correctly. Here is an example run of it.

    Enter coefficient and exponent (0 0 to stop):9 8
    Enter coefficient and exponent (0 0 to stop):3 10
    Enter coefficient and exponent (0 0 to stop):6 2
    Enter coefficient and exponent (0 0 to stop):10 44
    Enter coefficient and exponent (0 0 to stop):9 120
    Enter coefficient and exponent (0 0 to stop):0 0
    DEBUG: before sort
    9 8 3 10 6 2 10 44 9 120

    3 10 10 44 9 120 9 8 6 21
    DEBUG: After sort

    It should be 9 120 10 44 3 10 9 8 6 2

    counter is the size of the list.
    Code:
    void LList::sort() {
    listNode *temp;
    temp = head;  
      int j;
    for(j = 0; j < counter; j++)  {    
         
      while(temp->next != NULL) {
         if(temp->expo < temp->next->expo) {
             int tempInt, tempInt2;
             tempInt = temp->next->expo;
             tempInt2 = temp->next->coef;
             temp->next->expo = temp->expo;
             temp->next->coef = temp->coef;
             temp->expo = tempInt;
             temp->coef = tempInt2;
         } else {
            temp = temp->next;
         }
      } //end while
    } // end for
    
    } // end function
    
    This is the class decleration of the list
    
    class listNode {
     public:
      int coef;
      int expo;
      listNode *next;
    
    };
    Any help would be great thanks.


    Code tagged by Hammer.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use Code Tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    You say you wanted it to be sorted right. How sorted least to greastest. You say 3 10 10 44 9 120 9 8 6 21 but thats not in order .
    It should be 3 6 8 9 9 10 10 21 44 120.
    Maybe try a bubble sort?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try moving the line

    temp = head;

    after the for() loop and before the while() loop so temp starts at head each time the for loop is run, rather than just once as it is.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    It does not look like you are sorting your elements. Instead you are copying the data over existing elements.

    You need 5 pointers in a standard linked list. head, end,current,next,prev. head, end and current are usually in a base class. The next and prev are in a derived class with your other class specific data.

    You should never be referencing the elements of your pointers, other than next and prev.

    if( current->exp < next->exp)
    {
    //Insert the temp element in the list before next and update the pointers.
    current->next=next;
    current->prev=next->prev;
    next->prev=current;
    }
    Last edited by bonkey; 11-01-2002 at 12:48 PM.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM