I'm doing some work on data structures, just to learn. Right now I have an admittedly very basic graph data structure.
I can create the graph with a predefined size, and then add edges to/from each vertex (un-directed). Here is the code so far:
graph.h
#pragma once
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
class Graph {
int vertices; // num of vertices in graph
std::vector<int> *adjList;
public:
Graph(int vertices);
void addEdge(int v, int w);
void printGraph();
};
Graph::Graph(int vertices) {
this->vertices = vertices;
adjList = new std::vector<int>[vertices];
}
void Graph::addEdge(int v, int w) {
adjList[v].push_back(w);
}
void Graph::printGraph() {
for (int i = 0; i < adjList->size(); ++i) {
}
}
graph.cpp
#include "stdafx.h"
#include "graph.h"
int main()
{
Graph graph(4);
graph.addEdge(0, 1); //counter starts at 0
graph.addEdge(0, 2);
graph.addEdge(2, 1);
graph.addEdge(2, 3);
return 0;
}
This works fairly well, however I would also like to add nodes after the graph object has already been created. I really can't figure out how to do this.
Any sort of guidance towards this (as well as general improvements to the code) will be greatly appreciated.