我将此代码重新编译为项目文件。它在变成项目文件之前运行正常。主要错误是链接器错误,声明某些结构变量正在被复制,如果我添加一些extern,我之前的经验会被修复,但令我惊讶的是它没有诀窍而且没有真正改变。< / p>
注:
我正在使用turbo C作为编译器所需的学校以及我们在学校使用的内容所以是的
重复的变量是:
root,temp,t2和t1
标题
#ifndef BSTDLL_H
#define BSTDLL_H
#include<stdio.h>
#include<stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void create();
void search(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
void print(struct btnode *t, int x, int i, int y);
#endif
打印
#include "BSTDLL.h"
#include<stdio.h>
#include<stdlib.h>
void print(struct btnode *t, int x, int i, int y){
i = i / 2 + 2;
if (root == NULL){
printf("No elements in a tree to display");
return;
}
if(y > 6){
return;
}else{
if (t->l != NULL){
print(t->l, (x - i), i, (y + 1));
}
gotoxy(x, y * 2);
printf("%d ", t->value);
if (t->r != NULL){
print(t->r, (x + i), i, (y + 1));
}
}
}
插入
#include "BSTDLL.h"
#include<stdio.h>
#include<stdlib.h>
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create(){
int data;
printf("Enter value: ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t){
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
删除:
#include "BSTDLL.h"
#include<stdio.h>
#include<stdlib.h>
/* To check for the deleted node */
void delete()
{
int data;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
主要
#include "BSTDLL.h"
#include<stdio.h>
#include<stdlib.h>
struct btnode *root = NULL, *temp = NULL, *t2, *t1;
void main(){
int ch;
clrscr();
while(1){
printf("\n\n\n\n\n\n\n\n\n\nOPERATIONS ---");
printf("\n1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Exit\n");
printf("\nChoice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
clrscr();
print(root, 40, 30, 2);
break;
case 2:
delete();
clrscr();
print(root, 40, 30, 2);
break;
case 3:
exit(0);
default :
printf("Wrong choice");
break;
}
}
}
我没有在main.c文件中添加任何内容
p.s我省略了其他功能,因为它们在我的困境中可能无关紧要。
答案 0 :(得分:2)
标题:
变化
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
到
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
};
extern struct btnode *root, *temp, *t2, *t1;
main.c
添加
struct btnode *root = NULL, *temp = NULL, *t2, *t1;