簡體   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