0

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;
}
jay
  • 99
  • 6
  • 1
    `q->surname > p->surname` looks strange. String comparisions should be via `strcmp()`. – MikeCAT Jul 02 '20 at 13:34
  • 1
    [`while (!feof (dataFile))` is wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). You should check return values of `fscanf()` to check if readings are successful. – MikeCAT Jul 02 '20 at 13:35
  • `q` is used without being initialized. This must be the cause of "stuck at a while loop". – MikeCAT Jul 02 '20 at 13:37
  • @MikeCAT hm.. so maybe like this? ```res= strcmp(p, q); if (res > 0) { swap(*q->surname,*p->surname); }``` – jay Jul 02 '20 at 13:50
  • Style guide: the dot `.` and arrow `->` operators bind very tightly because they are [postfix operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.3). They should not be written with spaces around them. Writing `current -> surname` is not idiomatic C and indicates that the coder is a tyro (newbie). Use `current->surname`. – Jonathan Leffler Jul 02 '20 at 14:24
  • Hmmm, both of implementation and usage of `swap` is wrong. At least `swap` requires pointers as arguments, so pointers should be passed instead of dereferened `char`. Also, your `swap` will swap only the first characters of the strings. `strcpy()` is useful to copy strings. – MikeCAT Jul 02 '20 at 16:10
  • `char*` should be passed to `strcmp()`, not `Node*`. – MikeCAT Jul 02 '20 at 16:11
  • By "improve" sort, did you mean other sort algorithms? Merge sort for linked lists would be faster, but normally it's fastest to copy linked list to an array, sort the array, create a new sorted linked list. This is what Java collections.sort() does. – rcgldr Jul 03 '20 at 11:07

0 Answers0