3 Replies - 988 Views - Last Post: 10 March 2011 - 03:37 PM Rate Topic: -----

#1 jeonsah  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 30-August 09

seeing if a linked list is sorted

Posted 10 March 2011 - 02:31 PM

Hey guys, I am trying to make a function that decides whether a "list of integers" is sorted. It is all inside a linked list. If the list is sorted then bool sortedlist = true and if it is not sorted sortedlist = false. Where is my code going wrong?


I run it and when I call this void function, nothing happens. Heres my code. Sorry if its terrible, Im extremely tired... but thanks for the help!

void linkedList::sortedlist()
{

	    nodeType *current;
	    current = first;
	    bool sortedlist;
   
   if(first == NULL)
   {
       cout << endl << "The list is sorted. (first = null). " << endl;
       sortedlist = true;
   }
    
   while(current->next != NULL)
   {
      if(current->data < current->next->data)
         sortedlist = true;
      else
         sortedlist = false;
      
   }
   if(sortedlist = false)
   {
      while(current->next != NULL)
      {
         if(current->data > current->next->data)
            sortedlist = true;
         else
            sortedlist = false;                   
       }
   }
 
   if(sortedlist == true)
   cout << endl << "The list is sorted. " << endl;
   
   if(sortedlist == false)
   cout << endl << "The list is NOT sorted. " << endl;    
}



Is This A Good Question/Topic? 0
  • +

Replies To: seeing if a linked list is sorted

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,688
  • Joined: 25-December 09

Re: seeing if a linked list is sorted

Posted 10 March 2011 - 02:43 PM

This if(sortedlist = false) will always evaluate to true because you are using the assignment operator instead of the comparison operator (==). But why are you doing this if statement at all you are just repeating the loop above this if statement.

In the if statement that starts on line 14 if your if statement ever evaluates to false shouldn't you just exit the loop after setting your sortedloop variable. Also why are you always setting this value to true. If you have initialized your variable to true before the loop it will only change if your list is not sorted.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: seeing if a linked list is sorted

Posted 10 March 2011 - 03:04 PM

you may want to add an else block after the if(first == NULL)

if first IS null you are going to get a run time error when you execute:

while(current->next != NULL) (if current is null then current->next is an error.)

which you don't care about anyway because you already know the list is sorted because the first node is null. OR you could add a return inside the if(first == NULL) block -- that would keep the code from continuing on once you determined that the list WAS sorted.
Was This Post Helpful? 0
  • +
  • -

#4 jeonsah  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 30-August 09

Re: seeing if a linked list is sorted

Posted 10 March 2011 - 03:37 PM

thanks guys. Ill work on it some more and ill post up any other questions I got. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1