什么时候应该使用struct node*head=NULL和struct node*head=NULL?

什么时候应该使用struct node*head=NULL和struct node*head=NULL?,c,pointers,struct,C,Pointers,Struct,我已经构建了一个结构 struct node { int key; struct node *left,*right; } struct节点*a意味着我们构造了节点的指针变量,struct node*a做什么?它是否指向变量a // C program to demonstrate insert operation in binary search tree #include<stdio.h> #include<stdlib.h> struct node {

我已经构建了一个结构

struct node
{
int key;
struct node *left,*right;
}

struct节点*a
意味着我们构造了节点的指针变量,
struct node*a做什么?它是否指向变量a

// C program to demonstrate insert operation in binary search tree
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int key;
    struct node *left, *right;
};

// A utility function to create a new BST node
struct node *newNode(int item)
{
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

// A utility function to do inorder traversal of BST
void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d \n", root->key);
        inorder(root->right);
    }
}

/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL) return newNode(key);

    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left  = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);   

    /* return the (unchanged) node pointer */
    return node;
}

// Driver Program to test above functions
int main()
{
    /* Let us create following BST
              50
           /     \
          30      70
         /  \    /  \
       20   40  60   80 */
    struct node *root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);

    // print inoder traversal of the BST
    inorder(root);

    return 0;
}

我们什么时候把*放在节点附近,这意味着什么?

这个
*
的间距并不重要
struct node*a
struct node*a
struct node*a
都是等效的

它总是声明指向结构节点的指针


但是,您应该谨慎,不要像那样在同一行上声明多个变量。特别是,
struct节点*a,b
a
声明为指针,将
b
声明为结构的实例。你做得对,但这是一种危险的语法,我通常倾向于避免,因为它容易出错。

struct node*a;
表示我们构造了节点的结构变量“不,这表示我们构造了一个指针变量,而不是结构变量。”它指向变量a吗?“是的,如果变量
a
的指针被分配给它。从问题标题来看,
struct node*head=NULL
struct node*head=NULL
是相同的。通常将空格放在
*
之前,而不是之后,这样用法就更清楚了,就像在
struct
成员
struct节点*左、*右两者都需要一个
*
。即使将空格放在第一个
*
之后,
*right
成员仍然需要一个星形,如
结构节点*左、*右。还请注意,在变量/函数的声明/定义中使用的
*
与反引用运算符
*
有所不同。这里真正的重复不是:?
struct node *newNode(int item)