Reputation: 762
My program crashes when trying to run the function initialize_graph. I have omitted out all the other functions. This is one of my first times working with vectors. The graph.h header will be below main. I just need someone to help me initialize the vector with a graph object whose head pointer is null. I will be able to take it from there. Thanks for looking.
#include <cstdlib>
#include <iostream>
#include <vector>
#include "graph.h"
using namespace std;
vector<graph*> adj_list; // one dimensional vector; each position in the vector stores a pointer to the head of a linked list
void graph::initialize_graph(int num_of_vertices, int num_of_edges)
{
cout << "Doing push back"; // want to push back the same obj. call it graph* graph_obj (where head is null)
//adj_list.resize(num_of_vertices);
graph *graph_obj;
graph_obj -> head = NULL;
for(int k = 0; k < num_of_vertices; k++)
{
adj_list.push_back(graph_obj);
}
cout << "Pushback complete";
}
int main()
{
int num_of_vertices, num_of_edges, vertex1, vertex2, function;
graph graph_obj;
while(1)
{
cout<<"1 - initialize graph" <<endl;
cout<<"2 - insert an edge to the graph" <<endl;
cout<<"3 - delete an edge from the graph" <<endl;
cout<<"4 - list all edges in the graph" <<endl;
cout<<"5 - list all of the neighbors for a particular vertex" << endl;
cout<<"6 - list all of the vertices with no incoming edges" << endl << endl;
cout<<"Choose a function (1 - 6): ";
cin>>function;
cout<<endl<<endl;
switch(function)
{
case 1:
cout<<"Enter the number of vertices in the graph: ";
cin>>num_of_vertices;
cout<<endl<<"Enter the number of edges in the graph: ";
cin>>num_of_edges;
cout<<endl<<endl;
cin.get();
graph_obj.initialize_graph(num_of_vertices, num_of_edges);
break;
case 2:
cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
cout<<"Enter the edge to insert into the graph: ";
cin>>vertex1>>vertex2;
cout<<endl<<endl;
graph_obj.insert_edge(vertex1, vertex2);
break;
case 3:
cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
cout<<"Enter the edge to delete from the graph: ";
cin>>vertex1>>vertex2;
cout<<endl<<endl;
graph_obj.delete_edge(vertex1, vertex2);
break;
case 4:
graph_obj.list_all_edges(num_of_vertices);
break;
case 5:
cout<<"Enter the vertex to list all of the neighbors for: ";
cin>>vertex1;
cout<<endl<<endl;
graph_obj.list_all_neighbors(vertex1, num_of_vertices);
break;
case 6:
graph_obj.no_incoming_edges(num_of_vertices);
} //end switch
} //end while
system("PAUSE");
return 0;
}
class graph //class for the graph data structure
{
public:
void initialize_graph(int num_of_vertices, int num_of_edges); //creates a new directed graph
void insert_edge(int vertex1, int vertex2); // inserts a directed edge (V1 - > V2) into the graph
void delete_edge(int vertex1, int vertex2); // deletes an edge (V1 -> V2) from the graph
void list_all_edges(int num_of_vertices); // lists all of the edges in the graph
void list_all_neighbors(int vertex1, int num_of_vertices); // lists all of the neighbors for a particular vertex
void no_incoming_edges(int num_of_vertices); // lists all of the vertices with no incoming edges
private:
graph *prev; //pointer to the previous node in the linked list (used in the adjacency list implementation only)
graph *next; //pointer to the next node in the linked list (used in the adjacency list implementation only)
graph *head; //pointer to the head of a linked list (used in the adjacency list implementation only)
int edge; // the id of the vertex that there is an edge to (used in both implementations)
};
Upvotes: 0
Views: 1604
Reputation: 356
You never actually allocated new memory from the heap for the pointer at:
graph *graph_obj;
It's pointer value is otherwise undefined, which is why it's crashing.
Initialize it with:
graph *graph_obj = new graph(); // Depending on your constructor.
Edit: Also if you're going to be re-using this pointer and pushing it back onto a container, you'll need to create a new pointer per element in container (Don't forget to delete it later!).
Upvotes: 3
Reputation: 62053
A bit of a problem here:
graph *graph_obj;
graph_obj -> head = NULL;
You're creating an uninitialized pointer and then immediately dereferincing it. You'll need to create an object for that pointer to point to.
Upvotes: 0