خرید بک لینک

Vote count: 0

I want to read in numbers from text file and create a linked list dynamically, although I am able to do it, the last node with value of 0 is also added to the list. How can I correct this? Also I should be able to add value 0 if i want but not in this way.

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct node {
      int data;
      struct node *next;
    }node;


    int main(int argc, char const *argv[]) {

      FILE *fp = fopen("data.txt", "r");

      node *prev = NULL;
      while(!feof(fp)){
        node *curr = malloc(sizeof(node));

        fscanf(fp, "%d", &(curr->data));           
        fprintf(stdout, "%d-->", curr->data);

        if(prev != NULL){
          prev->next = curr;
        }
        prev = curr;
      }
      printf("NULLn");

      fclose(fp);
      retu 0;
    }

The input file is this single column of integers 1 5 2 3 6 4

The output comes like this, the 0 is not part of input 1-->5-->2-->3-->6-->4-->0-->NULL

asked 3 mins ago

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: شنبه 26 تير 1395 ساعت: 6:34

صفحه بندی