gpt4 book ai didi

c 如何在子列表中插入一个元素

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:10 26 4
gpt4 key购买 nike

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

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

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

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; //puntatore al sottonodo prodotto
}categoria;

typedef categoria *listaC;

这是完整的代码:

#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”中一定有问题导致程序崩溃。会是什么?

最佳答案

问题就在这里,在 inserimentoSottolista 里面:

listaP prodotto = malloc(sizeof(prodotto));

您需要 sizeof(prodotto) 是之前声明的结构的大小,即 sizeof(struct nodoLista)。但是您对正在初始化的变量的名称使用了相同的名称。在这种情况下,sizeof(prodotto) 中的 prodotto 不是结构,而是变量。所以 sizeof(prodotto) 最终与 sizeof(listaP) 相同,后者只是指针的大小。它太小,所以您没有分配足够的内存。

您可以通过更改变量名称使其不掩盖类型名称或使用 sizeof(struct nodoLista) 来修复它。

关于c 如何在子列表中插入一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com