C 链表为什么我的列表头变量仍然为空(C 的新手) [英] C linked list why is my list head variable remaining null (new to C)

查看:48
本文介绍了C 链表为什么我的列表头变量仍然为空(C 的新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有关此链接列表问题的其他问题上获得了很大的帮助.我目前的问题是,列表的开头保持为空,因此我实际上无法将任何节点链接到该列表或将任何内容输出到控制台.

i received great help on my other questions in relation to this link list problem. My current problem is now that the head of the list remains null so i cannot actually link any nodes to it or print anything out to the console.

问题是insert_node函数,因此当调用print时,由于head为null,因此while循环不会执行.我已经通过调试器解决了这个问题,它的绝对地址为0x0,它为null.

The problem is the insert_node function, so when print is called the while loop doesn't execute since head is null. I've got through it through the de bugger and it is definitely null it has the address 0x0.

这是另一个malloc问题吗?我对此还不太好.

Is this another malloc issue? im not too great on that yet.

代码:

    /* 
 * File:   main.c
 * Author: che16
 *
 * Created on 20 November 2013, 08:59
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "structure.h"

/*
 * 
 */

node* head = NULL;

int main(int argc, char** argv) {

    int no;

    printf("enter amount of books \n");

    scanf("%d", &no);

    create_books(no);
    print_list(head);


    return (EXIT_SUCCESS);

}

node* create_books(int no_of_books) {
    char title[50];
    char author[30];
    unsigned int number;
    int i;

    for (i = 0; i < no_of_books; i++) {
        node* new_node;
        new_node = (node *) malloc(sizeof (node));
        printf("enter book title \n");
        scanf("%s", title);
        printf("enter author name \n");
        scanf("%s", author);
        printf("enter ISDN number \n");
        scanf("%10u", &number);

        strncpy(new_node->btitle, title, 40);
        strncpy(new_node->name, author, 40);

        new_node->isbn = number;
        new_node->n = NULL;


        insert_node(head, new_node);
    }
}

void insert_node(node* head, node* insert) {
    printf("insert called \n");
    insert->n = NULL;

    if (head == NULL) {
        head = insert;
    } else {
        node* curr = head;

        while (curr->n != NULL) {
            curr = curr->n;
        }
        curr->n = insert;
    }
    printf("finished called \n");

}

void delete_node(node* head, node * node) {

}

void print_list(node * head) {
    while (head) {
        printf("%s: \"%s\" (%u)\n", head->btitle, head->name, head->isbn);

        head = head->n;
    }
}

解决方案

/* 
 * File:   main.c
 * Author: che16
 *
 * Created on 20 November 2013, 08:59
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "structure.h"

/*
 * 
 */

node** head = NULL;

int main(int argc, char** argv) {

    int no;

    printf("enter amount of books \n");

    scanf("%d", &no);

    create_books(no);
    print_list(head);


    return (EXIT_SUCCESS);

}

node* create_books(int no_of_books) {
    char title[50];
    char author[30];
    unsigned int number;
    int i;

    for (i = 0; i < no_of_books; i++) {
        node* new_node;
        new_node = (node *) malloc(sizeof (node));
        printf("enter book title \n");
        scanf("%s", title);
        printf("enter author name \n");
        scanf("%s", author);
        printf("enter ISDN number \n");
        scanf("%10u", &number);

        strncpy(new_node->btitle, title, 40);
        strncpy(new_node->name, author, 40);

        new_node->isbn = number;
        new_node->n = NULL;


        insert_node(&head, new_node);
    }
}

void insert_node(node** head, node* insert) {
    printf("insert called \n");
    insert->n = NULL;

    if (*head == NULL) {
        *head = insert;
    } else {
        node* curr = *head;

        while (curr->n != NULL) {
            curr = curr->n;
        }
        curr->n = insert;
    }
    printf("finished called \n");

}

void delete_node(node* head, node * node) {

}

void print_list(node * head) {
    while (head) {
        printf("%s: \"%s\" (%u)\n", head->btitle, head->name, head->isbn);

        head = head->n;
    }
}

推荐答案

问题是您要将 head 作为局部变量传递给了 insert_node 函数,而不是而不是使用全局的.

The problem is that you're passing head as a local variable into the insert_node function, rather than using the global one.

您在这里有三个选择:

  1. 使您所有的功能都作用于全局 head 上,而不是将其传递给周围(不推荐);

  1. Make all your functions act on the global head and not pass it around (not recommended);

head 作为 node ** 传递,以便函数可以更改其指向的 node * 值;

Pass head as a node**, so that functions can change the node* value it points to;

使用虚拟头策略,其中头是一个空列表节点,永远不会为NULL,并且永远不会更改.您使用它的 next 指针作为实际的列表头.这种方法倾向于使所有列表代码变得更简单,但会浪费一点内存.

Use a dummy head strategy where the head is an empty list node that is never NULL, and never changes. You use its next pointer as the actual list head. This approach tends to make all your list code much simpler at the expense of a little wasted memory.

这篇关于C 链表为什么我的列表头变量仍然为空(C 的新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆