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;
}
}

Recommended Answers

All 4 Replies

Line 15. What is h

it is the head of the linked list
student *h=0;

your void function does not know what h is, unless it's a global variable or being passed by value or reference.
Also are you trying to make objects inside your structure which I'm pretty sure that you cannot do( someone correct me if wrong, I don't want to spread misinformation.)

are you trying to sort names of all students?

In your if statement, shouldn't those be assignment statements rather than checking equality?

Also, temp is an array pointer, so trying to assign another address to it (if that's what you were trying to do) would cause a problem. If you just wanted to copy the string into temp, use strcpy. The thing is, if you did it that way, you'd have 3 calls to strcpy.

You could always use the string class for the student's name, and save yourself the headache.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.