0

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;
}
}
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Chilton
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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.