phonenode *start_ptr = NULL;
phonenode *crrnt_ptr;

void alpha_order(){
	phonenode *ptr, *curr, *temp;
	ptr = start_ptr;
	for(bool didSwap = true; didSwap;){
		didSwap = false;
		for(curr = ptr; curr->next != NULL; curr = curr->next){
			if (curr->surname > curr->next->surname){
				temp = curr;
				curr = curr->next;
				curr->next = temp;
				didSwap = true;
			}
		}
	}

	
}

here is my code which does not work..
from for loop it will check if surname > next->surname and if it is true that it will be swap all curr linked list.

Edited 7 Years Ago by hur1214: n/a

void sort()
{
 phonenode *tmp1, *tmp2, *tmp3; 
 for(tmp1 = start_ptr ; tmp1->next != NULL; tmp1 = tmp1 ->next)
 {
  for(tmp2 = tmp1->next ; tmp2->next != NULL ; tmp2 = tmp2->next)
	  if( tmp1->surname > tmp2->surname){
		  tmp3 = tmp1;
		  tmp1 = tmp2;
		  tmp2 = tmp3;

		  return;
	  }
 cout << tmp1->surname << endl;
 return;
 }
}

I changed a lot of codes, but still no luck.
seems there are no errors, but it does not sort linked list.

struct phonenode
{	
	char surname[14];	
	char initial[10];
	char num[5];
	
	phonenode *next;
};

Edited 7 Years Ago by hur1214: syntax error fixed

Your second version is really not going to work - your swap portion only changes the assignment of the temporary pointers in the function, it does not change the ->next pointers in the actual nodes. More importantly, the return statement in the if block ends the function on the first swap!

Your first version was closer to correct - but it ends up keeping the node curr pointing to the same following node - no reordering is occurring. The function needs to keep track of the node before curr, how about calling it prev? In the swap, this needs to then point to the node following curr, and that node gets pointed to curr. (Sounds like we're talking about bad dogs.)

Here's something closer

phonenode *start_ptr = NULL;
phonenode *crrnt_ptr;

void alpha_order(){
	phonenode *ptr, *curr, *temp, *prev;
	ptr = start_ptr;
        bool didSwap = true;
	while ( didSwap == true)  //not really a good place for for loop
        {
		didSwap = false;
                prev = ptr;
//need to handle comparing first and second nodes separately, then start the loop
		for(curr = ptr->next; curr->next != NULL; prev = prev->next, curr = prev->next)
                {
			if (curr->surname > curr->next->surname){
				prev->next = curr->next;
				curr->next = curr;
				didSwap = true;
			}
		}
	}
}

But, I'll leave to to figure out how to handle comparison of the headnode with its next node, which is a special case here.

I'm still not wholly in favor of the inner loop as a for loop - probably also should be a while, with the updates for prev and curr inside it.

look at the following

if( tmp1->surname > tmp2->surname)

thats not a way for comparing two strings. U r comparing the address of the string instead.

use strcmp() for that purpose

Comments
Good catch, I was just looking at the list aspects.
This article has been dead for over six months. Start a new discussion instead.