![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7151b81746f7e5621198c0b4100a9375.png)
#pragma once
#include<stdio.h>
#include<stdlib.h>
#define StackSize 100
typedef int DataType;
typedef struct{
DataType stack[StackSize];
int top;
}SeqStack;
void InitStack(SeqStack *S){
S->top=0;
}
int StackEmpty(SeqStack S){
if(S.top==0)
return 1;
else
return 0;
}
int GetTop(SeqStack S,DataType *e){
if(S.top<=0){
printf("栈已经空!\n");
return 0;
}else{
*e=S.stack[S.top-1];
return 1;
}
}
int PushStack(SeqStack *S,DataType e){
if(S->top>=StackSize){
printf("栈已满,不能进栈!\n");
return 0;
}else{
S->stack[S->top]=e;
S->top++;
return 1;
}
}
int PopStack(SeqStack *S,DataType *e){
if(S->top<=0){
printf("栈已经没有元素,不能出栈!\n");
return 0;
}else{
S->top--;
*e=S->stack[S->top];
return 1;
}
}
int StackLength(SeqStack S){
return S.top;
}
void ClearStack(SeqStack *S){
S->top=0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"SeqStack.h"
typedef char VertexType[4];
typedef char InfoPtr;
typedef int VRType;
#define MaxSize 50
typedef enum{DG,DN,UG,UN}GraphKind;
typedef struct ArcNode{
int adjvex;
InfoPtr *info;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MaxSize];
typedef struct{
AdjList vertex;
int vexnum,arcnum;
GraphKind kind;
}AdjGraph;
void BriefPath(AdjGraph G,int u,int v){
int k,i;
SeqStack S;
ArcNode *p;
int visited[MaxSize];
int parent[MaxSize];
InitStack(&S);
for(k=0;k<G.vexnum;k++)
visited[k]=0;
PushStack(&S,u);
visited[u]=1;
while(!StackEmpty(S)){
PopStack(&S,&k);
p=G.vertex[k].firstarc;
while(p!=NULL){
if(p->adjvex==v){
parent[p->adjvex]=k;
printf("the path from %s to %s is:",G.vertex[u].data,G.vertex[v].data);
i=v;
do{
PushStack(&S,i);
i=parent[i];
}while(i!=u);
PushStack(&S,u);
while(!StackEmpty(S)){
PopStack(&S,&i);
printf("%s ",G.vertex[i].data);
}
printf("\n");
}else if(visited[p->adjvex]==0){
visited[p->adjvex]=1;
parent[p->adjvex]=k;
PushStack(&S,p->adjvex);
}
p=p->nextarc;
}
}
}
int LocateVertex(AdjGraph G,VertexType v){
int i;
for(i=0;i<G.vexnum;i++)
if(strcmp(G.vertex[i].data,v)==0)
return i;
return -1;
}
void CreateGraph(AdjGraph *G){
int i,j,k,w;
VertexType v1,v2;
ArcNode *p;
printf("please enter node and line: ");
scanf("%d,%d",&(*G).vexnum,&(*G).arcnum);
printf("enter %d node:",G->vexnum);
for(i=0;i<G->vexnum;i++)
{
scanf("%s",G->vertex[i].data);
G->vertex[i].firstarc=NULL;
}
printf("please two nodes of the line:\n");
for(k=0;k<G->arcnum;k++)
{
scanf("%s%s",v1,v2);
i=LocateVertex(*G,v1);
j=LocateVertex(*G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=(InfoPtr*)malloc(sizeof(InfoPtr));
p->nextarc=G->vertex[i].firstarc;
G->vertex[i].firstarc=p;
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
p->info=NULL;
p->nextarc=G->vertex[j].firstarc;
G->vertex[j].firstarc=p;
}
(*G).kind=UG;
}
void DestroyGraph(AdjGraph *G){
int i;
ArcNode *p,*q;
for(i=0;i<G->vexnum;++i)
{
p=G->vertex[i].firstarc;
if(p!=NULL)
{
q=p->nextarc;
free(p);
p=q;
}
}
(*G).vexnum=0;
(*G).arcnum=0;
}
void DisplayGraph(AdjGraph G){
int i;
ArcNode *p;
printf("the number of node is %d",G.vexnum);
for(i=0;i<G.vexnum;i++)
printf("%s ",G.vertex[i].data);
printf("\nthe number of line is:%d\n",2*G.arcnum);
for(i=0;i<G.vexnum;i++)
{
p=G.vertex[i].firstarc;
while(p)
{
printf("(%s,%s) ",G.vertex[i].data,G.vertex[p->adjvex].data);
p=p->nextarc;
}
printf("\n");
}
}
void main(){
AdjGraph G;
CreateGraph(&G);
DisplayGraph(G);
BriefPath(G,0,4);
DestroyGraph(&G);
system("pause");
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/29cbfcdada4ccfd066e9f61a999532ad.png)