2020-11-26

数据结构实验

本实验为用邻接矩阵实现图的深度优先搜索和广度优先搜索
头文件head1.h

#pragma once
#define INF 32767
#define MAXV 100
typedef struct ANode{
	int adjvex;
	struct ANode* nextarc;
	int weight;
}ArcNode;
typedef struct Vnode {
	char info;
	ArcNode* firstarc;
}VNode;
typedef struct {
	VNode adjlist[MAXV];
	int n, e;
}AdjGraph;
void DFS(AdjGraph* G, int v);
void BFS(AdjGraph* G, int v);

头文件head2.h`

#pragma once
typedef int Elemtype;
#define MaxSize 100
typedef struct
{
    Elemtype data[MaxSize];
    int front, rear;
}SqQueue;
bool deQueue(SqQueue*& q, Elemtype &e);
bool enQueue(SqQueue* q, Elemtype e);
bool QueueEmpty(SqQueue* q);
void DestroyQueue(SqQueue*& q);
void InitQueue(SqQueue*& q);

源文件function.cpp
实现DFS和BFS

#include <cstdio>
#include "head.h"
#include "head2.h"
int visited[MAXV] = { 0 };
void DFS(AdjGraph* G, int v)       //深度优先搜索
{

	ArcNode* p;
	visited[v] = 1;
	printf("%d ", v+1);
	p = G->adjlist[v].firstarc;
	while (p != NULL)
	{
		if (visited[p->adjvex] == 0)
			DFS(G, p->adjvex);
		p = p->nextarc;
	}
}

void BFS(AdjGraph* G, int v)      //广度优先搜索
{
	int w=1 , i;
	ArcNode* p;
	SqQueue* qu;
	InitQueue(qu);
	int visited1[MAXV];
	for (i = 0; i < G->n; i++)
		visited1[i] = 0;
	printf("%d ", v+1);
	visited1[v] = 1;
	enQueue(qu, v);
	while (!QueueEmpty(qu))
	{
		deQueue(qu, w);
		p = G->adjlist[w].firstarc;
		while (p != NULL)
		{
			if (visited1[p->adjvex] == 0)
			{
				printf("%d ", p->adjvex+1);
				visited1[p->adjvex] = 1;
				enQueue(qu, p->adjvex);
			}
			p = p->nextarc;
		}
	}
	printf("\n");
}

源文件main.cpp
实现DFS和BFS的调用

#include <stdio.h>
#include <cstdlib>
#include "head.h"
#include "head2.h"
//int visited[MAXV][MAXV] = { 0 };
void CreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e)     //由图的邻接链表创建图的邻接表
{
	int i, j;
	ArcNode* p;
	G = (AdjGraph*)malloc(sizeof(AdjGraph));
	for (i = 0; i < n; i++)
		G->adjlist[i].firstarc = NULL;
	for (i = 0; i < n; i++)
	{
		for (j = n - 1; j >= 0; j--)
			if (A[i][j] != 0 && A[i][j] != INF)
			{
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->weight = A[i][j];
				p->nextarc = G->adjlist[i].firstarc;
				G->adjlist[i].firstarc = p;
			}
	}
	G->n = n;
	G->e = e;
}
void DispAdj(AdjGraph* G)    //输出邻接表
{
	int i; ArcNode* p;
	for (i = 0; i < G->n; i++)
	{
		p = G->adjlist[i].firstarc;
		printf("%3d:", i);
		while (p != NULL)
		{
			printf("%3d[%d]->", p->adjvex, p->weight);
			p = p->nextarc;
		}
		printf("\n");
	}
}
int main()
{
	int s[MAXV][MAXV];
	for (int i = 0; i < 12; i++)
		for (int j = 0; j < 12; j++)
			scanf("%d",&s[i][j]);
	AdjGraph* p;
	CreateAdj(p,s,12,14);
	printf("建立图的邻接如下\n");	
	DispAdj(p);
	printf("进行图的深度优先搜索结果如下:\n");
	DFS(p,0);
	printf("\n进行图的广度优先搜索结果如下:\n");
	BFS(p,0);
	/*for(int i=0;i<6;i++)
	printf("\n%d", p->adjlist[5+i].firstarc->adjvex);*/
	return 0;
}

队列算法
queue.cpp

//环形队列算法
#include <stdlib.h>
#include "head2.h"
void InitQueue(SqQueue*& q)
{
    q = (SqQueue*)malloc(sizeof(SqQueue));
    q->front = q->rear = 0;
}
void DestroyQueue(SqQueue*& q)
{
    free(q);
}
bool QueueEmpty(SqQueue* q)
{
    return(q->front == q->rear);
}
bool enQueue(SqQueue* q, Elemtype e)
{
    if ((q->rear + 1) % MaxSize == q->front)
        return false;
    q->rear = (q->rear + 1) % MaxSize;
    q->data[q->rear] = e;
    return true;
}

bool deQueue(SqQueue*& q, Elemtype &e)
{
    if (q->front == q->rear)
        return false;
    q->front = (q->front + 1) % MaxSize;
    e = q->data[q->front];
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值