Home > database >  Build a linked list in c
Build a linked list in c

Time:10-22

I am attempting to create a linked list from a file using structs, when I run the print_list it is not printing the name or threat level, but it is printing the ID. I have little experience with structs but I believe I am not understanding if the pointers are being used correctly here.

void insert(struct poi_t_struct **head_ref, char *name, long int id, int threat_level) {
    // printf("Name from insert %s \n", name); // debug
    struct poi_t_struct newNode = { name, id, threat_level, NULL };
    struct poi_t_struct *ptr_newNode = &newNode;
    struct poi_t_struct *temp = *head_ref;
}

void buildLList(char *filename) {
    FILE *poiProfiles;
    // char line[MAX_LINE_LENGTH];
    // have 3 char arrays. One for each name, id, threat level
    char idLine[MAX_LINE_LENGTH];
    char nameLine[MAX_LINE_LENGTH];
    char threat_levelLine[MAX_LINE_LENGTH];
    
    while (fgets(idLine, MAX_LINE_LENGTH, poiProfiles)) {
        long int id;
        id = atoi(idLine);
        fgets(nameLine, MAX_LINE_LENGTH, poiProfiles);
        char* name;
        name = nameLine;
        fgets(threat_levelLine, MAX_LINE_LENGTH, poiProfiles);
        int threat_level;
        threat_level = atoi(threat_levelLine);
        insert(&head, name, id, threat_level);
    }


void print_list(struct poi_t_struct *p) {
    struct poi_t_struct *temp = p;
    while (temp != NULL) {
        print_record(temp);
        temp = temp->next;
    }
}

CodePudding user response:

There are multiple problems in your code:

  • you do not open nor close the file for poiProfiles

  • there is a missing } at the end of the buildLList() function.

  • the insert function must allocate memory for the new node, inserting a local struct object is incorrect as this object becomes invalid as soon as the function returns. You attempt to insert the new node at the beginning of the list, but you neither set the next member of the struct, not set *head_ref to point to the new node.

Here is a modified version:

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

struct poi_t_struct {
    char *name;
    long int id;
    int threat_level;
    struct poi_t_struct *next;
} *head;

int insert(struct poi_t_struct **head_ref, const char *name, long int id, int threat_level) {
    // printf("Name from insert %s \n", name); // debug
    struct poi_t_struct *ptr_newNode = malloc(sizeof *ptr_newNode);
    if (ptr_newNode == NULL)
        return -1;
    ptr_newNode->name = strdup(name);
    ptr_newNode->id = id;
    ptr_newNode->threat_level = threat_level;
    ptr_newNode->next = *head_ref;
    *head_ref = ptr_newNode;
    return 0;
}

int buildLList(const char *filename) {
    // have 3 char arrays. One for each name, id, threat level
    char idLine[MAX_LINE_LENGTH];
    char nameLine[MAX_LINE_LENGTH];
    char threat_levelLine[MAX_LINE_LENGTH];
    FILE *poiProfiles;
    int count = 0;
    
    poiProfiles = fopen(filename, "r");
    if (poiProfiles == NULL) {
        fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
        return -1;
    }
    while (fgets(idLine, MAX_LINE_LENGTH, poiProfiles)
    &&     fgets(nameLine, MAX_LINE_LENGTH, poiProfiles)
    &&     fgets(threat_levelLine, MAX_LINE_LENGTH, poiProfiles)) {
        long int id = atoi(idLine);
        char *name = nameLine;
        int threat_level = atoi(threat_levelLine);
        if (!insert(&head, name, id, threat_level))
            count  ;
    }
    fclose(poiProfiles);
    return count;
}

void print_list(const struct poi_t_struct *p) {
    const struct poi_t_struct *temp = p;
    while (temp != NULL) {
        print_record(temp);
        temp = temp->next;
    }
}
  • Related