-
Notifications
You must be signed in to change notification settings - Fork 42
图 #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
图 #139
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来这种风格的代码可能是源自你这儿啊? #144 你俩是好朋友?