使用邻接表建立有向图与无向图

使用邻接表创建无向图

#include
using namespace std;
typedef struct biao{
	int bianhao;
	int weight;
	struct biao* next;
}biao; 
typedef struct tou{
	char a;
	biao* next;
}tou;
typedef struct Graph{
	
	tou b[20];
	int vex,edge;
}Graph;
int Locate(Graph &p,char a);
void Creat(Graph &p);
void dayin(Graph &p);
int Locate(Graph &p,char a){
	int b=p.vex;
	int c;
	for(c=0;cnext=NULL;
		m->bianhao=v1;
		if(p.b[u1].next==NULL){
			p.b[u1].next=m;
		}
		else{
			biao* n=p.b[u1].next;
			while(n->next!=NULL){
				n=n->next;
			}
			n->next=m;
		}//------------------------------------------
		biao* m2=(biao*)malloc(sizeof(biao));
		m2->next=NULL;
		m2->bianhao=u1;
		if(p.b[v1].next==NULL){
			p.b[v1].next=m2;
		}
		else{
			biao* n2=p.b[v1].next;
			while(n2->next!=NULL){
				n2=n2->next;
			}
			n2->next=m2;
		}
		//-------------------------------
	}
}
void dayin(Graph &p){
	int a=p.vex;
	int b;
	for(b=0;b",p.b[m->bianhao].a);
			m=m->next;
		}
		printf("\n");
	}
}
void xieru(Graph &p){
	FILE* fp;
	fp=fopen("answer.txt","w");
	int a=p.vex;
	int b;
	for(b=0;b",p.b[m->bianhao].a);//此处打印的是编号,可以改成节点p.b[m->bianhao].a 
			m=m->next;
		}
		fprintf(fp,"\n");
	}
}
int main(){
	Graph p;
	Creat(p);
	dayin(p);
	xieru(p);
	return 0;
}

使用邻接表创建有向图

#include
using namespace std;
typedef struct biao{
	int bianhao;
	int weight;
	struct biao* next;
}biao; 
typedef struct tou{
	char a;
	biao* next;
}tou;
typedef struct Graph{
	
	tou b[20];
	int vex,edge;
}Graph;
int Locate(Graph &p,char a);
void Creat(Graph &p);
void dayin(Graph &p);
int Locate(Graph &p,char a){
	int b=p.vex;
	int c;
	for(c=0;cnext=NULL;
		m->bianhao=v1;
		if(p.b[u1].next==NULL){
			p.b[u1].next=m;
		}
		else{
			biao* n=p.b[u1].next;
			while(n->next!=NULL){
				n=n->next;
			}
			n->next=m;
		}
	}
}
void dayin(Graph &p){
	int a=p.vex;
	int b;
	for(b=0;b",p.b[m->bianhao].a);
			m=m->next;
		}
		printf("\n");
	}
}
void xieru(Graph &p){
	FILE* fp;
	fp=fopen("answer.txt","w");
	int a=p.vex;
	int b;
	for(b=0;b",p.b[m->bianhao].a);//此处打印的是编号,可以改成节点p.b[m->bianhao].a 
			m=m->next;
		}
		fprintf(fp,"\n");
	}
}
int main(){
	Graph p;
	Creat(p);
	dayin(p);
	xieru(p);
	return 0;
}

你可能感兴趣的:(考试,算法)