首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C如何在子列表中插入元素

C如何在子列表中插入元素
EN

Stack Overflow用户
提问于 2018-01-07 03:06:48
回答 1查看 70关注 0票数 1

我在下面的"Magazzino.txt“文件中有几个类别的产品列表:

代码语言:javascript
复制
A451XX (codice prodotto)
CAT001 (codice categoria)
PASTA CONF. 1KG
100
99.0
A451XY (codice prodotto)
CAT002 (codice categoria)
MAGLIA LANA
25
6.70
A452XX (codice prodotto)
CAT001 (codice categoria)
SUGO
33
9.99

首先,我必须读取该文件,并复制具有以下结构的列表中的所有产品:

代码语言:javascript
复制
typedef struct {
    char codP[C];
    char codC[C];
    char descr[D];
    int num;
    float costo;
} tipoBaseLista;

typedef struct nodoLista{
    tipoBaseLista info;
    struct nodoLista *next;
}prodotto;

typedef prodotto *listaP;

我需要将此产品列表复制到类别列表中,以便每个类别都有一个子列表,其中包含属于特定类别的所有产品。该列表的结构如下:

代码语言:javascript
复制
typedef struct nodoCat{
    char codC[C];
    struct nodoCat *next;
    listaP nodoP; //puntatore al sottonodo prodotto
}categoria;

typedef categoria *listaC;

这是完整的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#define C 8
#define D 64

typedef struct {
    char codP[C];
    char codC[C];
    char descr[D];
    int num;
    float costo;
} tipoBaseLista;

typedef struct nodoLista{
    tipoBaseLista info;
    struct nodoLista *next;
}prodotto;

typedef prodotto *listaP;

typedef struct nodoCat{
    char codC[C];
    struct nodoCat *next;
    listaP nodoP; 
}categoria;

typedef categoria *listaC;

int carica_lista(char fName[], listaP *l);
void inserimentoProd(listaP *l, tipoBaseLista p);
listaC trovaCategoria(listaC lc, char categoria[]);
void inserimentoSottolista(listaC lc, tipoBaseLista p, listaP *l);

int main() {
    char filename[] = "Magazzino.txt";
    listaP lista = NULL;
    listaC listaCat = NULL;
    tipoBaseLista prodotto;

    printf("\nNumero prodotti caricati: %d\n", carica_lista(filename, &lista));

    if(lista == NULL){
        printf("\nLa lista dei prodotti è vuota!\n");
    }

    while(lista != NULL){
        prodotto = lista->info;
        if(listaCat == NULL){
            listaCat = malloc(sizeof(categoria));
            strcpy(listaCat->codC, prodotto.codC);
            listaCat->next = NULL;
            inserimentoSottolista(listaCat, prodotto, &lista);
        }
        else{
            listaCat = trovaCategoria(listaCat, prodotto.codC);
        if(listaCat != NULL){
                inserimentoSottolista(listaCat, prodotto, &lista);
        }
            else{
                listaCat = listaCat->next;
                inserimentoSottolista(listaCat, prodotto, &lista);
            }

        }

        lista = lista->next;
    }       
    return 0;
    system("PAUSE");
}

//read from file
int carica_lista(char fName[], listaP *l) {
    tipoBaseLista prodotto;
    int n = 0;
    char buf[D] = {0};
    char scarto[30];
    FILE *f; 
    f = fopen(fName, "r");
    if (f == NULL) {
        printf("Non e' possibile aprire il file\n");
        exit(1);
    }

    while (!feof(f)) {
        fgets(buf, sizeof(buf), f);
        sscanf(buf, "%s%s", prodotto.codP, scarto);
        fgets(buf, sizeof(buf), f);
        sscanf(buf, "%s%s", prodotto.codC, scarto);
        fgets(buf, sizeof(buf), f);
        strcpy(prodotto.descr, buf);
        fgets(buf, sizeof(buf), f);
        sscanf(buf, "%d", &prodotto.num);
        fgets(buf, sizeof(buf), f);
        sscanf(buf, "%f", &prodotto.costo);

        inserimentoProd(l, prodotto);
        n++;
    }

        fclose(f);
        return n;
        system("PAUSE");
}

//to insert product in the list
void inserimentoProd(listaP *l, tipoBaseLista p){
    listaP pCorrente = NULL; 
    listaP pNodo; 
    listaP pPrec;
    pNodo = malloc(sizeof(prodotto));
    pNodo->info = p;
    pNodo->next = NULL;
    if (*l == NULL){
        *l = pNodo;
    }

    else if(strcmp(p.codP, (*l)->info.codP) < 0){
        pNodo->next = *l;
        *l = pNodo;
        (*l)->next = pNodo;
    }

    else{
        pCorrente = *l; 
        while (pCorrente->next != NULL && strcmp(p.codP, pCorrente->info.codP) > 0){
            pPrec = pCorrente;
            pCorrente = pCorrente->next;
        }

        if(strcmp(p.codP, pCorrente->info.codP) < 0){
            pNodo->next = pCorrente;
            pPrec->next = pNodo;
        }
        else if(pCorrente->next == NULL) {
            pCorrente->next = pNodo;
        }
    }
}


//To find the category node under which we insert the sublist
listaC trovaCategoria(listaC lc, char categoria[]){
    listaC pCorrente = lc;
    while(pCorrente != NULL){
        if(strcmp(pCorrente->codC, categoria) == 0){
            printf("\nCategoria già presente.\n");
            return pCorrente;
        }
        pCorrente = pCorrente->next;
    }
    return(NULL);
}

//to insert the product in the head of sublist
void inserimentoSottolista(listaC lc, tipoBaseLista p, listaP *l){
printf("\nInserimento nella sottolista\n");
    listaP prodotto = malloc(sizeof(struct nodoLista));
    prodotto->info = p;
    prodotto->next = *l;
    *l = prodotto;
    lc->nodoP = prodotto;
    printf("\nInserimento effettuato\n");
}

一定是"inserimentoSottolista“出了问题,导致程序崩溃。那会是什么呢?

EN

回答 1

Stack Overflow用户

发布于 2018-01-07 03:18:30

问题出在这里,在inserimentoSottolista内部:

代码语言:javascript
复制
listaP prodotto = malloc(sizeof(prodotto));

您需要将sizeof(prodotto)设置为先前声明的结构的大小,即sizeof(struct nodoLista)。但是您使用了与正在初始化的变量的名称相同的名称。在本例中,sizeof(prodotto)中的prodotto不是结构,而是变量。所以sizeof(prodotto)最终和sizeof(listaP)一样,它只是一个指针的大小。它太小了,所以你没有分配足够的内存。

您可以通过更改变量名来修复它,这样它就不会屏蔽类型名,或者使用sizeof(struct nodoLista)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48130855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档