DFS算法实现

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define infinity 0x3f3f3f
#define N 5
#define E 6
#define target 2
typedef struct edges {
	int node;
	bool visit;
	struct edges* next;
	struct edges* snext;
}edge,*e;
void push(e stack, e node) {
	node->snext = stack->snext;
	stack->snext = node;
}
e pop(e &stack) {
	e node = stack->snext;
	stack = stack->snext;
	return node;
}
typedef struct graph {
	int n;
	e map;
}graph,*g;
int myrand() {
	return rand() % N;
}
void show(g myGraph) {
	for (int i = 0; i < myGraph->n; i++) {
		e temp = &(myGraph->map[i]);
		printf("\n%5d:", temp->node);
		while (temp && temp->next) {
			printf("%5d->", temp->next->node);
			temp = temp->next;
		}
		printf("NULL");
	}
	printf("\n");
}
void DFS(g graphy,int distance[N],bool visit[N]) {
	e node = &graphy->map[target];
	e stack = new edge;
	stack->snext = NULL;
	int length = 0;
	while (stack->snext || !visit[node->node]) {
		if (!visit[node->node]) {
			push(stack, node);
			visit[node->node] = true;
			printf("%5d", node->node);
		}

		//e stemp = stack;
		//while (stemp && stemp->snext) {
		//	printf("*%5d", stemp->snext->node);
		//	stemp = stemp->snext;
		//}
		//printf("\n");
		bool out = false;
		for (int i = 0; i < N&&!out; i++) {
			e temp = &graphy->map[i];
			while (temp && temp->next) {
				if ((graphy->map[i].node == node->node&&!visit[temp->next->node] || !visit[graphy->map[i].node]&&temp->next->node == node->node)) {
					if (temp->next->node == node->node) {
						node = &graphy->map[i];
					}
					else {
						node = temp->next;
					}
					out = true;
					break;
				}
				temp = temp->next;
			}
		}
		if (!out && (stack->snext)) {
			node = pop(stack);
		}
		else if(!out&&!(stack->snext))
			break;
	}
}
int main(void) {
	srand(time(NULL));
	g myGraph = new graph;
	myGraph->n = N;
	myGraph->map = new edge[myGraph->n];
	for (int i = 0; i < myGraph->n; i++) {
		myGraph->map[i].node = i;
		myGraph->map[i].next = NULL;
		myGraph->map[i].visit = false;
		myGraph->map[i].snext = NULL;
	}
	int Edges = E;
	while (Edges) {
		int src = myrand();
		int dst = myrand();
		if (src == dst)
			continue;
		e temp = &(myGraph->map[src]);
		while (temp&&temp->next) {
			temp = temp->next;
		}
		e mye = new edge;
		mye->node = dst;
		temp->next = mye;
		mye->next = NULL;

		temp = &(myGraph->map[dst]);
		while (temp && temp->next) {
			temp = temp->next;
		}
		mye = new edge;
		mye->node = src;
		temp->next = mye;
		mye->next = NULL;
		Edges--;
	}
	show(myGraph);
	/*for (int i = 0; i < myGraph->n; i++) {
		e temp = &(myGraph->map[i]);
		printf("%5d:", temp->node);
		while (temp && temp->next) {
			printf("%5d->", temp->next->node);
			temp = temp->next;
		}
		printf("NULL\n");
	}*/
	int distance[N];
	bool visit[N];
	for (int i = 0; i < N; i++) {
		distance[i] = infinity;
		visit[i] = false;
	}
	DFS(myGraph, distance, visit);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值