图的存储结构
图的存储结构和DFS/BFS代码放在一起写了~
邻接矩阵
图的存储结构/图的建立
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define OVERFLOW -2
#define MAX_VERTEX_SIZE 20
#define INT_MAX 65535
#define INFINITY INT_MAX
typedef int Status;
typedef int VertexType;
typedef int VRType;
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef struct VNode
{
VertexType data;
}VNode,AdjList[MAX_VERTEX_SIZE];
typedef struct ArcCell
{
VRType adj;
}ArcCell,AdjMatrix[MAX_VERTEX_SIZE][MAX_VERTEX_SIZE];
typedef struct
{
int vexnum,arcnum;
AdjMatrix arcs;
AdjList vertices;
GraphKind kind;
}MGraph;
Status CreateGraph(MGraph *G){
int isign,jsign,i,j;
VNode v1,v2;
int value;
(*G).kind = UDN;
printf("输入顶点数 弧的数\n");
scanf("%d%d",&(*G).vexnum, &(*G).arcnum);
printf("输入顶点信息\n");
for(i = 0; i < (*G).vexnum; ++i){
scanf("%d",&(*G).vertices[i].data);
}
for(i = 0; i < (*G).vexnum; ++i){
for(j = 0; j < (*G).vexnum; ++j){
if(i == j)
(*G).arcs[i][j].adj = 0;
else
(*G).arcs[i][j].adj = INFINITY;
}
}
printf("输入弧的信息\n");
for(i = 0; i < (*G).arcnum; ++i){
isign = -1;
jsign = -1;
scanf("%d%d%d",&v1.data,&v2.data,&value);
for(j = 0; j < (*G).vexnum; ++j){
if((*G).vertices[j].data == v1.data)
isign = j;
if((*G).vertices[j].data == v2.data)
jsign = j;
}
(*G).arcs[isign][jsign].adj = (*G).arcs[jsign][isign].adj = value;
}
return OK;
}
邻接表
图的存储结构/图的建立/BFS/DFS
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
#define MAX_VERTEX_SIZE 20
typedef int Status;
typedef int VRType;
typedef int VertexType;
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef VertexType QElemType;
int visited[MAX_VERTEX_SIZE];
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct
{
Queueptr rear,front;
}LinkQueue;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *next;
VRType weight;
}ArcNode;
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_SIZE];
typedef struct
{
int vexnum,arcnum;
AdjList vertices;
GraphKind kind;
}ALGraph;
Status InitQueue(LinkQueue *Q);
Status DestroyQueue(LinkQueue *Q);
Status EnQueue(LinkQueue *Q, QElemType e);
Status DeQueue(LinkQueue *Q, QElemType *e);
Status QueueEmpty(LinkQueue Q);
Status CreateGraph(ALGraph *G){
ArcNode *p,*q;
VNode v1,v2;
int i,j,sign1,sign2;
(*G).kind = DG;
printf("有向图\n");
printf("请输入顶点数,弧的数\n");
scanf("%d%d",&(*G).vexnum,&(*G).arcnum);
printf("请初始化顶点\n");
for(i = 0; i < (*G).vexnum; ++i){
scanf("%d",&(*G).vertices[i].data);
(*G).vertices[i].firstarc = NULL;
}
printf("请初始化弧\n");
printf("输入格式:顶点1 顶点2 (表示顶点1邻接到顶点2)\n");
for(i = 0; i < (*G).arcnum; ++i){
sign1 = -1;
sign2 = -1;
scanf("%d%d",&v1.data,&v2.data);
for(j = 0; j < (*G).vexnum; ++j){
if(v1.data == (*G).vertices[j].data)
sign1 = j;
if(v2.data == (*G).vertices[j].data)
sign2 = j;
}
p = (ArcNode*)malloc(sizeof(ArcNode));
if(!p)
exit(OVERFLOW);
p->next = NULL;
p->adjvex = sign2;
q = (*G).vertices[sign1].firstarc;
if(!q)
(*G).vertices[sign1].firstarc = p;
else{
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
return OK;
}
//测试函数-测试建图是否正确
void test(ALGraph G){
int i;
ArcNode *p;
for(i = 0; i < G.vexnum; ++i){
printf("i = %d\n",i);
for(p = G.vertices[i].firstarc; p; p = p->next)
printf("p->adjvex = %d\n",p->adjvex);
}
}
//图的销毁函数 //存在问题
/*
Status DestroyGraph(ALGraph *G){
int i;
ArcNode *p,*q;
for(i = 0; i < (*G).vexnum; ++i){
p = (*G).vertices[i].firstarc;
while(p){
q = p->next;
free(p);
p = q;
}
}
return OK;
}
Status DestroyGraph(ALGraph *G){
int i;
for(i = 0; i < (*G).vexnum; ++i){
p = (*G).vertices[i].firstarc;
while(p){
q = p->next;
free(p);
p = q;
}
(*G).vertices[i].firstarc = NULL;
}
free(G);
}
*/
Status Visit(VertexType e){
printf("%d",e);
return OK;
}
Status DFS(ALGraph G,int i){
ArcNode *p;
visited[i] = 1;
Visit(G.vertices[i].data);
for(p = G.vertices[i].firstarc; p; p = p->next){
if(!visited[p->adjvex])
DFS(G,p->adjvex);
}
return OK;
}
Status DFSTraverse(ALGraph G){
int i;
for(i = 0; i < G.vexnum; ++i)
visited[i] = 0;
for(i = 0; i < G.vexnum; ++i){
if(!visited[i])
DFS(G,i);
}
return OK;
}
Status BFS(ALGraph G, int i){
LinkQueue Q;
ArcNode *p;
int x;
InitQueue(&Q);
visited[i] = 1;
Visit(G.vertices[i].data);
EnQueue(&Q,i);
while(!QueueEmpty(Q)){
DeQueue(&Q,&x);
p = G.vertices[x].firstarc;
while(p){
if(!visited[p->adjvex]){
EnQueue(&Q,p->adjvex);
visited[p->adjvex] = 1;
Visit(G.vertices[p->adjvex].data);
}
p = p->next;
}
}
DestroyQueue(&Q);
return OK;
}
Status BFSTraverse(ALGraph G){
int i;
for(i = 0; i < G.vexnum; ++i)
visited[i] = 0;
for(i = 0; i < G.vexnum; ++i){
if(!visited[i])
BFS(G,i);
}
return OK;
}
int main(){
ALGraph G;
CreateGraph(&G);
printf("\nDFSTraverse\n");
DFSTraverse(G);
printf("\nBFSTraverse\n");
BFSTraverse(G);
LinkQueue Q;
InitQueue(&Q);
// DestroyGraph(&G);
return 0;
}
Status InitQueue(LinkQueue *Q){
(*Q).rear = (*Q).front = (Queueptr)malloc(sizeof(QNode));
if(!(*Q).front)
exit(OVERFLOW);
(*Q).front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q){
while((*Q).front){
(*Q).rear = (*Q).front->next;
free((*Q).front);
(*Q).front = (*Q).rear;
}
return OK;
}
Status EnQueue(LinkQueue *Q, QElemType e){
Queueptr p = (Queueptr)malloc(sizeof(QNode));
if(!p)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
(*Q).rear->next = p;
(*Q).rear = p;
}
Status DeQueue(LinkQueue *Q, QElemType *e){
Queueptr p = (*Q).front->next;
if((*Q).front == (*Q).rear){
printf("队空\n");
return ERROR;
}
*e = p->data;
if((*Q).rear == p){
(*Q).rear = (*Q).front;
}
(*Q).front->next = p->next;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q){
if(Q.front == Q.rear)
return TRUE;
else
return FALSE;
}