【问题标题】:Linked List C with pointers带指针的链表 C
【发布时间】:2016-03-09 18:11:45
【问题描述】:

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

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

【问题讨论】:

    标签: c pointers linked-list stack


    【解决方案1】:

    首先你必须用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;
    }
    

    【讨论】:

      【解决方案2】:

      您没有分配 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.
      }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        您在 main 中声明了 lista,但没有初始化任何字段。我的猜测是 lista.pFirst 是非 NULL 垃圾,然后你 if 会被跳过。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-28
          • 2014-02-18
          • 2020-05-10
          • 2013-08-07
          • 1970-01-01
          • 2018-02-17
          相关资源
          最近更新 更多