Struct to Class - c++

I'm trying to convert a c program to c++ basically a struct data type to a class but is keeps showing " ‘first’ was not declared in this scope". whereas c program is working just fine.
Original code
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
}*first=NULL;
void Display(struct Node *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void Insert(struct Node *p,int index,int x)
{
struct Node *t;
int i;
if(index < 0 || index > 9)
return;
t=(struct Node *)malloc(sizeof(struct Node));
t->data=x;
if(index == 0)
{
t->next=first;
first=t;
}
else
{
for(i=0;i<index-1;i++)
p=p->next;
t->next=p->next;
p->next=t;
}
}
int main()
{
Insert(first,0,5);
Insert(first,1,10);
Insert(first,2,15);
Display(first);
return 0;
}
edited Code
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void Display(struct Node *p){
while(p!=NULL){
std::cout << p->data << std::endl;
p=p->next;
}
}
void Insert(struct Node *p,int index,int x){
Node *t;
int i;
if(index < 0 || index > 9)
return;
t=new Node;
t->data=x;
if(index == 0){
t->next=first;
first=t;
}
else
{
for(i=0;i<index-1;i++)
p=p->next;
t->next=p->next;
p->next=t;
}
}
int main()
{
Node *first=NULL; //
Insert(first,1,10);
Insert(first,2,15);
Display(first);
return 0;
}
error is something related to "Node *first = Null" line something to do with global pointer
Insert(first,0,5);
error I'm getting.

In the C program you declared variable first in the file scope.
struct Node
{
int data;
struct Node *next;
}*first=NULL;
So it is visible in all functions defined after the structure definition.
In the C++ program you declared the variable first in a block scope of main
int main()
{
Node *first=NULL;
//...
So it is invisible inside the function Insert in this code snippet
if(index == 0){
t->next=first;
first=t;
}
In any case the function Insert is invalid because at least there is no check whether p is equal to nullptr in this for loop
for(i=0;i<index-1;i++)
p=p->next;
that can result in undefined behavior.
The first function parameter should have a referenced type as for example
void Insert(struct Node * &p,int index,int x){
In this case you could write for example
if(index == 0){
t->next=p;
p=t;
}
and the pointer first defined in main will be changed.
And if you want to rewrite the list in C++ then the provided functions should be declared as member functions of the class.

Related

How do I traverse a tree in-order and just get one value at a time instead of all values in C++

I'm trying to traverse a binary tree inorder and the problem I'm trying to solve requires me to return one value at a time. The problem with binary tree traversal is that you get everything at once using recursion.Don't get me wrong, I want everything but not at once.
What I tried implementing an array to store every value and then loop through and get each value.
But this too does not seem to work, CPP is complaining that "undefined reference to `IPAddressAnalyzer::nodesArray'"
Here's a snippet of my code:
struct node
{
int address;
int count;
node* left;
node* right;
};
class IPAddressAnalyzer{
private:
node* root;
static node *nodesArray;
int arrayIndex = 0;
void destroy_tree(node *leaf);
void insert(int ip, int count, node *leaf);
void inorder_print(node *leaf);
And here's where I'm trying to use the array:
void IPAddressAnalyzer::inorder_print(node* leaf)
{
if(leaf != NULL)
{
inorder_print(leaf->right);
nodesArray[arrayIndex].address = leaf->address;
nodesArray[arrayIndex].count = leaf->count;
updateArrayIndex();
inorder_print(leaf->left);
}
}
Here's where I create the array, access the elements in the array and try to write to a file.
//Create the array
tree->createArray(intCounter);
tree->inorder_print();
//Traverse tree and write to a file
int rank =1;
int counter = 0;
int valueHolder = 0;
int nodeIndex = 0;
while (rank<=n){
node element = nodesArray[nodeIndex];
printf("Popped ip: %s count: %d\n", IPAddressToString(element.address), element.count);
if(counter == 0) {
fprintf(outFileStream, "%d, %s, %d\n", rank, IPAddressToString(element.address), element.count);
valueHolder = element.count;
counter++;
}
else if(element.count == valueHolder)
{
fprintf(outFileStream, "%d, %s, %d\n", rank, IPAddressToString(element.address), element.count);
}
else{
rank++;
if(rank>n)
break;
fprintf(outFileStream, "%d, %s, %d\n", rank, IPAddressToString(element.address), element.count);
valueHolder = element.count;
}
nodeIndex++;
}
Please note that I set the size of the array size in the main function before I use it.
Or, to put it simply, here's an example of what I want;
#include <iostream>
using namespace std;
struct node
{
int value;
node *left;
node *right;
};
class btree
{
public:
btree();
~btree();
void insert(int key);
void destroy_tree();
void inorder_print();
private:
void destroy_tree(node *leaf);
void insert(int key, node *leaf);
void inorder_print(node *leaf);
node *root;
};
btree::btree()
{
root = NULL;
}
btree::~btree()
{
destroy_tree();
}
void btree::destroy_tree(node *leaf)
{
if(leaf != NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void btree::insert(int key, node *leaf)
{
if(key < leaf->value)
{
if(leaf->left != NULL)
{
insert(key, leaf->left);
}
else{
leaf->left = new node;
leaf->left->value = key;
leaf->left->left = NULL;
leaf->left->right = NULL;
}
}
else if(key >= leaf->value)
{
if(leaf->right != NULL)
{
insert(key, leaf->right);
}
else
{
leaf->right = new node;
leaf->right->value = key;
leaf->right->right = NULL;
leaf->right->left = NULL;
}
}
}
void btree::insert(int key)
{
if(root != NULL)
{
insert(key, root);
}
else
{
root = new node;
root->value = key;
root->left = NULL;
root->right = NULL;
}
}
void btree::destroy_tree()
{
destroy_tree(root);
}
void btree::inorder_print()
{
inorder_print(root);
cout << "\n";
}
void btree::inorder_print(node *leaf)
{
if(leaf != NULL)
{
inorder_print(leaf->left);
cout << leaf->value << ",";
inorder_print(leaf->right);
}
}
int main(){
//btree tree;
btree *tree = new btree();
tree->insert(10);
tree->insert(6);
tree->insert(14);
tree->insert(5);
tree->insert(8);
tree->insert(11);
tree->insert(18);
tree->inorder_print();
delete tree;
}
This produces the following output at once:
5,6,8,10,11,14,18,
How can I get 5, then 6, then 8 etc, but each at a time, instead of all at once?
Any help offered will be appreciated!
CPP is complaining that "undefined reference to IPAddressAnalyzer::nodesArray"
This is probably because nodesArray is a static member variable, but you never declared storage for it. In some .cpp file, preferably one related to IPAddressAnalyzer, you should add the following line:
node *IPAddressAnalyzer::nodesArray;
But maybe just making it a non-static member would be even better.
I suggest you make use of the standard library instead of implementing your own tree structure, and use std::map and/or std::set instead. Your example of what you want can be rewritten like so:
#include <iostream>
#include <set>
int main(){
std::set<int> tree;
tree.insert(10);
tree.insert(6);
tree.insert(14);
tree.insert(5);
tree.insert(8);
tree.insert(11);
tree.insert(18);
for (auto &element: tree) {
std::cout << element << ',';
}
std::cout << '\n';
}

insertion in binary search tree (C++)

I'm new to data structure.
I'm making a C++ program for insertion of an element in binary search tree .
The program compiles without any error but when I'm running the program , after giving the first input n , the program stops working.
Kindly help me in making this program work properly.
My code follows up as:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node *root=NULL ,*par=NULL ,*pos=NULL,*save=NULL , *ptr=NULL ;
struct node* newNode(int data)
{
struct node* newnode= (struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->right=NULL;
newnode->left=NULL;
return newnode;
}
void findpos(int data)
{
if(root==NULL)
{
par=NULL,pos=NULL;
return;
}
if(root->data==data)
{
par=NULL , pos=root;
return;
}
if(data<root->data)
{
save=root; ptr=root->left;
}
else{
save=root; ptr=root->right;
}
while(ptr!=NULL)
{
if(ptr->data==data)
{
par=save; pos=ptr;
return;
}
if(data<ptr->data)
{
save=ptr;
ptr=ptr->left;
}
else{
save=ptr;
ptr=ptr->right;
}
}
pos=NULL; par=save;
return;
}
void insert(int data)
{
findpos(data);
if(pos!=NULL)
{
return;
}
pos=newNode(data);
if(data<par->data)
par->left=pos;
else
par->right=pos;
return;
}
int main()
{
struct node *root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
The root pointer declared in the main() override the global root pointer. Then at the first findpos(), the root (global) is still NULL.
So, simply replace that code:
int main()
{
struct node *root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
By this one:
int main()
{
root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
Changed the main method so that it modifies root in global namespace; before you were declaring a new node called root localy within main.
int main() {
// struct node *root = newNode(4); // you made this root a member of main.
// not a global member anymore.
root = newNode(4); // use this
// Populate some nodes
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
/* tree so far:
* 4
* / \
* 3 6
* /
* 2
*/
int n;
cin >> n;
cout << "user input " << n << "\n";
insert(n);
cout << pos->data; //just trying to see if it works
return 0;
}

Binary Search Tree implementation in C++

#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* left_child;
Node* right_child;
Node(int x){
data = x;
left_child = NULL;
right_child = NULL;
}
};
class BST{
public:
//Initially root is null
Node* root = NULL;
void insert(Node* node, int data){
if(node == NULL){
node = new Node(data);
return;
}
if(data < node->data){
insert(node->left_child,data);
}
else if(data > node->data){
insert(node->right_child,data);
}
}
void just_insert(int data){
insert(root,data);
}
void print(Node* node){
if(node == NULL){
return;
}
cout<<node->data<<" ";
print(node->left_child);
print(node->right_child);
}
void just_print(){
print(root);
}
};
int main() {
//For fast IO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,x;
cin>>n;
BST bst = BST();
for(int i=0; i<n; i++){
cin>>x;
bst.just_insert(x);
}
bst.just_print();
return 0;
}
What is wrong with this implementation of BST ? I am giving 8 values as input:
8
3
5
1
6
8
7
2
4
But when I invoke the print function. I do not get any output.
Am I missing out on some pointer logic ? The insert function goes recursively down the tree, to find a place to insert the value
The print function also works recursively.
Lets take a look at these lines from the insert function:
if(node == NULL){
node = new Node(data);
return;
}
The problem here is that the argument node is passed by value and is like any other local variable, and like any other local variable it will go out of scope once the function returns, and all changes to the variable will be lost.
What you need is to pass the pointer by reference, like
void insert(Node*& node, int data){ ... }
// ^
// Note ampersand here
You never assign to root in your BST class because your assignment to node in the insert class is not visible outside the insert function. You can fix this by passing the Node pointer by reference to the insert function:
void insert(Node*& node, int data)

Inserting data in a singly link list by specifying the nth node position

So the logic goes like this:
Suppose the link list consists of (6,7,8) as data and I pass insert(1,5),so the list will be as (5,6,7,8).
Similarly on insert(3,2) link list is (6,7,2,8).
I tried compiling the below code but it gives me an error stating-
Undefined reference to main by '-start'
I tried debugging,even searching for answers but found no help.Kindly suggest a solution.Any further suggestions and bug fixes shall be welcomed.
(I have used codepad for compiling)
#include<iostream>
using namespace std;
class Link_no
{
struct node
{
int data;
node *next;
};
void insert(int n,int d,node *head)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print(node *start)
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int main()
{
node *head=NULL;
Link_no o1;
o1.insert(1,5,head);
o1.insert(2,7,head);
o1.insert(1,9,head);
o1.print(head);
return 0;
}
}
C++ isnt java, the main does not belong inside a class. The compiler complains because there is no int main() in your code only a int Link_no::main() but that is not the entry point of the program.
Take out int main() from class Link_no. Take out struct node from class Link_no. It should compile.
The following compiles
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print()
{
node *temp=head;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.insert(2,7);
o1.insert(1,9);
o1.print();
return 0;
}
It does not completely do what you want yet only prints out 5 and 9 as data so you need to debug some more.
Edit:
I suggest you take a paper and pen and manually try to do what you're doing in your else since there is something going wrong there.
If you can't find it out on your own the following works for me, I haven't tried testing for extreme cases yet.
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
{
cout << "foo" << endl;
temp1=head;
for(int i=1;i<n-1;i++)
{
temp1=temp1->next;
}
node *temp2 = temp1->next;
temp1->next = temp;
temp->next=temp2;
}
}
void print()
{
node *temp=head;
cout << "link" << endl;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.print();
o1.insert(2,7);
o1.print();
o1.insert(1,9);
o1.insert(2,6);
o1.print();
return 0;
}

Search function returns the same regardless of item present in the binary search tree

Here is my code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
char r;
} ;
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void inordertrav();
void inorder(tree_node *);
void postordertrav();
void postorder(tree_node *);
void preordertrav();
void preorder(tree_node *);
int search(tree_node * ,int);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *previous;
p->data=item;
p->left=NULL;
p->right=NULL;
previous=NULL;
if(isempty())
root=p;
else
{
tree_node *current;
current=root;
while(current!=NULL)
{
previous=current;
if(item<current->data)
current=current->left;
else
current=current->right;
}
if(item<previous->data)
previous->left=p;
else
previous->right=p;
}
}
int bst::search(tree_node* root,int data) {
int r;
if(root == NULL) {
// r='f';
return 0;
}
else if (root != NULL){
if(root->data == data) {
// r='t';
return 1;
}
}
else if(data <= root->data) {
return search(root->left,data);
}
else {
return search(root->right,data);
}
}
void main()
{
int digit;
bst b;
tree_node *root;
/*b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20); */
cout<<"insert the nodes in the BT";
cout<<"enter integer: to quit enter 0";
cin>>digit;
while (digit!=0)
{
b.insert(digit);
cin>>digit;
}
cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
int number;
cout<<"Enter number be searched\n";
cin>>number;
//If number is found, print "FOUND"
int c;
c=b.search(root,number);
cout<<"returned value"<<c;
if (c==1) cout<<"Found\n";
else cout<<"Not Found\n";
getch();
}
The search function is always returning the same value whether it is in the BST or not.
Please help me to figure out the error.
The above code has no compilation error.
All other functions except search function are working fine.
But the search function is not working as required to search whether the element is in the Binary Search tree or not.
Your code invoke UB.
tree_node *root;
...
c=b.search(root,number); // root is uninitialized
To solve this add a new function:
class bst
{
...
int search(tree_node * ,int);
int search(int v) {
return search(root, v);
}
};
Also in bst::search function:
else //if (root != NULL){ Comment this condition
if(root->data == data) {
// r='t';
return 1;
}
//} Comment this line
This condition is not only redundant but also make some code flow paths return without value.