- 论坛徽章:
- 0
|
深度遍历没有遍历全,而广度遍历却"跳着"遍历。- #include <stdio.h>;
- #define vnum 20
- typedef struct arcnode
- {
- int adjvex;
- struct arcnode *nextarc;
- } ArcNodeTp;
- typedef struct vexnode
- {
- int vertex;
- ArcNodeTp *firstarc;
- } AdjList[vnum];
- typedef struct linkgraph
- {
- AdjList adjs;
- int vexnum, arcnum;
- } LGraphTp;
- typedef struct linked_queue
- {
- int data;
- struct linked_queue *next;
- } LqueueTp;
- typedef struct queueptr
- {
- LqueueTp *front, *rear;
- } QueptrTp;
- LGraphTp* Create_LNvGraph (LGraphTp *ga);
- int main (void)
- {
- LGraphTp *ga;
- int input, choose;
- ga = (LGraphTp *) malloc (sizeof(LGraphTp));
- ga = Create_LNvGraph(ga);
- Print_LinkList(ga);
- printf("\n1:深度优先搜索 2:广度优先搜索 3,返回:");
- scanf("%d", &choose);
- switch (choose)
- {
- case 1:
- printf("\n%s\n%s", "###深度优先搜索###", "请问要搜索的结点号:");
- scanf("%d", &input);
- DFS(ga, input);
- break;
- case 2:
- printf("\n%s\n%s", "###广度优先搜索###", "请问要搜索的结点号:");
- scanf("%d", &input);
- BFS(ga, input);
- break;
- case 3:
- break;
- default:
- printf("\n选择错误!\n");
- }
- printf("\n\n");
- }
- LGraphTp* Create_LNvGraph (LGraphTp *ga)
- {
- ArcNodeTp *p;
- int vex, arc, vexs, arcnum;
- int i, j, k;
- printf("\n无向图邻接表\n");
- printf("请输入顶点个数:");
- scanf("%d", &vex);
- printf("请输入边数个数:");
- scanf("%d", &arc);
- ga ->; vexnum = vex;
- ga ->; arcnum = arc;
- for (i = 0; i < ga ->; vexnum; i++)
- {
- ga ->; adjs[i].vertex = i + 1;
- ga ->; adjs[i].firstarc = NULL;
- }
- for (k = 0; k < arc; k++)
- {
- printf("请输入第%d条边两个顶点的编号:", k + 1);
- scanf("%d %d", &i, &j);
- p = (ArcNodeTp *) malloc (sizeof(ArcNodeTp));
- p ->; adjvex = j;
- p ->; nextarc = ga ->; adjs[i - 1].firstarc;
- ga ->; adjs[i - 1].firstarc = p;
- p = (ArcNodeTp *) malloc (sizeof(ArcNodeTp));
- p ->; adjvex = i;
- p ->; nextarc = ga ->; adjs[j - 1].firstarc;
- ga ->; adjs[j - 1].firstarc = p;
- }
- return (ga);
- }
- int Print_LinkList (LGraphTp *ga)
- {
- ArcNodeTp *p;
- int k;
- printf("\n目前输入的邻接表为:\n\n");
-
- for (k = 0; k < ga ->; vexnum; k++)
- {
- printf("V%d:%d |", k + 1, ga ->; adjs[k].vertex);
- p = ga ->; adjs[k].firstarc;
- while (p != NULL)
- {
- printf(" ->; %d", p ->; adjvex);
- p = p ->; nextarc;
- }
- printf("\n");
- }
- }
- int DFS (LGraphTp *ga, int v)
- {
- ArcNodeTp *p;
- int j;
- static int visited[vnum];
- for (j = 0; j < vnum; j++)
- visited[vnum] = 0;
- printf(" ->; %d ", v);
- visited[v] = 1;
- p = ga ->; adjs[v].firstarc;
- while (p != NULL)
- {
- if (! visited[p ->; adjvex])
- DFS(ga, p ->; adjvex);
- p = p ->; nextarc;
- }
- }
- int InitQueue (QueptrTp **lq)
- {
- LqueueTp *p;
- p = (LqueueTp *) malloc (sizeof(LqueueTp));
- (*lq) ->; front = p;
- (*lq) ->; rear = p;
- ((*lq) ->; front) ->; next = NULL;
- }
- int EnQueue (QueptrTp **lq, int x)
- {
- LqueueTp *p;
- p = (LqueueTp *) malloc (sizeof(LqueueTp));
- p ->; data = x;
- p ->; next = NULL;
- ((*lq) ->; rear) ->; next = p;
- (*lq) ->; rear = p;
- }
- int OutQueue (QueptrTp **lq, int *x)
- {
- LqueueTp *s;
- if ((*lq) ->; front == (*lq) ->; rear)
- {
- printf("队空!\n");
- return 0;
- }
- else
- {
- s = ((*lq) ->; front) ->; next;
- *x = s ->; data;
- ((*lq) ->; front) ->; next = s ->; next;
- if (s ->; next == NULL)
- (*lq) ->; rear = (*lq) ->; front;
- free(s);
- return 1;
- }
- }
- int EmptyQueue (QueptrTp **lq)
- {
- if (((*lq) ->; rear) == ((*lq) ->; front))
- return 1;
- else
- return 0;
- }
- int GetHead (QueptrTp **lq, int *x)
- {
- LqueueTp *p;
- if ((*lq) ->; rear == (*lq) ->; front)
- return 0;
- else
- {
- p = ((*lq) ->; front ) ->; next;
- *x = p ->; data;
- return 1;
- }
- }
- int BFS(LGraphTp *ga, int v)
- {
- QueptrTp *Q;
- static int visited[vnum];
- ArcNodeTp *p;
- InitQueue(&Q);
- printf("%d", v);
- visited[v] = 1;
- EnQueue(&Q, v);
- while (! EmptyQueue(&Q))
- {
- OutQueue(&Q, &v);
- p = ga ->; adjs[v].firstarc;
- while (p != NULL)
- {
- if (! visited[p ->; adjvex])
- {
- printf("%d", p ->; adjvex);
- visited[p ->; adjvex] = 1;
- EnQueue(&Q, p ->; adjvex);
- }
- p = p ->; nextarc;
- }
- }
- }
复制代码 |
|