James Wong
James Wong

Reputation: 35

Storing multiple entry into linked list

I have a text file consists of multiple journal entries which I can read it and store it into a struct called Journal. But my problem now is that I dont know how to add it into the linked list 1 by 1.

this is my insert method

void insertStart (LinkedList *list, Journal *JEntry) {
    Node *newNode;
    newNode = (Node*)malloc(sizeof(Node));
    newNode->data.day = JEntry->day;
    newNode->data.month = JEntry->month;
    newNode->data.year = JEntry->year;
    strcpy(newNode->data.entry, JEntry->entry);


    if (list == NULL) {
        list->head = newNode;
    }
    else {
        newNode->next = list->head;
        list->head = newNode;
    }
    printf("Inserted Entry = %d/%d/%d :%s\n", newNode->data.day, newNode->data.month, newNode->data.year, newNode->data.entry);
}

and this is

fscanf(fo, "%d", &numEntry);
journal = (Journal *)malloc(numEntry * sizeof(Journal));

for (i=0; i<numEntry;i++) {
    fscanf(fo, "%d/%d/%d\n", &journal[i].day, &journal[i].month, &journal[i].year);
    fgets(journal[i].entry, SIZE, fo);

    }

    insertStart(list, journal);
    index = atoi(argv[1]);
    printf("%d-%02d-%02d: %s\n", journal[index].year, journal[index].month, journal[index].day, journal[index].entry);
    free(journal);
    fclose(fo);
}

this is my struct


#define SIZE 102
typedef struct {
    int day;
    int month;
    int year;
    char entry[SIZE];
} Journal;


typedef struct Node {
    Journal data;
    struct Node *next;
} Node;

typedef struct {
    Node *head;
} LinkedList;

if I put the insertStart in the for loop, yes it entered 4 times, but its the same result over and over again.

if i put it to where it is shown above, it will only run 1 time.

my question now is how can i store the rest of the entry into the linked list? thank you

this is the content of the text file

4
12/04/2010 
Interview went well I think, though was told to wear shoes. 
18/04/2010 
Doc advised me to concentrate on something... I forget. 
03/05/2010 
Was asked today if I was an art exhibit. 
19/05/2010 
Apparently mudcakes not made of mud, or angry wasps.

the first number 4 is the number of entries in case anyone's wondering.

Upvotes: 0

Views: 295

Answers (2)

AndersK
AndersK

Reputation: 36092

Have you considered just storing JEntry in the node instead of making a copy?

typedef struct 
{
  Journal* data;
  struct Node* next'
} 
Node;

That way this can be simplified

void insertStart (LinkedList *list, Journal *JEntry) 
{
  Node* newNode = malloc(sizeof(Node));
  newNode->data = JEntry;
  newNode->next = NULL;

instead of having a

struct { Node* head } LinkedList

just declare it

Node* list;

if you want to change the pointer pass it in as a double pointer

void insertStart( Node** list, Journal* JEntry)  /* insertStart(&list,journal + i) */

If list already has an entry, make sure your new node points to that

if (*list != NULL) 
{
  newNode->next = *list;
}

Now make your list point to the new node

*list = newNode;

When printing use -> for JEntry as well.

printf("Inserted Entry = %d/%d/%d :%s\n", newNode->data->day, newNode->data->month, newNode->data->year, newNode->data->entry);

Upvotes: 1

Daniel Walker
Daniel Walker

Reputation: 6772

First, you need to set list->head to NULL when you create it (or use calloc instead of malloc).

However, your problem comes from the line insertStart(list,journal);. Although journal is the pointer to the beginning of an array of Journal objects, insertStart doesn't know that. It dereferences the pointer and only adds the first journal entry to the list.

Upvotes: 1

Related Questions