#ifndef GRAPHL_H
#define GRAPHl_H
#include <iostream>
#include "graph.h"
using namespace std;
// node data element
struct listUnit {
int vertex; // 顶点
int weight; // 边的权
};
// linked list node
template <class Elem>
class Link{
public:
Elem element;
Link *next;
Link(const Elem &elemval, Link *nextval = NULL) {
element = elemval;
next = nextval;
}
Link(Link *nextval = NULL) {
next = nextval;
}
};
// linked list
template<class Elem>
class LList {
public:
Link<Elem> *head;
LList() {
head = new Link<Elem>;
}
};
// graph
class Graphl : public Graph {
private:
LList<listUnit> *graList;
public:
Graphl(int numVert) : Graph(numVert) {
graList = new LList<listUnit>[numVertex];
}
// e 要用引用类型才会传递修改结果
bool FirstEdge(int oneVertex, Edge &e) {
e.from = oneVertex;
Link<listUnit> *temp = graList[oneVertex].head;
if (temp->next != NULL) {
e.to = temp->next->element.vertex;
e.weight = temp->next->element.weight;
return true;
}
else
return false;
}
bool NextEdge(Edge preEdge, Edge &e) {
e.from = preEdge.from;
Link<listUnit> *temp = graList[preEdge.from].head;
// 定位到preEdge
while (temp->next != NULL && temp->next->element.vertex <= preEdge.to)
temp = temp->next;
if (temp->next != NULL ) {
e.to = temp->next->element.vertex;
e.weight = temp->next->element.weight;
return true;
}
else
return false;
}
void setEdge(int from, int to, int weight) {
Link<listUnit> *temp = graList[from].head;
// 定位到(from, to)前一个结点
while (temp->next != NULL && temp->next->element.vertex < to)
temp = temp->next;
if (temp->next == NULL) {
/* listUnit e;
e.vertex = to;
e.weight = weight;
temp->next = new Link<listUnit>(e, NULL);
*/
temp->next = new Link<listUnit>;
temp->next->element.vertex = to;
temp->next->element.weight = weight;
numEdge++;
Indegree[to]++;
return;
}
if(temp->next->element.vertex == to) {
temp->next->element.weight = weight;
return;
}
if (temp->next->element.vertex > to) {
listUnit e;
e.vertex = to;
e.weight = weight;
temp->next = new Link<listUnit>(e, temp->next);
numEdge++;
Indegree[to]++;
return;
}
}
void delEdge(int from, int to) {
Link<listUnit> *temp = graList[from].head;
while (temp->next != NULL && temp->next->element.vertex < to)
temp = temp->next;
if (temp->next == NULL)
return;
if (temp->next->element.vertex > to)
return;
if (temp->next->element.vertex == to) {
Link<listUnit> *other = temp->next->next;
delete temp->next;
temp->next = other;
numEdge--;
Indegree[to]--;
return;
}
}
void print() {
Edge temp, temp1;
for (int i = 0; i < numVertex; i++) {
if (FirstEdge(i, temp)) {
temp.print();
while (NextEdge(temp, temp1)) {
temp1.print();
temp = temp1;
}
}
}
}
int sizeofGraph() {
return numVertex;
}
}; // class Graphl
#endif
#include <iostream>
#include "graphl.h"
using namespace std;
int
main() {
Graphl g(5);
for (int i = 0; i < g.sizeofGraph(); i++)
for (int n = 0; n < 5; n++)
g.setEdge(i, n, i);
g.print();
Edge first, next;
g.FirstEdge(0, first);
first.print();
g.NextEdge(first, next);
next.print();
if (g.isEdge(first))
cout<<"first is edge!"<<endl;
g.delEdge(0, 1);
g.print();
return 0;
}