the code terminates before i even initialize the inputs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include<map>
#include<iostream>
#include<vector>
int N=0;
struct Node{
int index;
std::vector<Node> * Linked;
};
class tree
{
public :
tree();
void Add(int,int); //search then add
Node* search(Node *current,int key); //return the pointer of the node
int countNodes(Node * current,int Nodes); // count the nodes of the tree or subtrees;
private:
Node * Root;
};
tree::tree()
{
Root->index = -1;
}
int tree::countNodes(Node * current,int Nodes) //DFS
{
for(int i = 0 ; i < current->Linked->size();i++)
{
Nodes++;
tree::countNodes(&(current->Linked->at(i)),Nodes);
}
return ++Nodes;
}
void tree::Add(int key , int newnode)
{
Node * x = search(Root , key);
if(x)
{
Node * Newnode = new Node;
Newnode->index = newnode ;
x->Linked->push_back(*Newnode);
}
else if(key == 0 && Root->index != 0)
{
Node * temp = new Node;
Node * root = new Node;
*temp = *Root;
*Root = *root;
*root = *temp;
Root->index =key;
Root->Linked->push_back(*root);
}
else
{
Root->index =key;
Node * NN = new Node;
NN->index = newnode ;
Root->Linked->push_back(*NN);
}
}
Node* tree::search(Node *current,int key)
{
if(current->index == key)
{
return current;
}
for(int i = 0 ; i < current->Linked->size() ; i++){
tree::search(&(current->Linked->at(i)),key);
}
return nullptr;
}
int main()
{
tree Tree;
std::cin>>N;
int x = N;
int key;
int newnode_val;
for(int i = 0 ; i < N ; i++)
{
std::cin >> key ;
std::cin >> newnode_val;
Tree.Add(key,newnode_val);
}
}
|
The problem is on line 23. You access Root when it is a nullptr
alright so now it terminates the code after i input
5
1 2
although i made a loop in the main function and my code doesn't seem to care ..
about it being a pointer ,thanks i thought vector<Node*>*Linked is a vector of pointers but i think this is the right one vector<Node*> Linked
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include<map>
#include<iostream>
#include<vector>
int N=0;
struct Node{
int index;
std::vector<Node*> Linked;
};
class tree
{
public :
tree();
void Add(int,int); //search then add
Node* search(Node *current,int key); //return the pointer of the node
int countNodes(Node * current,int Nodes); // count the nodes of the tree or subtrees;
private:
Node * Root;
};
tree::tree()
{
Root = new Node;
Root->index = -1;
Root->Linked = {nullptr};
}
int tree::countNodes(Node * current,int Nodes) //DFS
{
for(int i = 0 ; i < current->Linked.size();i++)
{
Nodes++;
tree::countNodes(current->Linked.at(i),Nodes);
}
return ++Nodes;
}
void tree::Add(int key , int newnode)
{
Node * x = search(Root , key);
if(x)
{
Node * Newnode = new Node;
Newnode->index = newnode ;
x->Linked.push_back(Newnode);
}
else if(key == 0 && Root->index != 0)
{
Node * temp = new Node;
Node * root = new Node;
*temp = *Root;
*Root = *root;
*root = *temp;
Root->index =key;
Root->Linked.push_back(root);
}
else
{
Root->index =key;
Node * NN = new Node;
NN->index = newnode ;
Root->Linked.push_back(NN);
}
}
Node* tree::search(Node *current,int key)
{
if(current->index == key)
{
return current;
}
for(int i = 0 ; i < current->Linked.size() ; i++){
tree::search((current->Linked.at(i)),key);
}
return nullptr;
}
int main()
{
tree Tree;
std::cin>>N;
int x = N;
int key;
int newnode_val;
for(int i = 0 ; i < N ; i++)
{
std::cin >> key ;
std::cin >> newnode_val;
Tree.Add(key,newnode_val);
}
}
|
VS 2015 let me input 5 values but then I get an exception at line 68 - current is nullptr.
> Root->Linked = {nullptr};
that creates a vector of size 1. it has one element, a null pointer.
Netbeans is a piece of junk , im downloading VS2015 <3
Thank you Ne55 and Thomas .
Topic archived. No new replies allowed.