Skip to content
Merged

#139

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2017-1/ywy/图/2017-05-17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
284 changes: 284 additions & 0 deletions 2017-1/ywy/图/TU.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
#include <stdio.h>
#include <malloc.h>
#define INFINITY -1
#define MXAVERTEX_NUM 10//��󶥵����
#define VRType int
#define VertexType int
#define InfoType int
#define QElemType int
typedef enum
{
OK,
ERROR,
OVERFLOW
}Status;
typedef enum
{
true,
false
}bool;
//-------ͼ���ڽӾ���洢-----------//
typedef struct Arcell
{
VRType adj;//�����ϵ��1��0
InfoType *info;//�������Ϣ��ָ��
}Arcell, AdjMatrix[MXAVERTEX_NUM][MXAVERTEX_NUM];
typedef struct
{
VertexType vexs[MXAVERTEX_NUM]; //��������
AdjMatrix arcs; //�ڽӾ���
int vexnum, arcnum; //ͼ�ĵ�ǰ����ͻ���
}MGraph;
//--------����---------//
typedef struct QNode
{
QElemType data;
struct QNode *next;
struct QNode *pre;
}QNode, *QueuePtr;
typedef struct LinkQueue
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
/*=========���еĻ�������=========*/
Status InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q->front)
{
return ERROR;
}
Q->front->next = Q->rear->next = NULL;
return OK;
}

Status EnQueue(LinkQueue*Q, QElemType e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
{
return ERROR;
}
else
{
p->data = e;
p->next = NULL;
p->pre = Q->front;
Q->rear->next = p;
Q->rear = p;
}
}

Status DeQueue(LinkQueue*Q, QElemType*e)
{
if (Q->front == Q->rear)
{
return ERROR;
}
Q->front = Q->front->next;
*e = Q->front->data;
return OK;
}

bool QueueEmpty(LinkQueue*Q)
{
if (Q->front == Q->rear)
{
return true;
}
else
{
return false;
}
}

Status DestroyQueue(LinkQueue*Q)
{
while (Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}
/*=========ͼ�Ļ�������=========*/

Status LocateVex(MGraph *G, int v1, int v2)
{
int i;
int j;
int m = 0;
int n = 0;
for (i = 0; i <= G->vexnum; i++)
{
if (G->vexs[i] == v1)
{
m = i;
break;
}
}
for (j = 0; j <= G->vexnum; j++)
{
if (G->vexs[j] == v2)
{
n = j;
break;
}
}
G->arcs[i][j].adj = 1;//����֮�������ߣ���ֵΪ1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来这种风格的代码可能是源自你这儿啊? #144 你俩是好朋友?

G->arcs[j][i] = G->arcs[i][j];
return OK;
}

//����ͼ
Status CreateUDN(MGraph *G)
{
int i;
int j;
//��������ֱ�Ӹ�ֵ��
G->vexnum = 9;
G->arcnum = 12;
for (i = 1; i <= G->vexnum; i++)
{
G->vexs[i] = i;//������������;
}
for (i = 0; i <= G->vexnum; i++)//��ʼ���ڽӾ���;
{
for (j = 0; j <= G->vexnum; j++)
{
G->arcs[i][j].adj = INFINITY;
G->arcs[i][j].info = NULL;
}
}
//�����ڽӾ���;
LocateVex(G, 1, 2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考这里我的代码审查意见,一模一样的问题 #144

LocateVex(G, 1, 3);
LocateVex(G, 1, 4);
LocateVex(G, 1, 7);
LocateVex(G, 2, 3);
LocateVex(G, 4, 5);
LocateVex(G, 4, 6);
LocateVex(G, 5, 6);
LocateVex(G, 6, 8);
LocateVex(G, 7, 8);
LocateVex(G, 7, 9);
LocateVex(G, 8, 9);

return OK;
}

//�ҳ���һ���ڽӵ�
int FirstAdjVex(MGraph *G, int u)
{
int i;
for (i = 1; i <= G->vexnum; i++)
{
if (G->arcs[u][i].adj == 1)
{
return i;
}
}
return -1;
}

//�ҳ���һ���ڽӵ�
int NextAdjvex(MGraph *G, int u, int w)
{
int i;
for (i = w + 1; i <= G->vexnum; i++)
{
if (G->arcs[u][i].adj == 1)
{
return i;
}
}
return -1;
}
//������ȱ���ͼ,������a,b������·��;
Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b)
{

int v;
int u = 0;
int w = 0;
int m = 0;
int n = 0;
bool visited[MXAVERTEX_NUM];
for (v = 1; v <= G->vexnum; v++)
{
visited[v] = false; //������飬���ͼ���ѷ��ʵĵ�
}

EnQueue(Q, a); //a�������;
while (QueueEmpty(Q) != true)
{
DeQueue(Q, &u);
for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjvex(G, u, w))
{
if (visited[w] == false)//�ж�w�Ƿ��Ѿ����ʹ�
{
visited[w] = true;
EnQueue(Q, w);
}
if (w == b)
{
break;
}
}
if (w == b)
{
break;
}
}
return OK;
}

Status print(LinkQueue *Q, int a)
{
if (Q->rear->data == a)
{
printf("%d->%d\n", a, a);
return OK;
}

int i = 0;
int j;
int num[MXAVERTEX_NUM] = { 0 };
while (Q->rear->data != a)//�����������
{
num[i] = Q->rear->data;
Q->rear = Q->rear->pre;
i++;
}
printf("%d", a);
for (j = i - 1; j >= 0; j--)
{
printf("->%d", num[j]);
}


printf("\n");
return OK;
}

int main()
{
int i, j;
MGraph Graph;
CreateUDN(&Graph);
for (i = 1; i <= Graph.vexnum; i++)
{
for (j = 1; j <= Graph.vexnum; j++)
{
LinkQueue Q;
InitQueue(&Q);
printf("%d<->%d: ", i, j);
BFSTraverse(&Graph, &Q, i, j);
print(&Q, i);
DestroyQueue(&Q);

}
}
}