数据结构C语言版之邻接表(各种遍历)
来源:互联网 发布:地图 数据可视化工具 编辑:程序博客网 时间:2024/05/17 08:27
//邻接表:#include<stdio.h>#include<stdlib.h>#define MAX 100#define Stack_size 100#define Stackincreament 10#define MAX_VERTE_NUM 20int visited[MAX_VERTE_NUM];typedef struct ArcNode{int adjvex;struct ArcNode *nextArc;}ArcNode,*N;typedef struct VNode{char data;ArcNode *firstarc;}VNode,AdjList[MAX_VERTE_NUM];typedef struct{AdjList vertices;int vexnum,arcnum;}ALGraph;int Location(ALGraph ALG,char v){for(int i=0;i<ALG.vexnum;i++)if(ALG.vertices[i].data==v)return i;return -1;}void CreatALGraph(ALGraph &ALG){int i,m,n;char v1,v2;printf("请输入图的顶点数和边数:\n");scanf("%d",&ALG.vexnum);scanf("%d",&ALG.arcnum);getchar();printf("请输入各个顶点:\n");for(i=0;i<ALG.vexnum;i++){scanf("%c",&ALG.vertices[i].data);ALG.vertices[i].firstarc=NULL;}printf("请输入图的各边:\n");getchar();for(i=0;i<ALG.arcnum;i++){ArcNode *p,*s;scanf("%c%c",&v1,&v2);getchar();m=Location(ALG,v1);n=Location(ALG,v2);p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=n;p->nextArc=NULL;if(!ALG.vertices[m].firstarc)ALG.vertices[m].firstarc=p;else{ArcNode *q;q=ALG.vertices[m].firstarc;while(q->nextArc)q=q->nextArc;q->nextArc=p;}s=(ArcNode*)malloc(sizeof(ArcNode));s->adjvex=m;s->nextArc=NULL;if(!ALG.vertices[n].firstarc)ALG.vertices[n].firstarc=s;else{ArcNode *r;r=ALG.vertices[m].firstarc;while(r->nextArc)r=r->nextArc;r->nextArc=s;}}}void DFS(ALGraph G,int v){ArcNode *w;visited[v]=1;printf("%c",G.vertices[v]);for(w=G.vertices[v].firstarc;w;w=w->nextArc)if(!visited[w->adjvex])DFS(G,w->adjvex);}void DFStraverse(ALGraph G){//深度优先遍历的递归算法int v;for(v=0;v<G.vexnum;v++)visited[v]=0;for(v=0;v<G.vexnum;v++)if(!visited[v])DFS(G,v);}/*typedef struct{ ArcNode *base;int top;int stacksize;}sqstack;void initstack(sqstack &s){s.base=(N)malloc(Stack_size*sizeof(ArcNode));s.stacksize=Stack_size;if(!s.base)exit(0);s.top=0;}int stackempty(sqstack s){if(s.top==0)return 1;return 0;}void push(sqstack &s,N e){if(s.top>=s.stacksize){s.base=(N)realloc(s.base,(Stack_size+Stackincreament)*sizeof(ArcNode));if(!s.base) exit(0);s.stacksize+=Stackincreament;}s.base[s.top++]=e;}void pop(sqstack &s,N e){if(s.top==0)return ;e=s.base[--s.top];}void DFS(ALGraph G,int v){ArcNode *p;sqstack s;visited[v]=1;printf("%c",G.vertices[v].data);p=G.vertices[v].firstarc;initstack(s);while(!stackempty(s) || p){while(p){if(visited[p->adjvex]==1){p=p->nextArc;}else{printf("%c",G.vertices[p->adjvex].data);visited[p->adjvex]=1;push(s,p);p=G.vertices[p->adjvex].firstarc;}}if(!stackempty(s)){pop(s,p);p=p->nextArc;}}}*/typedef struct{int *data;int front;int rear;}SqQueue;void InitQueue(SqQueue &Q){Q.data=(int*)malloc(MAX*sizeof(int));Q.front=Q.rear=0;}int QueueEmpty(SqQueue Q){if(Q.rear==Q.front)return 1;return 0;}void EnQueue(SqQueue &Q,int e){if((Q.rear%MAX)>=Q.front)return;else{Q.data[Q.rear]=e;Q.rear=(Q.rear+1)%MAX;}}void DeQueue(SqQueue &Q,int &e){if(QueueEmpty(Q))return;e=Q.data[Q.front];Q.front=(Q.front+1)%MAX;}void BFSTraverse(ALGraph G){int u,v;ArcNode *p;SqQueue Q;InitQueue(Q);for(v=0;v<G.vexnum;v++)visited[v]=0;for(v=0;v<G.vexnum;v++){if(!visited[v]){printf("%c",G.vertices[v].data);EnQueue(Q,v);visited[v]=1;while(!QueueEmpty(Q)){DeQueue(Q,u);for(p=G.vertices[u].firstarc;p;p=p->nextArc){if(!visited[p->adjvex]){printf("%c",G.vertices[p->adjvex].data);visited[p->adjvex]=1;EnQueue(Q,p->adjvex);}}}}}printf("\n");}int main(){ALGraph ALG;CreatALGraph(ALG);DFStraverse(ALG);printf("\n");BFSTraverse(ALG);return 0;}
0 0
- 数据结构C语言版之邻接表(各种遍历)
- 数据结构(C语言版)规范代码之图(邻接多重表遍历)
- 数据结构C语言版之邻接矩阵(遍历)
- 数据结构与算法(C语言版)__邻接表
- 数据结构(C语言版)规范代码之图(邻接矩阵与邻接表)
- 数据结构(C语言版)规范代码之图(邻接表的拓扑排序)
- 数据结构C语言版之排序(各种排序)
- 数据结构(c语言版)之顺序表
- 数据结构之线性表(C语言版)
- 数据结构之非递归遍历和层次遍历(C语言版)
- 熟悉笔记—数据结构(c语言版) 之 顺序表
- 数据结构学习笔记之链表(C语言版)
- 数据结构之对线性表的操作(C语言版)
- 数据结构C语言版之线性表
- 数据结构之单链表(c语言版)
- 数据结构之简单排序(C语言版)
- 数据结构(C语言版)
- 数据结构(C语言版)
- 整理的各种IDE调整内存限制的方法,看看有没有你需要的
- 机器学习种类(三)
- 数据结构C语言版之二叉树操作
- Linux 下编译 redis 报错解决办法
- OC学习篇之-----内存管理
- 数据结构C语言版之邻接表(各种遍历)
- I Hate It (HD_1754) 线段树
- QFileDialog、QColorDialog中英文提示信息翻译方法
- 关于mybatis使用中遇到的问题集合帖(持续更新)
- 使用FEC改善UDP(RTP)音视频传输效果
- 文件操作与模板编程题 #1(C++程序设计第7周)
- Java 8 的 JVM 有多快?Fork-Join 性能基准测试
- 数据结构C语言版之邻接矩阵(遍历)
- 给马小怂写的教程-----微信如何添加表情包