Thread: i need help!!

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    i need help!!

    i have a very big problem with my read function it has to get constant and exponent values from the user and creates a linked list but this linked list should be sorted the problem occurs when i sort them pls check my code :

    Code:
    void Poly::read()
    {     
        int Constant,Exponent;
        int end = 0;
        int counter = 0;
        List *currentptr,*new_node,*temp;
        
        currentptr = new List;
        currentptr = head;
        
        
        cout<<"\nEnter 0 to constant to exit\n";
        
          
        do 
          {
               cout<<"\nEnter constant:"; 
               cin>>Constant;
    	   cout<<"\nEnter exponent:"; 
    	   cin>>Exponent;
    	   
    	   currentptr->set_constant(Constant);
    	   currentptr->set_exponent(Exponent);
    
    	   new_node = new List;
    	   currentptr->next = new_node;
    	   currentptr = currentptr->next;
    	   counter++;
          } while(Constant !=0);
    	   currentptr = head;
    	   
    	
            
    	           //sorting
    		   while(end < counter) 
    		   {
    			   while(currentptr->next != NULL)
    			   {
    			      if(currentptr->get_exponent() < currentptr->next->get_exponent())
    			      {
    				temp = new List;
    				temp->set_constant(currentptr->next->get_constant());
    				temp->set_exponent(currentptr->next->get_exponent());
    				
    				currentptr->next->set_exponent(currentptr->get_exponent());
    				currentptr->next->set_constant(currentptr->get_constant());
    				
    				currentptr->set_exponent(temp->get_exponent());
    				currentptr->set_constant(temp->get_constant());
    				
    				delete temp;
    							        
    			      }
    			       end++;
    			       currentptr = currentptr->next;
    		           }
    			   currentptr = head;
    		   }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you appear to be trying a bubble sort on the linked list, but you are incrementing end at the wrong time. You should increment end after the inner while loop stops, not after the if statement.

    BTW---IMO your varaible names are confusing---you use the name List where most people would use the name node/Node or link/Link, but that doesn't appear to be the problem as far as I can see.

Popular pages Recent additions subscribe to a feed