简体   繁体   English

链接列表C与指针

[英]Linked List C with pointers

I'm trying to push an element in a linked list, then print it but the problem received is: segmentation fault . 我正在尝试将元素推入链表中,然后打印它,但是收到的问题是: segmentation fault

I've created struct libro , cella and lista . 我创建了struct librocellalista Then in function insHead i've tried to insert an element to the list, taking input from user and then in function printList (and here i've got the segmentation fault) i would print list's elements. 然后在insHead函数中,我尝试将一个元素插入列表,从用户那里获取输入,然后在函数printList (这里我遇到了分段错误),我将打印列表的元素。

The code is: 代码是:

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);
}

You are not assigning the pFirst value, so it is having garbage data, which is not equal to NULL . 您没有分配pFirst值,因此它具有垃圾数据,该数据不等于NULL Modify your code like this; 像这样修改您的代码;

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.
}

Hope this helps. 希望这可以帮助。

You declare lista in main, but you don't initialize any fields. 您在main中声明lista,但不初始化任何字段。 My guess is that lista.pFirst is non-NULL garbage and you if is then skipped. 我的猜测是lista.pFirst是非NULL垃圾,如果您随后将其跳过。

First of all you have to initialize lista.pFirst and lista.pLast with 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;
}

The input paramter to your function insHead has to be an in and output paramter. 函数insHead的输入参数必须是in和输出参数。 Otherwise you would pass an copy of lista to function insHead and never get back its content. 否则,您会将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 );
}

Don't delete nodes infunction print. 不要删除无效打印的节点。

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

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

Write a function which deletes the list. 写一个删除列表的函数。

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