0

I am doing a program that let's me take in input a dim number of series, any of them has got a name. This is how I coded them:

struct lista // lista means list
{
    char nome[30]; // nome means name
    struct serie *serie;
    struct lista *prossima; // prossima means next
};

struct serie
{
    int n;
    struct serie *prossim;
};

I assigned this structs to make lista work in main() this way:

struct lista *inizio = NULL; // inizio means start
struct lista *list = NULL;
struct lista *precedente = NULL; // precedente means previous

How do I create struct to access struct serie *serie of list? I thought of doing it this way:

struct list.serie start = NULL;
struct list.serie seri = NULL;
struct list.serie previous = NULL;

but it doesn't work.
How should I type it?
And how do I free memory?

with the struct list I do

list = inizio;
while (list != NULL)
{
    struct lista *pros = list->prossima;
    free(list);
    list = pros;
}
list = NULL;

and how do I free serie list?

I tried as in the previous example how I created serie structs by typing:

struct list.serie start;

etc... but it doesn't compile because it says Expected an identifier For the deallocation I may apply the same way of how I did with the original list lista

I tried doing it without accessing to struct serie *serie, so only making the input of the name, so how I utilize lists seems to be OK because I can program a simple list, but a list of lists is the problem.

For anyone that want to see the entire code:

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

struct lista
{
    char nome[30];
    struct serie *serie;
    struct lista *prossima;
};

struct serie
{
    int n;
    struct serie *prossim;
};

int main()
{
    int dim;

    struct lista *inizio = NULL;
    struct lista *list = NULL;
    struct lista *precedente = NULL;

    printf("How many series do you want to add? ");
    scanf("%d", &dim);

    for (int i = 0; i < dim; i++)
    {
        list = malloc(sizeof(struct lista));

        printf("Enter the name of the series: ");
        scanf("%s", list->nome);

        list->prossima = NULL;

        struct list.serie start; // THIS IS WHERE I GET THE ERROR
                                 // HOW DO I USE list.serie to create lists?

        if (i == 0)
        {
            inizio = list;
        }
        else
        {
            precedente->prossima = list;
        }
        precedente = list;
    }
    
    list = inizio;
    while (list != NULL)
    {
        printf("The name of the series is: %s\n", list->nome);
        list = list->prossima;
    }
    
    list = inizio;
    while (list != NULL)
    {
        struct lista *pros = list->prossima;
        free(list);
        list = pros;
    }
    list = NULL;
    
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
ShishRobot
  • 113
  • 5
  • 1
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 17 '23 at 19:08
  • 1
    If you want to allocate memory for a new node in a linked list, the function [`malloc`](https://en.cppreference.com/w/c/memory/malloc) is usually used. That function is the counterpart to the function `free`. You can only `free` memory that has been allocated by `malloc` or a similar function. – Andreas Wenzel May 17 '23 at 19:11
  • 1
    Please note that Stack Overflow is a site for asking specific questions and getting specific answers, one question and answer at a time. You are asking several questions in the same question. Also, it appears to me that you need an entire tutorial on how to use linked lists. Therefore, I'm afraid that your question is too broad for Stack Overflow. You may want to buy a [book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) that teaches C. Linked lists are also explained in [week 5 of the CS50 course](https://cs50.harvard.edu/x/2023/weeks/5/). – Andreas Wenzel May 17 '23 at 19:29
  • You could ask what shorter sequences is the input a part of efficiently with a [prefix tree](https://en.wikipedia.org/wiki/Radix_tree) by collapsing like prefixes, _eg_, https://imgur.com/xpouBYD. – Neil May 18 '23 at 01:18

1 Answers1

3

Kindly do show the error messages so we can have better understanding of issue.

Important things to note

  1. NULL needs <stdlib.h> header
  2. Pointers are accessed with '->' and not '.'
  3. serie* serie is not a declaration of structure but a variable. So it can't be used instead of datatype

you can access series of list using list->serie

struct serie* temp = list->serie;
JustaNobody
  • 150
  • 8