I am trying to sort my structure based on names (alphabetically). For example I have this structure:
struct person{
char name[MAX];
char num[MAX];
char email[MAX];
struct person *next;
struct person *previous;
};
struct person *next, *first, *end;
and in my main function :
int main()
{
struct person p = {{0}}
.
.
.
switch(choice)
{
case 1 :
printf("Name : "); scanf("%s", &p.name);
printf("Num : "); scanf("%s", &p.num);
printf("E-Mail : "); scanf("%s", &p.email);
input(p); //function to input data
break;
case 2 :
sort(p); //function to sort the data
break;
}
}
And in my prototype function:
void sort(struct person p)
{
struct person *ptr, *ptr2, *temp;
ptr=first;
ptr2=first;
for (ptr= first; ptr!= NULL; ptr= ptr->next)
{
for (ptr2= ptr2->next; ptr2!= NULL; ptr2= ptr2->next)
{
if (strcmp(ptr->name, ptr2->name) > 0)
{
temp = ptr;
ptr= ptr2;
ptr2= temp;
}
}
}
}
Okay, so I have a prototype function which sort the structure based on its element which is name. I tried sorting it using the same principle as bublesort but it didnt work out for me. Pointer ptr points to the first node in the list and ptr2 points to the second node. Is my algorithm wrong?I cant seem to find out what is wrong. Main problem is with the sorting. I Hope my question is clear.
struct person p; it is likely to need to be astruct person **por astruct person &*p.