graphl.h

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值