数据结构实验六 图结构及其应用 下载本文

}

}

scanf(\, &v1, &v2); getchar(); printf(\);

i = locateVertex(*umg, v1); j = locateVertex(*umg, v2);

// (*umg).arcs[i][j] = (*umg).arcs[j][i] = 1; //由于是无向图,因此一条边关(*umg).arcs[i][j] = 1; //有向图,边为单向

联两个顶点

//创建无向(有向)有权图 void createMGraph(MGraph *mg) {

//将图中相邻的顶点的邻接矩阵值设为边的权值

printf(\输入边的信息,输入边的两个顶点名称和权值v1 v2 w\\n\); for (v = 0; v < (*mg).arcnum; v++) {

printf(\输入第%d条边两个顶点和权值:\, v); scanf(\, &v1, &v2, &w); getchar();

i = locateVertex(*mg, v1); j = locateVertex(*mg, v2);

// (*mg).arcs[i][j] = (*mg).arcs[j][i] = w; //由于是无向图,因此一条边关联//初始化邻接矩阵

for (i = 0; i < (*mg).vexnum; i++)

for (j = 0; j < (*mg).vexnum; j++)

(*mg).arcs[i][j] = INFINITY;

int i, j, v, w; char v1, v2;

printf(\输入有权图的顶点数和边数\\n\);

scanf(\, &(*mg).vexnum, &(*mg).arcnum); for (v = 0; v < (*mg).vexnum; v++)

visited[v] = false; getchar();

printf(\输入顶点名称\\n\);

for (v = 0; v < (*mg).vexnum; v++) { }

printf(\输入第%d个顶点名称:\, v); scanf(\, &(*mg).vexs[v]); getchar();

两个顶点 }

//打印图的邻接矩阵 void print(MGraph G) { }

//深度优先遍历图

int FirstAdjVex(MGraph G, int v) { //获取与顶点v相邻的第一个顶点下标 }

int NextAdjVex(MGraph G, int v, int w) { //得到v的下一个未被访问的相邻顶点下标

int i;

for (i = w; i < G.vexnum; i++) { }

if (G.arcs[v][i] != 0 && G.arcs[v][i] != INFINITY && !visited[i])

return i;

int i;

for (i = 0; i < G.vexnum; i++) { }

return -1;

if (G.arcs[v][i] != 0 && G.arcs[v][i] != INFINITY && !visited[i])

return i;

int i, j;

printf(\图的邻接矩阵\\n\); for (i = 0; i < G.vexnum; i++) { }

printf(\);

for (j = 0; j < G.vexnum; j++) { }

printf(\);

if (G.arcs[i][j] != INFINITY)

printf(\, G.arcs[i][j]); printf(\∞ \); else

}

(*mg).arcs[i][j] = w; //有向图,边为单向

}

return -1;

void DFS(MGraph G, int v) { int w;

visited[v] = true;

printf(\, G.vexs[v]); //访问第v个顶点

for (w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w)) if (!visited[w])

DFS(G, w); }

void DFSTraverse(MGraph G) { printf(\深度优先遍历序列:\); int v;

for (v = 0; v < G.vexnum; v++) visited[v] = false; for (v = 0; v < G.vexnum; v++) if (!visited[v])

DFS(G, v);

printf(\); }

//广度优先遍历

//创建用于广度优先遍历的队列 typedef struct QNode { QElemType data; struct QNode *qnext; }QNode, *PQNode;

typedef struct Queue { PQNode front; PQNode rear; }Queue, *PQueue;

//初始化一个空队列

//对v的尚未访问的邻接顶点w递归调用DFS

PQueue initQueue() { }

//队尾入队

void enQueue(PQueue pqueue, QElemType data) { }

//判断队列是否为空

bool isEmpty(PQueue pqueue) { }

//队头出队

QElemType deQueue(PQueue pqueue) {

if (isEmpty(pqueue)) { }

printf(\队列为空\\n\); exit(-1);

if (pqueue->front == pqueue->rear)

return true; return false;

PQNode pqnode = (PQNode)malloc(sizeof(QNode)); if (pqnode == NULL) { }

pqnode->data = data; pqnode->qnext = NULL;

pqueue->rear->qnext = pqnode; pqueue->rear = pqnode;

printf(\队列结点申请失败!\\n\); exit(-1);

PQueue pqueue = (PQueue)malloc(sizeof(Queue)); PQNode pqnode = (PQNode)malloc(sizeof(QNode)); if (pqnode == NULL) { }

pqueue->front = pqueue->rear = pqnode; pqnode->qnext = NULL; return pqueue;

printf(\队列头空间申请失败!\\n\); exit(-1);