算法导论图的表示

#include<iostream>
using namespace std;
#define N 10
struct Edge{
	Edge* next;
	int start ;
	int end ;
	int weight;
	Edge(int s,int e):start(s),end(e){
		weight = 1;
		next = NULL;
	}
};
struct Vext{
	Edge* head;
	Vext(){
		head = NULL;
	}
};

struct Graph{
	Vext* p;
	int n;
	Graph(int num):n(num){
		p = new Vext [n];
		cout<<sizeof(p)<<endl;//correspond to char* p;
	}
	~Graph(){
		delete [] p;
	}
	
	void addSingle(int s,int e){
	Edge *pe = new Edge(s,e);
	if(p[s].head==NULL||p[s].head->end>e){
		pe->next = p[s].head;
		p[s].head = pe;
	}else{
		Edge *tmp = p[s].head,*pre=tmp;
		while(tmp!=NULL&&tmp->end<e){
			pre = tmp;
			tmp = tmp->next;
		}
		if(tmp!=NULL&&tmp->end==e){
			delete tmp;
			return ;
		}
		pe->next = tmp;
		pre->next =pe;
	}
}

	void addDouble(int s,int e){
		addSingle(s,e);
		addSingle(e,s);
}

	void deleteSingle(int s,int e){
	Edge* tmp = p[s].head; 
	Edge* pre;
	while(tmp->end!=e&&tmp!=NULL){
			pre = tmp;
			tmp = tmp->next;
	}
	if(tmp == NULL||tmp->end>e)return ;
	if(tmp==p[s].head)
		p[s].head=tmp->next;	
	else{
		pre->next = tmp->next;
	}
}
	void deleteDouble(int s,int e){
	deleteSingle(s,e);
	deleteSingle(e,s);
}

	void outdegree(){
		int i;
		int ret[N+1]={0};
		for(i=0;i<n;++i){
			Edge* pe = p[i].head;
			while(pe!=NULL){
				ret[pe->start]++;
				pe = pe->next;
			}
		}
		for (i=0;i<n;++i)
		{
			cout<<"the outdegree of "<< i <<" is "<<ret[i]<<endl;;
		}
	}

	void indegree(){
		int i;
		int ret[N+1]={0};
		for(i=0;i<n;++i){
			Edge* pe = p[i].head;
			while(pe!=NULL){
				ret[pe->end]++;
				pe = pe->next;
			}
		}
		for (i=0;i<n;++i)
		{
			cout<<"the indegree of "<< i <<" is "<<ret[i]<<endl;;
		}
	}
//图的转置
		Graph* reserve(){
			tmp = new Graph(n);
			for(int i=0;i<n;++i){
				Edge *pe = p[i]->head;
				while(pe!=NULL){
					tmp->addSingle(pe->start,pe->end);
				}
			}
			return tmp;
		}

};

struct Graph_Matrix{
	int Matrix[N+1][N+1];
	int nume;
	int numv;
	Graph_Matrix(){
		cout<<"input the num of vext:"<<endl;
		cin>>numv;
		cout<<"input the num of edge:"<<endl;
		cin>>nume;
		for(int i=0;i<numv;++i)
			for(int j=0;j<numv;++j){
				Matrix[i][j]=0;
			}
	}
	void init();
	void show();
};

void Graph_Matrix::init(){
	cout<<"init the graph..."<<endl;
	for(int i=0;i<nume;++i){
		int start ,end;
		cout<<"int the start and the end point :"<<endl;
		cin>>start>>end;
		cout<<"input the weight of the edge :"<<endl;
		cin>>Matrix[start][end];
	}
}

void Graph_Matrix::show(){
	for(int i=0;i<numv;++i)
		for(int j=0;j<numv;++j){
			cout<<Matrix[i][j]<<" ";
			if(j==numv-1)
				cout<<endl;
		}
}

int main(){	
	Graph graph(5);
	graph.addSingle(0,4);
	graph.addSingle(1,0);
	graph.addSingle(1,2);
	graph.addSingle(1,3);
	graph.addSingle(2,4);	
	graph.outdegree();
	graph.indegree();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值