sorting problem

hello every one
i make a project about linked list and do some functions on the linked list i want to sort the name in the linked list alphabetical i wrote this code but there is something wrong in it anyone can help thanks :)
# include <iostream>
using namespace std;
struct student
{
int id;
char name [40];
int phone;
student *next; 
};
void sort()
{
student *temp1;
temp1=new student;
char temp[40]="";
temp1=h;
for(temp1=h;temp1->next!=0;temp1->next)
{
if(strcmp(temp1->name,temp1->next->name)>=0)
{
temp==temp1->name;
temp1->name==temp1->next->name;
temp1->next->name==temp;
}
}

closed account (D80DSL3A)
What is wrong in it? Please identify the problem(s).

I see several problems with what you are trying to do.

1) This is a sort function, not an add or insert function. If you aren't adding a node to the list then
you should not be allocating new nodes. temp1=new student;// has no place here

2) You want to change the order of the nodes in the list, not copy data fragments between nodes.
With your approach only the name would get swapped. The other data parts (id and phone) would stay the same.

3) Your temp1 pointer isn't advancing in the for loop. You need
for(temp1=h;temp1->next!=0;temp1=temp1->next)

Does this all make sense?
ok thanks i will try to correct the code but how to solve problem 2 how to swap all the data not only the name :) and thank you so much
closed account (D80DSL3A)
You're welcome.
My point in #2 is that you shouldn't be swapping data at all. You leave the data where it is (in memory) and swap the pointers.

Example: Suppose there are 4 student structures stored in memory. Let these be A,B,C and D
and that A.next points to B, B.next points to C and C.next points to D.
A diagram of this may look like so:
A -> B -> C -> D


Now suppose that in the sort() it is found that B.name > C.name. We would want to "swap" these two students in the list. We want to re-order the list and make it:
A -> C -> B -> D

We have the next pointers to work with to accomplish this.

Step 1: Get A.next to point to C, but save the pointer to B first.
student* temp = A.next;// save the pointer to B
Then we can reassign A.next to point to C
A.next = B.next;// since B.next is pointing to C. Now A -> C

Step 2: Get B.next to point to D. C.next has this value
B.next = C.next;// now B -> D

Step 3: Finally make C.next point to B. This pointer was saved as temp.
C.next = temp;// Now C -> B

That completes the swap. NOTE: This is just an explanation of the logic behind the swap. The implementation of it must be worked out!
You will also need to deal with the special case where the "head" node is one of those being swapped.
Also, your sort() needs to go through the list multiple times - not just once. Suppose that in the above example that A.name > C.name. You must repeat the swap above to then get:
C -> A -> B -> D
and so on.

Hope this helped. Feel free to reply with questions. It's your thread after all!
Last edited on
thank you so much. you helped me a lot :)
Topic archived. No new replies allowed.