数据结构实验3--邻接表的相关操作(实验内容3~4)

运行结果:

数据结构实验3--邻接表的相关操作(实验内容3~4)

源文件:

#include "邻接表宽度优先遍历的queue.h"
//邻接表的初始化
Status Init(LGraph* lg, int nSize)
{
	int i;
	lg->numofDot = nSize;
	lg->numofEdge = 0;
	lg->a = (ENode**)malloc(nSize * sizeof(ENode*));
	if (!lg->a)
	{
		return ERROR;

	}
	else
	{
		for (i = 0; i < lg->numofDot; i++)
			lg->a[i] = NULL;
		return OK;
	}
}

//邻接表的撤销
void Destroy(LGraph* lg)
{
	int i;
	ENode* p, * q;
	for (i = 0; i < lg->numofDot; i++)
	{
		p = lg->a[i];
		q = p;
		while (p)
		{
			p = p->nextArc;
			free(q);
			q = p;
		}
	}
	free(lg->a);
}

//边的搜索
Status Exist(LGraph* lg, int u, int v)
{
	ENode* p;
	if (u<0 || v<0 || u>lg->numofDot - 1 || v>lg->numofDot - 1 || u == v)
	{
		return ERROR;

	}
	p = lg->a[u];
	while (p && p->adjVex != v)
	{
		p = p->nextArc;
	}
	if (!p)
	{
		return ERROR;
	}
	else
	{
		return OK;
	}
}

//边的插入
Status Insert(LGraph* lg, int u, int v, ElemType w)
{
	ENode* p;
	if (u<0 || v<0 || u>lg->numofDot - 1 || v>lg->numofDot - 1 || u == v)
		return ERROR;
	if (Exist(lg, u, v))
	{
		return Duplicate;
	}
	p = (ENode*)malloc(sizeof(ENode));
	p->adjVex = v;  
	p->w = w;
	p->nextArc = NULL;
	p->nextArc = lg->a[u];  //将新的边节点插入单链表的最前面
	lg->a[u] = p;
	lg->numofEdge++;
	return OK;
}

//边的删除
Status Remove(LGraph* lg, int u, int v)
{
	ENode* p, * q;
	if (u<0 || v<0 || u>lg->numofDot - 1 || v>lg->numofDot - 1 || u == v)
		return ERROR;
	p = lg->a[u], q = NULL;
	while (p && p->adjVex != v)
	{
		q = p;
		p = p->nextArc;
	}
	if (!p)
		return NotPresent;
	if (q)
		q->nextArc = p->nextArc;
	free(p);
	lg->numofEdge--;
	return OK;
}

//图的邻接表的深度优先遍历
void DFS(int v, int visited[], LGraph g)
{
	ENode* w;
	printf("%d ", v);
	visited[v] = 1;
	for (w = g.a[v]; w; w = w->nextArc)
	{
		if (!visited[w->adjVex])
			DFS(w->adjVex, visited, g);
	}
}

void DFSGraph(LGraph g)
{
	int i;
	int* visited = (int*)malloc(g.numofDot * sizeof(int));
	for (i = 0; i < g.numofDot; i++)
	{
		visited[i] = 0;
	}
	for (i = 0; i < g.numofDot; i++)
	{
		if (!visited[i])
		{
			DFS(i, visited, g);
		}
	}
	free(visited);
}

//图的邻接表的宽度优先遍历
void BFS(int v, int visited[], LGraph g)
{
	ENode* w;
	Queue q;
	queuecreate(&q, g.numofDot); //初始化队列
	visited[v] = 1;
	printf("%d ", v);
	EnQueue(&q, v);
	while (!queueIsEmpty(&q))
	{
		queueFront(&q, &v);
		DeQueue(&q);
		for (w = g.a[v]; w; w = w->nextArc)
		{
			if (!visited[w->adjVex])
			{
				visited[w->adjVex] = 1;
				printf("%d ", w->adjVex);
				EnQueue(&q, w->adjVex);
			}
		}
	}
}

void BFSGrapg(LGraph g)
{
	int i;
	int* visited = (int*)malloc(g.numofDot * sizeof(int));
	for (i = 0; i < g.numofDot; i++)
	{
		visited[i] = 0;
	}
	for (i = 0; i < g.numofDot; i++)
	{
		if (!visited[i])
		{
			BFS(i,visited, g);
		}
	}
	free(visited);
}
void main()
{
	LGraph lg;
	int nSize;
	int u, v,w;
	int judge = 0;
	printf("请输入图的总结点数:\t");
	scanf_s("%d", &nSize);
	Init(&lg, nSize);
	printf("\n输入-1则停止输入边数\n");
	for (int i = 0;judge!=-1; i++)
	{
		printf("\n请输入judge的值");
		scanf_s("%d", &judge);
		if (judge == -1)
			break;
		printf("请输入边的指向(u->v)和权重,格式:  u  v   w  :\n");
		scanf_s("%d %d %d", &u, &v, &w);
		int judge = Insert(&lg, u, v, w);
		switch (judge)
		{
		case 0:
			printf("\n输入的u或v有问题,插入失败,请重试");
			break;
		case 1:
			printf("\n插入成功!");
			break;
		case 5:
			printf("\n此边已存在,插入失败,请重试");
			break;
		default:
			break;
		}
	}
	printf("\n邻接表的深度优先遍历为:\n");
	DFSGraph(lg);
	printf("\n邻接表的宽度优先遍历为:\n");
	BFSGrapg(lg);
}

邻接表宽度优先遍历的queue.h

#pragma once
#include "邻接表的基础结构.h"

typedef struct Queue
{
	int front;
	int rear;
	int maxSize;
	int* element;
}Queue;
void queuecreate(Queue* Q, int mSize)
{
	Q->maxSize = mSize;
	Q->element = (int*)malloc(sizeof(int) * mSize);
	Q->front = Q->rear = 0;
}
void queueDestory(Queue* Q)
{
	Q->maxSize = 0;
	Q->front = Q->rear = -1;
	free(Q->element);
}
BOOL queueIsEmpty(Queue* Q)
{
	return Q->front == Q->rear;
}
BOOL queueIsFull(Queue* Q)
{
	return (Q->rear + 1) % Q->maxSize == Q->front;
}
BOOL queueFront(Queue* Q, int* x)
{
	if (queueIsEmpty(Q))
		return false;
	*x = Q->element[(Q->front + 1) % Q->maxSize];
	return true;
}
BOOL EnQueue(Queue* Q, int x)
{
	if (queueIsFull(Q))
		return false;
	Q->rear = (Q->rear + 1) % Q->maxSize;
	Q->element[Q->rear] = x;
	return true;
}
BOOL DeQueue(Queue* Q)
{
	if (queueIsEmpty(Q))
		return false;
	Q->front = (Q->front + 1) % Q->maxSize;
	return true;
}
void queueClear(Queue* Q)
{
	Q->front = Q->rear = 0;
}

邻接表的基础结构.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define ERROR 0 ;
#define OK 1;
#define Overflow 2;
#define Underflow 3;
#define NotPresent 4;
#define Duplicate 5;
typedef int Status;
typedef int ElemType;
typedef bool BOOL;
typedef struct eNode
{
	int adjVex; //与任意顶点u相邻接的顶点
	ElemType w;
	struct eNode* nextArc;
}ENode;
typedef struct lGraph
{
	int numofDot;   //图的当前顶点数
	int numofEdge;  //图的当前边数
	ENode** a; //指向一维数组指针
}LGraph;
上一篇:[LeetCode] 79. Word Search(单词查找)


下一篇:SCI论文如何写--斯坦福大学公开课-Writing in the Sciences