我已经设置了一个链表,但是如何从csv文件中读取链表?

时间:2019-05-15 12:25:32

标签: c struct linked-list

我有一个带有正常工作链表的C文件,但是我必须输入变量,如何更改它以便从文件中读取?我具有读取文件的基础知识,但是之后不知道该怎么办。如果有人可以帮助我,那将是惊人的

struct Node 
{ 
  int data; 
  struct Node *next; 
};
 void push(struct Node** head_ref, int new_data) 

    /* 1. allocate node */
   struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 

    /* 2. put in the data  */
    new_node->data  = new_data; 

    /* 3. Make next of new node as head */
    new_node->next = (*head_ref); 

    /* 4. move the head to point to the new node */
    (*head_ref)    = new_node; 


void append(struct Node** head_ref, int new_data) 
{ 
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 

    struct Node *last = *head_ref;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->data  = new_data; 

    /* 3. This new node is going to be the last node, so make next of 
          it as NULL*/
    new_node->next = NULL; 

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL) 
    { 
       *head_ref = new_node; 
       return; 
    } 

    /* 5. Else traverse till the last node */
    while (last->next != NULL) 
        last = last->next; 

    /* 6. Change the next of last node */
    last->next = new_node; 
    return; 
} 
  void createList() 
{ 
  /* Start with the empty list */
  struct Node* distance = NULL;
  struct Node* direction = NULL;

我有一个单独的C文件,其中包含用于读取文件的代码,我真的需要帮助才能通过链表读取文件


#include <string.h> 
#include "linkedList.h"


void readFile()

  /*Reads in a file*/
  {
        FILE* file;
        if ((file = fopen("exmp1.csv","r")) == NULL) {
                perror("Error opening file");
        }
        else {
                printf("File is ready to be read");
                if (ferror(file)) {
                        perror("Error reading from file");
                        fclose(file);
                }

                     /* Declaring the variables */
                    int distance;
                    char* direction;
                    /* Creates an empty linked list */

                    create();

                    do
                    {
                        /* reading line in file and creating node 
                                     */
                        fscanf(file, "%s %d" , &direction, 
                                              &distance);

                    }
                    /* check if end of file */
                    while(!EOF);
}
fclose(file);
}

我有一个csv

UP 2
DOWN 3
LEFT 2
RIGHT 4

0 个答案:

没有答案