C中的数字系列的列表列表。

huangapple go评论61阅读模式
英文:

List of lists of series of number in C

问题

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

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

struct list_serie *start = NULL;
struct list_serie *seri = NULL;
struct list_serie *previous = NULL;

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

请注意,你在代码中有一处错误。在声明 struct list_serie 的时候应该使用 struct serie

英文:

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

答案1

得分: 3

请显示错误信息以便我们更好地理解问题。

重要注意事项:

  1. NULL 需要 stdlib.h 头文件
  2. 访问指针使用 '->' 而不是 '.'
  3. serie* serie 不是结构的声明,而是变量。因此不能用作数据类型的替代。

你可以使用 list->serie 访问列表的系列。

struct serie* temp = list->serie; 
英文:

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-&gt;serie

struct serie* temp = list-&gt;serie;

huangapple
  • 本文由 发表于 2023年5月18日 02:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定