错误:在二叉搜索树的 C99 中,函数“elseif”的隐式声明无效我该如何解决这个问题?

问题描述

B_S_T.c:38:17: error: implicit declaration of function 'elseif' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                elseif(ptr == parptr->left)
                ^
B_S_T.c:38:44: error: expected ';' after expression
                elseif(ptr == parptr->left)
                                           ^
                                           ;
B_S_T.c:42:17: error: expected expression
                else
                ^
3 errors generated.

为什么这段代码给我这 3 个错误

#include <stdio.h> 
#include <stdlib.h>
struct BST
{
    struct BST *left;
    int item;
    struct BST *right;
};void del(struct BST **r,int data)
{
    struct BST *ptr,*parptr,*pred,*parpred;
    if (*r == NULL)
        printf("Underflow");
    else
    {
        parptr = NULL;
        ptr = *r;
        while (ptr->item != data && ptr != NULL)
        {
            if (ptr->item > data)
            {
                parptr = ptr;
            }
            else
            {
                parptr = ptr;
                ptr = ptr->right;
            }
        }
        if (ptr == NULL)
            printf("Data not found");
        else
        {
            if (ptr->left == NULL && ptr->right == NULL)
            {
                if (parptr == NULL)
                    *r = NULL;
                elseif(ptr == parptr->left)
                {
                    parptr->left = NULL;
                }
                else
                {
                    parptr->right = NULL;
                }
                free(ptr);
            }
            else if (ptr->left == NULL || ptr->right == NULL)
            {
                if (parptr == NULL)
                {
                    if (ptr->left != NULL)
                        *r = ptr->left;
                    else
                        *r = ptr->right;
                }
                if (ptr == parptr->left)
                {
                    if (ptr->left != NULL)
                        parptr->left = ptr->left;
                    else
                        parptr->left = ptr->right;
                }
                else
                {
                    if (ptr->left != NULL)
                        parptr->right = ptr->left;
                    else
                        parptr->right = ptr->right;
                }
                free(ptr);
            }
            else
            {
                pred = ptr->left;
                parpred = ptr;
                while (pred->right != NULL)
                {
                    parpred = pred;
                    pred = pred->right;
                }
                ptr->item = pred->item;
                if (pred == parpred->left)
                {
                    parpred->left = pred->left;
                }
                else
                {
                    parpred->right = pred->left;
                }
                free(pred);
            }
        }
    }
}
void insert(struct BST **r,int data)
{
    struct BST *n,*ptr;
    n = (struct BST *)malloc(sizeof(struct BST));
    n->item = data;
    n->left = NULL;
    n->right = NULL;
    if (*r == NULL)
        *r = n;
    else
    {
        ptr = *r;
        while (1)
        {
            if (ptr->item == data)
            {
                printf("Duplicate item cannot be inserted");
                free(n);
                break;
                //duplicate data
            }
            else if (ptr->item > data)
            {
                if (ptr->left == NULL)
                {
                    ptr->left = n;
                    break;
                }
                else
                    ptr = ptr->left;
                //insert in left sub tree
            }
            else
            {
                if (ptr->right == NULL)
                {
                    ptr->right = n;
                    break;
                }
                else
                    ptr = ptr->right;
                //insert in right sub tree
            }
        }
    }
}
//traversing
void postorder(struct BST *root)
{
    if (root != NULL)
    {
        if (root->left != NULL)
            postorder(root->left);
        if (root->right != NULL)
            postorder(root->right);
        printf("%d ",root->item);
    }
}

void preorder(struct BST *root)
{
    if (root != NULL)
    {
        printf("%d ",root->item);
        if (root->left != NULL)
            preorder(root->left);
        if (root->right != NULL)
            preorder(root->right);
    }
}

void inorder(struct BST *root)
{
    if (root != NULL)
    {
        if (root->left != NULL)
            inorder(root->left);
        printf("%d ",root->item);
        if (root->right != NULL)
            inorder(root->right);
    }
}

int main()
{
    struct BST *root = NULL;
    int data = 50;
    insert(&root,50);
    insert(&root,70);
    insert(&root,30);
    insert(&root,60);
    insert(&root,40);
    insert(&root,10);
    del(&root,data);
    printf("\nPreorder: ");
    preorder(root);
    printf("\nIorder: ");
    inorder(root);
    printf("\npostorder: ");
    postorder(root);
    return 0;
}

解决方法

将“elseif”替换为“else if”。