gpt4 book ai didi

c - 带指针的链表 C

转载 作者:行者123 更新时间:2023-11-30 17:04:11 25 4
gpt4 key购买 nike

我试图将一个元素推送到链接列表中,然后打印它,但收到的问题是:段错误

我创建了 struct librocellalista。然后在函数 insHead 中,我尝试将一个元素插入到列表中,获取用户的输入,然后在函数 printList 中(这里我遇到了段错误)我会打印列表的元素。

代码是:

typedef struct libro {
char titolo[64];
char autore[32];
short inLibreria;
short fuoriLibreria;
short id;
} Libro;

typedef struct cella {
Libro libro;
struct cella *pNext;
} Cella;

typedef Cella *Pcella;

typedef struct listaLibri{
Cella *pFirst;
Cella *pLast;
} ListaLibri;

void insHead(ListaLibri lista){
Pcella cella;
Libro libro;
printf("Inserisci titolo libro: ");
scanf("%s", libro.titolo);
printf("Inserisci autore libro: ");
scanf("%s", libro.autore);
printf("Inserisci il numero di copie presenti in libreria: ");
scanf("%d",&libro.inLibreria);
if(lista.pFirst == NULL){
cella = NULL;
Pcella temp = cella;
cella = malloc(sizeof(Cella));
(*cella).libro = libro;
(*cella).pNext = temp;
lista.pFirst = cella;
lista.pLast = cella;
}
printList(lista);
}

void printList(ListaLibri *lista){
Pcella cella = lista->pFirst;
Pcella temp = cella;
while (temp != NULL){
printf("%s", temp->libro.autore);
temp = temp->pNext;
}
free(cella);
free(temp);
}

void main(){
ListaLibri lista;
insHead(lista);
}

最佳答案

首先,您必须使用 NULL 初始化 lista.pFirstlista.pLast

int main()
{
ListaLibri lista;
lista.pFirst = NULL; // <- init NULL
lista.pLast = NULL; // <- init NULL

insHead( &lista ); // <- pass pointer to list to function insHead
insHead( &lista );
.....

deleteList( &lista );
return 0;
}

函数 insHead 的输入参数必须是输入和输出参数。否则,您会将 lista 的副本传递给函数 insHead 并且永远不会取回其内容。

void insHead( ListaLibri *lista )
// ^ input and outout paramter
{
if ( lista == NULL )
return;

Cella *head = lista->pFirst; // remember head of list
lista->pFirst = malloc(sizeof(Cella)); // allocate new node right to target
lista->pFirst->pNext = head; // successor of new node is head of list
if ( lista->pLast == NULL ) // if list was empty new node is tail of list
lista->pLast = lista->pFirst;

Libro libro;
printf("Inserisci titolo libro: ");
scanf("%s", libro.titolo);
printf("Inserisci autore libro: ");
scanf("%s", libro.autore);
printf("Inserisci il numero di copie presenti in libreria: ");
scanf("%d",&libro.inLibreria);
lista->pFirst->libro = libro;

printList( lista );
}

不要删除打印功能中的节点。

void printList(ListaLibri *lista)
{
if ( lista == NULL )
return;

Pcella temp = lista->pFirst;
while (temp != NULL)
{
printf("%s\n", temp->libro.autore);
temp = temp->pNext;
}
}

编写一个删除列表的函数。

void deleteList(ListaLibri *lista)
{
if ( lista == NULL )
return;

Pcella temp = lista->pFirst;
while (temp != NULL) // terminate if tail of list is reached
{
Pcella next = temp->pNext; // rmember successor of node
free( temp ); // delete node
temp = next; // go to next node
}
lista->pFirst = NULL;
lista->pLast = NULL;
}

关于c - 带指针的链表 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35899360/

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