努力打卡 每天学习 不浪费每一天 Day78

无向图的基本操作

#include<iostream>
using namespace std;

#define MAXSIZE 100

bool visited[MAXSIZE] = { false };

class Graph {
private:
	char vertex[MAXSIZE];
	int edge[MAXSIZE][MAXSIZE]={0};
	int VertexNum, EdgeNum;

public:
	Graph(char a[], int n, int e);
	~Graph();
	void DFtraverse(int v);
	void BFtraverse(int v);
	void Print();
};
//构造函数
Graph::Graph(char a[], int n, int e) {
	int i, j;//顶点下标
	int k;//边数
	VertexNum = n;
	EdgeNum = e;
	for (int i = 0; i < VertexNum; i++) {
		vertex[i] = a[i];
	}
	for (int i = 0; i < VertexNum; i++) {
		for (int k = 0; k < VertexNum; k++) {
			edge[i][k] = 0;
		}
	}
	for (int k = 0; k < EdgeNum; k++) {
		cin >> i >> j;
		edge[i][j] = 1;
		edge[j][i] = 1;

	}
}

Graph::~Graph() {

}

//深度优先遍历
void Graph::DFtraverse(int v) {
	cout << vertex[v] << " ";
	visited[v] = true;
	for (int i = 0; i < VertexNum; i++) {
		if (edge[v][i] == 1 && visited[i] != true) {
			DFtraverse(i);
		}
	}

}
//广度优先遍历 (队列)
void Graph::BFtraverse(int v) {
	int w, j;
	int Q[MAXSIZE];
	//初始顶点都没有被访问
	for (int i = 0; i < VertexNum; i++) {
		visited[i] = false;
	}
	int front = -1, rear = -1;
	cout << vertex[v] << " ";
	visited[v] = true;
	Q[++rear] = v;
	while (front!=rear)
	{
		w = Q[++front];
		for (int j = 0; j < VertexNum; j++) {
			if (edge[w][j] == 1 && visited[j] != true) {
				cout << vertex[j] << " ";
				visited[j] = true;
				Q[++rear] = j;
			}
		}
	}

}

void Graph::Print() {
	
	for (int i = 0; i < VertexNum; i++) 
	{
		for (int j = 0; j < VertexNum; j++) 
		{
			cout << edge[i][j] << " ";
		}
		cout << endl;
	}
}


int main(){
	
	char ch[] = { 'A','B','C','D','E' };
	Graph MG(ch,5,6);//在构造函数中传入初始默认参数{5个顶点6个边}
	cout << endl << "深度优先遍历是:" << endl;
	MG.DFtraverse(0);//从顶点0出发
	cout << endl << "广度优先遍历是:" << endl;
	MG.BFtraverse(0);
	cout << endl;
	MG.Print();
	return 0;
}

事件绑定

 

<body>
<input type="button" value="点我" onclick="on()">
<input type="button" value="再点我" id="btn">

<script>
  function on(){
    alert("success");
  }
  document.getElementById("btn").onclick=function (){
    alert("xixi");
  }
</script>
</body>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值