Why does my code throw an exception?
The code defines a class called Graph which represents a graph. A graph is a data structure that consists of nodes and edges. Nodes represent the vertices of the graph, and edges represent the connections between the nodes.
The Graph class has the following members:
array: An array of nodes. size: The size of the graph. j: A counter used to keep track of the next available index in the array. The Graph class provides the following methods:
addvertex(): Adds a vertex to the graph. add_edge_no_direction(): Adds an undirected edge between two vertices in the graph. display(): Displays the graph. The main() function creates a new Graph object with a size of 200. It then adds five vertices to the graph and four undirected edges between the vertices. Finally, it calls the display() method to display the graph
c
#include <iostream>
using namespace std;
struct node {
int data;
int weight;
node* next;
node() {}
node(int data, int weight) {
this->data = data;
next = nullptr;
this->weight = weight;
}
};
class Graph {
private:
node* array;
int size;
int j;
public:
Graph(int size) {
this->size = size;
array = new node[size];
j = 0;
for (int i = 0; i < size; i++) {
array[i].data = 0;
array[i].weight = 0;
array[i].next = nullptr;
}
}
bool addvertex(int value) {
for (int i = 0; i < size; i++) {
node* current = array[i].next;
while (current != nullptr) {
if (current->data == value) {
return false;
} else {
current = current->next;
}
}
}
node* newnode = new node(value, 0);
newnode->next = nullptr;
array[j].next = newnode;
j++;
return true;
}
bool add_edge_no_direction(int source, int destination, int weight) {
node* newnode1 = new node(source, weight);
node* newnode2 = new node(destination, weight);
// check if edge present
for (int i = 0; i < size; i++) {
if (array[i].data != source || array[i].data != destination) {
return false;
}
}
for (int i = 0; i < size; i++) {
node* current = array[i].next;
if (current->data == source || current->data == destination) {
if (current->next == nullptr) {
if (current->data == source) {
current->next = newnode2;
} else {
current->next = newnode1;
}
} else {
while (current->next != nullptr) {
current = current->next;
}
if (current->data == source) {
current->next = newnode2;
} else {
current->next = newnode1;
}
}
} else {
cout << "no way it comes here";
}
}
return true;
}
void display(){
for(int i=0;i<size;i++){
node* current=array[i].next;
cout<<current->data;
while(current){
cout<<"->";
current=current->next;
cout<<current->data;
}
}
}
};
int main() {
Graph g1(200);
g1.addvertex(1);
g1.addvertex(2);
g1.addvertex(3);
g1.addvertex(4);
g1.addvertex(5);
g1.add_edge_no_direction(1, 2, 100);
g1.add_edge_no_direction(1, 3, 150);
g1.add_edge_no_direction(1, 4, 200);
g1.add_edge_no_direction(1, 5, 250);
g1.display();
}
std::vectorand stay away from direct allocations withnew[]. These come with considerable responsibility, and one you seem to shirk here as you do not have a proper destructor, nor a copy constructor, meaning you're violating the Rule of Three.