main.cpp
- #include”Graph.h”
- int main(){
- GraphAdjList G;
- int sp,n;
- while(true){
- menu();
- printf(”请输入你的操作: ”);
- scanf(”%d”,&sp);
- if(sp==6)break;
- switch(sp){
- case 1:
- GreateALGraphF(&G);
- DispGraphAdjList(&G);
- break;
- case 2:
- GreateALGraph(&G);
- DispGraphAdjList(&G);
- break;
- case 3:
- DispGraphdu(&G);
- break;
- case 4:
- DFSTraverse(G);
- break;
- case 5:
- BFSTraverse(G);
- break;
- }
- }
- system(”pause”);
- return 0;
- }
Graph.cpp
- #include “GRaph.h”
- void menu(){
- printf(”\t\t\t 1,有向图邻接表的创建并输出!\n”);
- printf(”\t\t\t 2,无向图邻接表的创建并输出!\n”);
- printf(”\t\t\t 3,有向图邻接表的个顶点的度!\n”);
- printf(”\t\t\t 4,无向图邻接表的深度优先遍历!\n”);
- printf(”\t\t\t 5,无向图邻接表的广度优先遍历!\n”);
- printf(”\t\t\t 6,退出\n”);
- }
- void GreateALGraph(GraphAdjList *G){
- int i,j,k;
- EdgeNode *e;
- printf(”输入定点数和边数:\n”);
- scanf(”%d,%d”,&G->numVertexes,&G->numEdges);
- for(i = 0;i<G->numVertexes;i++){
- printf(”请输入顶点信息\n”);
- scanf(”%s”,&G->adjList[i].data);
- G->adjList[i].firstedge=NULL;
- }
- for(k = 0;k < G->numEdges;k++){
- printf(”输入边(vi,vj)上的顶点序号:\n”);
- scanf(”%d,%d”,&i,&j);
- e= (EdgeNode*)malloc(sizeof(EdgeNode));
- e->adjvex=j;
- e->next=G->adjList[i].firstedge;
- G->adjList[i].firstedge=e;
- e=(EdgeNode*)malloc(sizeof(EdgeNode));
- e->adjvex=i;
- e->next=G->adjList[j].firstedge;
- G->adjList[j].firstedge=e;
- }
- }
- void GreateALGraphF(GraphAdjList *G){
- int i,j,k;
- EdgeNode *e;
- printf(”输入定点数和边数:\n”);
- scanf(”%d,%d”,&G->numVertexes,&G->numEdges);
- for(i = 0;i<G->numVertexes;i++){
- printf(”请输入顶点信息\n”);
- scanf(”%s”,&G->adjList[i].data);
- G->adjList[i].firstedge=NULL;
- }
- for(k = 0;k < G->numEdges;k++){
- printf(”输入边(vi,vj)上的顶点序号:\n”);
- scanf(”%d,%d”,&i,&j);
- e= (EdgeNode*)malloc(sizeof(EdgeNode));
- e->adjvex=j;
- e->next=G->adjList[i].firstedge;
- G->adjList[i].firstedge=e;
- }
- }
- void DispGraphAdjList(GraphAdjList *G)
- {
- int i;
- EdgeNode *p;
- printf(”\n”);
- printf(”图的邻接表表示如下\n”);
- printf(”%6s%6s%12s\n”,“编号”,“顶点”,“相邻边编号”);
- for(i=0;i< G->numVertexes;i++)
- {
- printf(”%4d%6c ”,i,G->adjList[i].data);
- for(p=G->adjList[i].firstedge;p!=NULL;p=p->next){
- printf(”%2d->”,p->adjvex);
- }
- printf(”%s”,“ NULL”);
- printf(”\n”);
- }
- }
- void DispGraphdu(GraphAdjList *G){
- int i,m,n;
- printf(”%6s%6s%6s\n”,“编号”,“入度”,“出度”);
- for(i = 0; i<G->numVertexes;i++){
- m= getIndu(G,i);
- n= getOutdu(G,i);
- printf(”%3d %3d %3d\n”,i,m,n);
- }
- }
- int getIndu(GraphAdjList *G,int m){
- int i,sum=0;
- EdgeNode *p;
- for(i=0;i< G->numVertexes;i++)
- {
- for(p=G->adjList[i].firstedge;p!=NULL;p=p->next){
- if(p->adjvex==m)
- sum++;
- }
- }
- return sum;
- }
- int getOutdu(GraphAdjList *G,int m){
- int i,sum=0;
- EdgeNode *p;
- for(p=G->adjList[m].firstedge;p!=NULL;p=p->next){
- sum++;
- }
- return sum;
- }
- Boolean visited[MAXSIZE];
- void DFSTraverse(GraphAdjList GL){
- int i;
- for(i=0;i<GL.numVertexes;i++)
- visited[i]=FALSE;
- for(i=0;i<GL.numVertexes;i++)
- if(!visited[i])
- DFS(GL,i);
- printf(”\n”);
- }
- void DFS(GraphAdjList GL, int i){
- EdgeNode *p;
- visited[i] = TRUE;
- printf(”%c ”,GL.adjList[i].data);
- p=GL.adjList[i].firstedge;
- while(p){
- if(!visited[p->adjvex])
- DFS(GL,p->adjvex);
- p=p->next;
- }
- }
- void BFSTraverse(GraphAdjList GL){
- int i;
- EdgeNode *p;
- LinkQueue Q;
- for(i =0;i<GL.numVertexes;i++){
- visited[i]=FALSE;
- }
- InitQueue(&Q);
- for(i =0;i<GL.numVertexes;i++){
- if(!visited[i]){
- visited[i] = TRUE;
- printf(”%c ”,GL.adjList[i].data);
- EnQueue(&Q,i);
- while(!IsEmptyQueue(Q)){
- DeQueue(&Q,&i);
- p = GL.adjList[i].firstedge;
- while(p){
- if(!visited[p->adjvex]){
- visited[p->adjvex]=TRUE;
- printf(”%c ”,GL.adjList[p->adjvex].data);
- EnQueue(&Q,p->adjvex);
- }
- p= p->next;
- }
- }
- }
- }
- printf(”\n”);
- }
- Status IsEmptyQueue(LinkQueue Q){
- if(Q.front==Q.rear){
- return OK;
- }else{
- return ERROR;
- }
- }
- Status InitQueue(LinkQueue *Q){
- Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
- if(!Q->front)
- exit(OVERFLOW);
- Q->front->next=NULL;
- return OK;
- }
- Status EnQueue(LinkQueue *Q,QElemType e ){
- QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
- if(!s)exit (OVERFLOW);
- s->data=e;
- s->next=NULL;
- Q->rear->next=s;
- Q->rear=s;
- return OK;
- }
- Status DeQueue(LinkQueue *Q,QElemType *e){
- QueuePtr p;
- if(Q->front==Q->rear){
- return ERROR;
- }
- p=Q->front->next;
- *e=p->data;
- Q->front->next=p->next;
- if(Q->rear==p)
- Q->rear=Q->front;
- free(p);
- return OK;
- }
DS.h
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<io.h>
- #include<math.h>
- #define OK 1
- #define ERROR 0
- #define TRUE 1
- #define FALSE 0
- #define MAXVEX 100
- #define INFINITY 65535
- #define MAXSIZE 9
- #define MAXEDGE 15
- typedef int Status;
- typedef int Boolean;
- typedef char VertexType;
- typedef int EdgeType;
- typedef int QElemType;
Graph.h
- #include “DS.h”
- typedef struct EdgeNode{
- int adjvex;
- EdgeType weight;
- struct EdgeNode *next;
- }EdgeNode;
- typedef struct VertexNode{
- VertexType data;
- EdgeNode *firstedge;
- }VertexNode, AdjList[MAXVEX];
- typedef struct {
- AdjList adjList;
- int numVertexes, numEdges;
- }GraphAdjList;
- typedef struct QNode{
- QElemType data;
- struct QNode *next;
- }QNode,*QueuePtr;
- typedef struct {
- QueuePtr front ,rear;
- }LinkQueue;
- void menu();
- void GreateALGraph(GraphAdjList *G);
- void GreateALGraphF(GraphAdjList *G);
- void DispGraphAdjList(GraphAdjList *G) ;
- void DispGraphdu(GraphAdjList *G);
- int getIndu(GraphAdjList *G,int m);
- int getOutdu(GraphAdjList *G,int m);
- void DFSTraverse(GraphAdjList GL);
- void DFS(GraphAdjList GL, int i);
- void BFSTraverse(GraphAdjList GL);
- Status InitQueue(LinkQueue *Q);
- Status EnQueue(LinkQueue *Q,QElemType e );
- Status DeQueue(LinkQueue *Q,QElemType *e);
- Status IsEmptyQueue(LinkQueue Q);