图基本操作

/*
 *		Graph Basic Operation
 *			DFS(), DFS_R(), BFS(), CreateAdjacencyList(), Add().
 *			StackPush(), StackPop(), StackGetTop().
 *			QueuePush(), QueuePop(), QueueGetFront().
 *			Connected().
 *		Date: 2013/3/22
 *									NolanJian
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 2001
#define TRUE 1
#define FALSE 0

struct node;
struct stack;
struct queue;
typedef struct node Node;
typedef struct stack Stack;
typedef struct stack Queue;
typedef Node *NodePointer;
typedef Stack *StackPointer;
typedef Queue *QueuePointer;

NodePointer StackGetTop(StackPointer top);
void StackPush(StackPointer *top, NodePointer p);
void StackPop(StackPointer *top);
void DFS(int v);
void Test_DFS(void);
void DFS_R(int v);
void Test_DFS_R(void);
void CreateAdjacencyList(void);
void Add(NodePointer *p, int y);
void BFS(int v);
void Test_BFS(void);
void QueuePush(QueuePointer *Front, QueuePointer *Rear, NodePointer p);
NodePointer QueueGetFront(QueuePointer Front);
void QueuePop(QueuePointer *Front);
void Test_Connected(void);
void Connected(void);

struct node {
	int y;
	NodePointer Next;
};
struct stack {
	NodePointer p;
	StackPointer Next;
};
struct queue {
	NodePointer p;
	QueuePointer Next;
};

NodePointer path[SIZE];
short int visit[SIZE];
int N, M;

int main() {
	while(TRUE) {
		CreateAdjacencyList();
		Test_DFS_R();
		Test_DFS();
		Test_BFS();
		Test_Connected();
	}
}

void StackPush(StackPointer *top, NodePointer p) {
	StackPointer tmp = (StackPointer)malloc(sizeof(Stack));
	tmp->p = p;
	tmp->Next = NULL;
	if(*top == NULL) {
		*top = tmp;
		return ;
	}
	tmp->Next = *top;
	(*top) = tmp;
	return ;
}

NodePointer StackGetTop(StackPointer top) {
	return top->p;
}

void StackPop(StackPointer *top) {
	StackPointer p = *top;
	*top = (*top)->Next;
	free(p);
}

void DFS(int v) {
	StackPointer top = NULL;
	NodePointer tmp;
	StackPush(&top, path[v]);
	visit[v] = TRUE;
	printf("%4d", v);
	while(top != NULL) {
		tmp = StackGetTop(top);
		StackPop(&top);
		while(tmp) {
			if(visit[tmp->y] == FALSE) {
				visit[tmp->y] = TRUE;
				printf("%4d", tmp->y);
				StackPush(&top, tmp);
				tmp = path[tmp->y];
			}
			else
				tmp = tmp->Next;
		}
	}
	return ;
}

void BFS(int v) {
	NodePointer tmp, p, w;
	QueuePointer Front, Rear;
	int u;
	Front = Rear = NULL;
	printf("%4d", v);
	visit[v] = TRUE;
	p = (NodePointer)malloc(sizeof(Node));
	p->y = v;
	p->Next = path[v];
	QueuePush(&Front, &Rear, p);
	while(Front) {
		tmp = QueueGetFront(Front);
		QueuePop(&Front);
		while(tmp)
			if(visit[tmp->y] == FALSE) {
				printf("%4d", tmp->y);
				visit[tmp->y] = TRUE;
				QueuePush(&Front, &Rear, path[tmp->y]);
			}
			else
				tmp = tmp->Next;
	}
	return ;
}

void QueuePush(QueuePointer *Front, QueuePointer *Rear, NodePointer p) {
	QueuePointer tmp = (QueuePointer)malloc(sizeof(Queue));
	tmp->p = p;
	tmp->Next = NULL;
	if(*Front == NULL)
		*Front = tmp;
	else
		(*Rear)->Next = tmp;
	*Rear = tmp;
	return ;
}

NodePointer QueueGetFront(QueuePointer Front) {
	return Front->p;
}

void QueuePop(QueuePointer *Front) {
	QueuePointer tmp = *Front;
	*Front = (*Front)->Next;
	free(tmp);
}

void Test_BFS(void) {
	memset(visit, FALSE, sizeof(visit));
	printf("\n****************************************************************\n");
	printf("BFS: \n");
	BFS(1);
	printf("\n****************************************************************\n");
}

void Test_DFS(void) {
	memset(visit, FALSE, sizeof(visit));
	printf("\n****************************************************************\n");
	printf("DFS: \n");
	DFS(1);
	printf("\n****************************************************************\n");
}

void Test_DFS_R(void) {
	memset(visit, FALSE, sizeof(visit));
	printf("\n****************************************************************\n");
	printf("DFS_R: \n");
	DFS_R(1);
	printf("\n****************************************************************\n");
}

void DFS_R(int v) {
	NodePointer w;
	visit[v] = TRUE;
	printf("%4d", v);
	for(w = path[v]; w; w = w->Next)
		if(!visit[w->y])
			DFS_R(w->y);
}

void CreateAdjacencyList(void) {
	int i, x, y;
	printf("Input: ");
	scanf("%d%d", &N, &M);
	for(i = 0; i < SIZE; i++)
		free(path[i]);
	for(i = 0; i < N; i++) {
		scanf("%d%d", &x, &y);
		Add(&path[x], y);
	}
}

void Add(NodePointer *p, int y) {
	NodePointer tmp = (NodePointer)malloc(sizeof(Node));
	tmp->y = y;
	tmp->Next = NULL;
	if(*p == NULL) {
		(*p) = tmp;
		return ;
	}
	tmp->Next = *p;
	*p = tmp;
	return ;
}

void Connected(void) {
	int i;
	for(i = 1; i <= M; i++)
		if(visit[i] == 0) {
			DFS(i);
			printf("\n");
		}
}

void Test_Connected(void) {
	memset(visit, FALSE, sizeof(visit));
	printf("\n****************************************************************\n");
	printf("Connected: \n");
	Connected();
	printf("****************************************************************\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值