Reputation: 1579
I have this code that was remade to be a project file. it runs fine before it was turned into a project file. the main error is linker error stating that some of the struct variables are being duplicated, which from my experience before would be fixed if i add some extern to it, but to my surprise it didn't to the trick and nothing really changed.
NOTE:
I am using turbo C as a compiler school required and what we use in school so yeah
the variables that are duplicated are:
root, temp, t2, and t1
Header:
#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
print:
#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));
}
}
}
insert:
#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;
}
delete:
#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);
}
main
#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;
}
}
}
I did not add anything in my main.c file
p.s I have omitted the other function as they maybe irrelevant in my dilemma here.
Upvotes: 0
Views: 489
Reputation: 40145
Header :
change
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
to
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
};
extern struct btnode *root, *temp, *t2, *t1;
main.c
add
struct btnode *root = NULL, *temp = NULL, *t2, *t1;
Upvotes: 2