Hi I am trying to implement sort function. With this function, my expected output should be increasing order. For example, if there is three names such as jarvan, anivia, ziggs, then the output should be organized like anivia, jarvan, ziggs. But this gives me the issue that process is stuck at a while loop. Without sort function, this code works very well. but with it, it dosen't.
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList{
char name[30];
char surname[30];
int grade;
struct linkedList *next;
} Node;
void swap(char *p, char *q)
{
char temp;
temp=*p;
*p=*q;
*q=temp;
}
void sort(Node *head)
{
Node *p=head, *q;
while(1)
{
if (q->surname > p->surname)
{
swap(*q->surname,*p->surname);
}
if (q->next ==NULL)
{
break;
}
q=q->next;
if (p->next ==NULL)
{
break;
}
p=p->next;
}
}
Node *readList(Node *head)
{
FILE *dataFile;
dataFile = fopen("12.txt", "r");
if(dataFile == NULL) {
printf("no file\n");
} else {
printf("students\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(Node));
fscanf(dataFile, "%s %s %d", &head -> name, &head -> surname, &head -> grade);
head -> next = NULL;
} else {
Node *current = head;
Node *temp = malloc(sizeof(Node));
while(current -> next != NULL) {
current = current -> next;
}
fscanf(dataFile, "%s %s %d", &temp -> name, &temp -> surname, &temp -> grade);
current -> next = temp;
temp -> next = NULL;
}
return head;
}
void printList(Node *head)
{
Node *current = head;
while (current != NULL) {
printf("%s %s %d\n", current -> name, current -> surname, current -> grade);
current = current -> next;
}
printf("NULL\n");
return;
}
int main()
{
Node* AB=NULL;
AB=readList(AB);
sort(AB);
printList(AB);
return 0;
}