What is wrong with this sort function? (linked list)

This is a discussion on What is wrong with this sort function? (linked list) within the C++ Programming forums, part of the General Programming Boards category; the list stores a group of terms in a polynomial Code: struct term { int co; //coefficient of a term ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    What is wrong with this sort function? (linked list)

    the list stores a group of terms in a polynomial

    Code:
    struct term
    {
      int co;    //coefficient of a term
      int exp;  //exponent of a term
      term* link;
    };
    
    typedef term* termptr;
    
    class Polynomial
    {
      public:
        void sort();
        friend istream& operator >> (istream& ins, Polynomial& p1);
        //other random stuff you don't need to know for this
      private:
        termptr head;
    };
    I'm trying to sort them once they've been inputted in order of exponent from greatest to least. here's what my sort function looks like:

    Code:
    void Polynomial::sort()
    {
      termptr cursor, current, prev;
      int largest = 0;
    
      current = head;
      cursor =  current->link;
      prev = current;
      for (termptr ptr = head; ptr != NULL; ptr = ptr->link)
      {
        largest = current->exp;
        while (cursor)
        {
          if (cursor->exp > largest)
          {
            largest = cursor->exp;
            prev->link = cursor->link;
            ptr = cursor;
            current = cursor;
            cursor = prev;
            cursor = cursor->link;
          }else
          {
            prev = prev->link;
            cursor = cursor->link;
          }
        }
        current = current->link
        prev = current
        cursor = current->link;
      }
    }
    
    istream& operator >> (istream& ins, Polynomial& p1)
    {
       char x;
       string line;
       termptr new_term, current;
    
      new_term = new term;
      new_term->link = NULL;
      p1.set_head(new_term);
      current = new_term;
    
      getline(ins, line);
    
      if (ins)
      {
        line.erase(remove(line.begin(), line.end(), ' '), line.end());
        stringstream ss(line);
    
        while (ss>>current->co >> x >> current->exp)
        {
          new_term = new term;
          new_term->link = NULL;
          current->link = new_term;
          current = current->link;
        }
      }
      current->link = NULL;
      return ins;
    }
    
    //here's my application file
    
    int main()
    {
      Polynomial poly1;
    
      cout << "Enter a polynomial: ";
      cin >> poly1;
      cout << endl;
      poly1.sort();
    
      cout << poly1 << endl;
    
      return 0;
    }
    it compiles fine, but when I try to call it in my application file I get a segmentation fault and I can't figure out why
    Last edited by Strait; 02-21-2005 at 05:45 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, let's see. The last time you dereferenced a null pointer, so I'll go with that. Like I said before, when you typedef away the pointer notation, it obscures when you are doing that.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I'd love to try and compile this, but I'd prefer to have a bit more code, such as Polynomial::set_head and Polynomial::operator<<.

    edit: Some sample input would be nice, too.
    Last edited by pianorain; 02-22-2005 at 07:40 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21