1

I have created a linked list in C but it is printing in the reverse order. When printing the linked list, I want my print function to be separate from the main function. How do I print the linked list in the same order as the data was inputted?

struct CTAStations
{
    int stationID;
    char *stationName;
    float latitude;
    struct CTAStations *next;
}*head;

typedef struct CTAStations cta;

void display();

int main(int argc, const char * argv[])
{
    FILE *file;
    file = fopen("station8.csv","r");

    char buffer[64];
    
    if(file != NULL) {
        
        while (!feof(file)) {
            cta *node = malloc(sizeof(cta));
            fgets(buffer, 64, file);
            node->stationID = atoi(strtok(buffer, ","));
            node->stationName = strdup(strtok(NULL, ","));        
            node->latitude = atoi(strtok(NULL, ","));
            node->next = head;            
            head = node;
            printf("While Loop print test: %s\n", node->stationName);
        }
    }
    printf("\n");
    display();
    fclose(file);
    return 0;
}

void display()
{
    struct CTAStations *temp;
    temp = head;
    while(temp!=NULL) {
        printf("%s\n",temp->stationName);
        temp = temp->next;
    }
}

Here is the input file:

40302,20th,39.483921
40830,18th (Pink Line),41.857908
40120,35th/Archer (Orange Line),41.829353
41120,35th-Bronzeville-IIT (Green Line),41.831677

Here is the output: enter image description here

daf
  • 81
  • 4
  • By the way, your usage of `while (!feof(file))` is [wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and you should use "the data read" only after checking if readings are successful. – MikeCAT Aug 27 '20 at 15:57
  • Could you clarify what you mean? – daf Aug 27 '20 at 15:58
  • 1
    Which do you want to modify, the order of data in the linked list or just the order of printing? – MikeCAT Aug 27 '20 at 15:58
  • The order of printing. I have previously added a print statement in the while loop to check and it seems that the data in the linked list is correct. – daf Aug 27 '20 at 16:01
  • The data in the linked list is the reverse of whatever it is in `station.csv` (which you should show a representative snippet from along with actual/expected output). Welcome to SO! – ggorlen Aug 27 '20 at 16:03
  • How do I insert the data in the linked list in the correct order? I've just edited my post to show the output. – daf Aug 27 '20 at 16:10

1 Answers1

0

The data is stored in the linked list is stored in reversed order compared to data in the file because data newly read are connected to the head of linked list.

Therefore, you have to print the linked list in reversed order to have the print the same order as the file.

An easy way to do that is using recursion like this:

void display_internal(struct CTAStations *temp)
{
    if(node!=NULL) {
        display_internal(temp->next);
        printf("%s\n",temp->stationName);
    }
}

void display()
{
    display_internal(head);
}

To have it store data to the linked list in the same order of the file, you can keep track the tail of the list and add node there like this:

    char buffer[64];
    struct CTAStations** tail = &head;
    
    if(file != NULL) {
        
        while (fgets(buffer, 64, file)) {
            cta *node = malloc(sizeof(cta));
            node->stationID = atoi(strtok(buffer, ","));
            node->stationName = strdup(strtok(NULL, ","));
            node->latitude = atoi(strtok(NULL, ","));
            node->next = NULL;
            *tail = node;
            tail = &node->next;
            printf("While Loop print test: %s\n", node->stationName);
        }
    }
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • If `station.csv` is large, this will blow the stack. I'd prefer iteration (OP _probably_ has an xy problem and really wants to build their list in a forward manner using a `tail` pointer). – ggorlen Aug 27 '20 at 16:04
  • How do I insert the data in the linked list in the correct order? – daf Aug 27 '20 at 16:10
  • Keep track a pointer for the tail node and append node using that. – MikeCAT Aug 27 '20 at 16:12
  • Are you sure that the order of the data in the linked list is incorrect? Have you seen my output image in my post? – daf Aug 27 '20 at 16:14
  • I'm not sure if the order of the data in the linked list is incorrect because I don't know what @daf want. – MikeCAT Aug 27 '20 at 16:18