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;
}