简体   繁体   English

我在 Turbo C++ 中使用 struct — 它给出的错误声明是不允许的

[英]I am using struct in Turbo C++ — it is giving error declaration is not Allowed Here

Error in Turbo C++: Turbo C++ 中的错误:

TURBO 中的错误

I compile this file in Turbo C++ but it takes many errors type of struct declaration is not allowed.我在 Turbo C++ 中编译了这个文件,但是它需要很多错误类型的结构声明是不允许的。

Here is the code:这是代码:

#include<stdio.h>
#include<malloc.h>

struct node{
    int data;
    struct node* left;
    struct node* right;
};

struct node* createNode(int data){
    struct node *n; // creating a node pointer
    n = (struct node *) malloc(sizeof(struct node)); //
    n->data = data;
    n->left = NULL;
    n->right = NULL;
    return n;
}
void inOrder(struct  node* root){
    if(root!=NULL)
    {
        inOrder(root->left);
        printf("%d ", root->data);
        inOrder(root->right);
    }
}
struct node * search(struct node* root, int key){
    if(root==NULL)
    {
        return NULL;
    }
    if(key==root->data)
    {
        return root;
    }
    else if(key<root->data)
    {
        return search(root->left, key);
    }
    else{
        return search(root->right, key);
    }
}
void insert(struct node *root, int key){
    struct node *prev = NULL;
    while(root!=NULL){
        prev = root;
        if(key==root->data){
            printf("Cannot insert %d, already in BST", key);
            return;
        }
        else if(key<root->data){
            root = root->left;
        }
        else{
            root = root->right;
        }
    }
    struct node* new = createNode(key);
    if(key<prev->data){
        prev->left = new;
    }
    else{
        prev->right = new;
    }
}
struct node *inOrderPredecessor(struct node *root){
    root = root->left;
    while (root->right!=NULL)
    {
        root = root->right;
    }
    return root;
}
struct node *deleteNode(struct node *root, int value)
{
    struct node* iPre;
    if (root == NULL){
        return NULL;
    }
    if (root->left==NULL&&root->right==NULL)
    {
        free(root);
        return NULL;
    }
    if (value < root->data){
        root-> left = deleteNode(root->left,value);
    }
    else if (value > root->data){
        root-> right = deleteNode(root->right,value);
    }
    //deletion strategy when the node is found
    else{
        iPre = inOrderPredecessor(root);
        root->data = iPre->data;
        root->left = deleteNode(root->left, iPre->data);
    }
    return root;
}
int main(){
    struct node *p = createNode(8);
    insert(p,3);
    insert(p,1);
    insert(p,6);
    insert(p,7);
    insert(p,10);
    insert(p,14);
    insert(p,4);
    inOrder(p);
    printf("\n After Deleting NOde\n");
    deleteNode(p,4);
    inOrder(p);
    struct node* n = search(p, 6);
    if(n!=NULL){
        printf("\nFound Element : %d", n->data);
    }
    else{
        printf("Element not found");
    }
    return 0;
}

This code runs smoothly in VSCODE but I want to run in Turbo C++.此代码在 VSCODE 中运行顺畅,但我想在 Turbo C++ 中运行。

How can I fix it?我该如何解决?

I am a beginner in data structures.我是数据结构的初学者。 A struct is a type consisting of a sequence of members whose storage is allocated in an ordered sequence (as opposed to union, which is a type consisting of a sequence of members whose storage overlaps)结构是由一系列成员组成的类型,其存储按有序序列分配(与联合相反,联合是由存储重叠的成员序列组成的类型)

Turbo C++ is extremely obsolete compiler. Turbo C++ 是极其过时的编译器。 It supports very old C.它支持非常旧的 C。 It requires all variable declarations to be placed in the first lines after opening braces.它要求所有变量声明都放在大括号后的第一行。 For example例如

struct node* new = createNode(key);

Should be split应该分开

struct node* new = NULL;  // goes up to struct node* next = NULL;
...
new = createNode(key);  // initialization in a desired line
void insert(struct node *root, int key){
    struct node *prev = NULL;
    struct node* new = NULL; // <--
    while(root!=NULL){
        prev = root;
        if(key==root->data){
            printf("Cannot insert %d, already in BST", key);
            return;
        }
        else if(key<root->data){
            root = root->left;
        }
        else{
            root = root->right;
        }
    }
    new = createNode(key); // <--
    if(key<prev->data){
        prev->left = new;
    }
    else{
        prev->right = new;
    }
}

Split similarly all late declarations with initializations to early declarations and late initializations.将所有带有初始化的后期声明类似地拆分为早期声明和后期初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM