-2

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();
}

1
  • PSA: If you can, use std::vector and stay away from direct allocations with new[]. 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. Commented Nov 7, 2023 at 19:50

1 Answer 1

1

If you run your code under a debugger you will see that the exception occurs because you do not check for null pointers in the display method.

You should always use the debugger when you hit an exception.

A debugger is a very powerful tool for helping diagnose problems with programs. And debuggers are available for all practical programming languages. Therefore, being able to use a debugger is considered a basic skill of any professional or enthusiast programmer. And using a debugger yourself is considered basic work you should do yourself before asking others for help. As this site is for professional and enthusiast programmers, and not a help desk or mentoring site, if you have a question about a problem with a specific program, but have not used a debugger, your question is very likely to be closed and downvoted. If you persist with questions like that, you will eventually be blocked from posting more.

The fixed code is:

void display()
{
    for (int i = 0; i < size; i++)
    {
        node *current = array[i].next;
        if (!current)
            continue;
        cout << current->data;
        while (current)
        {
            cout << "->";
            current = current->next;
            if (!current)
                break;
            cout << current->data;
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Have you run your code under a debugger? That is how you help yourself.
i want too diaplay like this 1->2->3->4->5 2->1
can you please send the corrected code i been tryinf for 2 dats
One last time: run your code under a debugger so you can fix it yourself. You need to learn how to use a debugger - it is a vital skill.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.