GraphNode.h: #ifndef _GRAPH_NODE_H_ #define _GRAPH_NODE_H_ #include #include "GraphLink.h" using namespace std; class GraphNode { private: char element; GraphLink* first; int indegree; bool seen; public: GraphNode(char element); ~GraphNode(); void setElement(char element); char getElement() const; void setIndegree(int indeg); int getIndegree() const; void setSeen(bool seen); bool getSeen() const; void setFirst(GraphLink* first); GraphLink* getFirst() const; void addLink(GraphLink* link); void removeLink(int toIndex); bool hasFirst() const; }; GraphNode::GraphNode(char element) { this->setElement(element); this->setIndegree(0); } GraphNode::~GraphNode() {} void GraphNode::setElement(char element) { this->element = element; } char GraphNode::getElement() const { return this->element; } void GraphNode::setIndegree(int indeg) { this->indegree = indeg; } int GraphNode::getIndegree() const { return this->indegree; } void GraphNode::setSeen(bool seen) { this->seen = seen; } bool GraphNode::getSeen() const { return this->seen; } void GraphNode::setFirst(GraphLink* first) { this->first = first; } GraphLink* GraphNode::getFirst() const { return this->first; } void GraphNode::addLink(GraphLink* link) { if(this->hasFirst()) { GraphLink* trav = this->getFirst(); for(;trav->hasNext();trav = trav->getNext()); trav->setNext(link); } else { this->setFirst(link); } } void GraphNode::removeLink(int toIndex) { if(this->hasFirst()) { GraphLink* trav = this->getFirst(); GraphLink* before = NULL; for(;trav->getNodeIndex() != toIndex && trav->getNext() != NULL;before = trav, trav = trav->getNext()); if(this->getFirst() != trav && trav->getNodeIndex() == toIndex) {// link found now we can remove it if(before != NULL) before->setNext(trav->getNext()); trav->setNext(NULL); delete trav; } else if (this->getFirst() == trav && trav->getNodeIndex() == toIndex) { // link found in first this->setFirst(NULL); trav->setNext(NULL); delete trav; } } } bool GraphNode::hasFirst() const { return this->getFirst() != NULL; } #endif GraphLink.h: #ifndef _GRAPH_LINK_H_ #define _GRAPH_LINK_H_ #include using namespace std; class GraphLink { private: int nodeIndex; GraphLink* next; int cost; public: GraphLink(int nodeIndex, int cost); ~GraphLink(); void setNodeIndex(int nodeIndex); int getNodeIndex(); void setCost(int cost); int getCost(); void setNext(GraphLink* next); GraphLink* getNext(); bool hasNext(); }; GraphLink::GraphLink(int nodeIndex, int cost) { this->setNodeIndex(nodeIndex); this->setCost(cost); this->setNext(NULL); } GraphLink::~GraphLink() {} void GraphLink::setNodeIndex(int nodeIndex) { this->nodeIndex = nodeIndex; } int GraphLink::getNodeIndex() { return this->nodeIndex; } void GraphLink::setCost(int cost) { this->cost = cost; } int GraphLink::getCost() { return this->cost; } void GraphLink::setNext(GraphLink* next) { this->next = next; } GraphLink* GraphLink::getNext() { return this->next; } bool GraphLink::hasNext() { return this->getNext() != NULL; } #endif Graph.h: #ifndef _GRAPH_H_ #define _GRAPH_H_ #include #include //#include "GraphLink.h" #include "GraphNode.h" using namespace std; class Graph { private: vector nodes; public: Graph(); ~Graph(); void addNode(char element); GraphNode* getNode(int i); void makeLink(int from, int to, int cost); void removeLink(int from, int to); int hashElem(char element); }; #endif Graph.cpp: #include "Graph.h" using namespace std; Graph::Graph() { } Graph::~Graph() { for(int i = 0; i < (int)nodes.size(); i++) delete this->nodes[i]; this->nodes.clear(); } int Graph::hashElem(char element) { return (int)element - 64; } void Graph::addNode(char element) { int index = this->hashElem(element); this->nodes[index] = new GraphNode(element); } GraphNode* Graph::getNode(int i) { return this->nodes[i]; } void Graph::makeLink(int from, int to, int cost) { this->getNode(from)->addLink(new GraphLink(to,cost)); GraphNode* toNode = this->getNode(to); toNode->setIndegree(toNode->getIndegree() + 1); } void Graph::removeLink(int from, int to) { this->getNode(from)->removeLink(to); GraphNode* toNode = this->getNode(to); toNode->setIndegree(toNode->getIndegree() - 1); }