gpt4 book ai didi

C - 二分搜索树中 ")"之前应为 "*"

转载 作者:行者123 更新时间:2023-11-30 18:25:47 26 4
gpt4 key购买 nike

我正在用 c 编写一个二叉搜索树,并且在函数的声明和头文件中,我不断收到愚蠢的“错误:在 '*' 标记之前预期 ')' 消息。

主文件

#include <stdlib.h>
#include <stdio.h>
#include "func.h"

#define ARRAYSIZE 12

int main(int argc, char *argv[3]) {

typedef struct node {
int value;
struct node * left;
struct node * right;
} * node;

node* root = NULL;

int nodes[ARRAYSIZE] = {3,6,2,1,7,8,3,5,7,2,9,4};
int i;

for(i = 0; i < ARRAYSIZE; i++) {
root = insert(root, nodes[i]);
}


if (strcmp(argv[1], "q") == 0) quit();
if (strcmp(argv[1], "i") == 0) insert(argv[2]);
if (strcmp(argv[1], "d") == 0) delete(argv[2]);
if (strcmp(argv[1], "s") == 0) search(argv[2]);
if (strcmp(argv[1], "e") == 0) empty();

return 0;
}

函数文件

    #include "func.h"


node * createNode(int value) {
node * new_node = (node*)malloc(sizeof(node));

if(new_node == NULL) {
exit(1);
}

new_node->value = value;
new_node->left = NULL;
new_node->right = NULL;

return new_node;
}

void insert(node * root, int n){
if(root == NULL) {
root = createNode(n);
}
else {
int left = 0;
node * current = root;
node * previous = NULL;

while(current != NULL){
previous = current;

if((n == current->n) < 0) {
left = 1;
current = current->left;
}
else if((n == current->n) > 0) {
left = 0;
current = current->right;
}
}
if(left)
previous->left = createNode(n);
else
previous->right = createNode(n);
}
}

void delete(node * root, int n){
node *current, *parent, *successor, *presuccessor, q;

if (root->left == NULL) {
printf("\ntree is empty! (deleting)");
}

parent = root;
current = root->left;

while (current !=NULL && n != current->value) {
parent = current;
current = (n next) ? current->left : current->right;
/* maybe add the } here ;) */

if (current == NULL) {
printf("\n %d is missing \n", n);
}

// Item found, now delete it

if (current->left == NULL)
q = current->right;
else if(current->right == NULL)
q = cur->left;
else {
// Obtain the inorder successor and its parent
presuccessor = current;
current = current->left;
while (successor->left != NULL) {
presuccessor = successor;
successor = successor->left;
}
if (current == presuccessor) { /*situation 1*/
successor->left = current->right;
}
else { /*situation 2*/
successor->left = current->left;
presuccessor->left = successor->right;
successor->right = current->right;
}
q = successor;
}

if (parent->left == current)
parent->left = q;
else
parent->right = q;

freeNode(current);
}

void search(node * root, int n){
if (root == NULL){
return NULL;
}

node * current = root;

while (current != null){
if (current->value > n) {
current = current->left;
}
else if (current->value < n){
current = current->right;
}
else
printf("%d is present", n)
}

printf("n is missing");
}

void empty(node * root){
if(root != NULL) {
empty(root->left);
empty(root->right);
free(root);
}
}

/* Print the value at each node with a single space character */
/* Else if tree is empty print "tree is empty" */
/* Do this for the following functions */

/*void inTraversal(node * root){
if node == null
return;

inTraversal(node.left)
visit(node)
inorder(node.right)
}
void preTraversal(){
preorder(node)
if node == null then return
visit(node)
preorder(node.left)
preorder(node.right)
}
void postTraversal(){
postorder(node)
if node == null then return
postorder(node.left)
postorder(node.right)
visit(node)
}*/

int quit() {
exit(1);

return 0;
}

标题

#ifndef FUNC_H
#define FUNC_H

node * createNode(int value);
void insert(node * root, int n);
void search(node * root, int n);
void empty(node * root);
void delete(node * root, int n);
int quit();

#endif

最佳答案

大概是这样的:

    node * createNode(int value);

节点 此时未知。

node 的定义移至头文件中。

关于C - 二分搜索树中 ")"之前应为 "*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22746395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com