繁体   English   中英

链接列表C与指针

[英]Linked List C with pointers

我正在尝试将元素推入链表中,然后打印它,但是收到的问题是: segmentation fault

我创建了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);
}

您没有分配pFirst值,因此它具有垃圾数据,该数据不等于NULL 像这样修改您的代码;

void insHead(ListaLibri lista)
{
    // Your code goes here

    lista.pFirst = NULL;
    if(lista.pFirst == NULL)
    {
        // Your code goes here
    }
    printList(&lista); //Pass it as a reference, because printList() input parameter is a pointer type.
}

希望这可以帮助。

您在main中声明lista,但不初始化任何字段。 我的猜测是lista.pFirst是非NULL垃圾,如果您随后将其跳过。

首先,您必须使用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的输入参数必须是in和输出参数。 否则,您会将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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM