Hello, I'm working on an assignment right now and I need some feedback. My project is to implement a graph that uses an adjacency list like an Edge list. I'm fairly new with these and I would apprecite if anyone can see my code if I'm on the right track. Thanks!

#include<iostream>
#include<string>
#include<list>
using namespace std;

#define D true
#define U false
#define W true
#define N false
#define MAX 100

class Edge 
{
public:
    Edge() 
    {
    }
    Edge(int from, int to, double weight = -1) 
    : from (from), to (to), weight (weight)
    {
    }
    Edge(const Edge & other) 
    {
        from = other.from;
        to = other.to;
        weight = other.weight;
    }
    Edge & operator=(const Edge & other) 
    {
        this->from = other.from;
        this->to = other.to;
        this->weight = other.weight;
        return *this;
    }
    int operator== (const Edge & other) const 
    {
        if (this->from != other.from) return 0;
        if (this->to != other.to) return 0;
        if (this->weight != other.weight) return 0;
        return 1;
    }
    int from;
    int to;
    bool directed;
    double weight;
};

class Graph 
{
public:
    Graph(int n, bool directed, bool weighted)
        : directed(directed), weighted(weighted)
    {
    }
    void addEdge(Edge e) 
    {
        list<Edge> adjList;
        adjList.push_back(e);   
    }
    bool isDirected() 
    {
        return directed;
    }
    bool isWeighted() 
    {
        return weighted;
    }
    int getNumVertices ();
    int getNumEdges ();
private:
    bool directed;
    bool weighted;
    Edge *adjList;
};

int main() {
    Graph a(8, U, N); 
    Edge a0(0, 1);
    Edge a1(1, 3);
    Edge a2(6, 3);
    Edge a3(4, 5);
    Edge a4(1, 2);
    Edge a5(3, 7);
    Edge a6(3, 4);
    a.addEdge(a0);
    a.addEdge(a1);
    a.addEdge(a2);
    a.addEdge(a3);
    a.addEdge(a4);
    a.addEdge(a5);
    a.addEdge(a6);
}

Looks like you're off to a good start. I believe operator==() is generally expected to return type bool, rather than int, so you could just do:

return this->from == other.from && this->to == other.to && this->weight == other.weight;

But I think it'll work as-is.

Your Edge class can be stripped down to this

class Edge 
{
public:
    Edge() 
    {
    }
    Edge(int from, int to, double weight = -1) 
    : from (from), to (to), weight (weight)
    {
    }
    int from;
    int to;
    bool directed;
    double weight;
};

Since the compiler generated functions will suffice.

In line 51,52, you make no use of the parameter 'n'

Also in line 60, and 64,make the function 'const-corrected' meaning that add a const after the function prototype. For example bool isDirected()const{return directed;} Notice the const.

This article has been dead for over six months. Start a new discussion instead.