avatar
文章
23
标签
8
分类
0
首页
音乐
照片
友链
说说
关于
LogoLucky数据结构:图(C语言)
搜索
首页
音乐
照片
友链
说说
关于

数据结构:图(C语言)

发表于2019-12-12|更新于2025-03-03
|总字数:1.2k|阅读时长:6分钟

实现图的创建,广度,深度遍历,拓扑排序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include<stdio.h>
#define MAXQSIZE 50
#define MVNum 50
#define error MVNum+10
#define OVERFLOW 0
typedef int Status;
#define OK 1
#define ERROR 0
#define null -1
#include "stdlib.h"
#include "string.h"

typedef struct arcnode{
int adjvex;
struct arcnode *nextatc;
}arcnode;

typedef struct vnode{
int data;
arcnode *firstarc;
}vnode,adjlist[MVNum];

typedef struct{
adjlist vertices; //特殊数组
int vexnum,arcnum; //顶点数,边数
}algraph;

//循环队列定义
typedef struct
{
int *base;
int front;
int rear;
} SqQueue;
//循环队列的初始化
Status InitQueue(SqQueue &Q)
{
Q.base=new int[MAXQSIZE];
if(!Q.base)
return(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
//入队
Status EnQueue(SqQueue &Q,int e)
{
if((Q.rear+1)%MAXQSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
//出队
Status DeQueue(SqQueue &Q,int &e)
{
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}

int pankong(SqQueue Q)
{
if(Q.front==Q.rear) return 1;
return 0;
}

//栈的定义 (实数栈)
typedef struct StackNode1
{
float data;
struct StackNode1 *next;
}StackNode1,*LinkStack1;
//栈的初始化 (实数栈)
Status InitStack1(LinkStack1 &S)
{
S=NULL;
return OK;
}
//判断栈是否为空(实数栈)
int StackEmpty1(LinkStack1 S)
{
if(S==NULL)return 1;
return 0;
}
//入栈 (实数栈)
Status Push1(LinkStack1 &S,int e)
{
LinkStack1 p;
p=(LinkStack1)malloc(sizeof(StackNode1));
p->data=e;
p->next=S;
S=p;
return OK;
}
//出栈 (实数栈)
Status Pop1(LinkStack1 &S,int &e)
{
LinkStack1 p;
if(S==NULL) return ERROR;
e=S->data;
p=S;
S=S->next;
delete p;
return OK;
}





//找出节点位置
int location(algraph g,int h){
for(int i = 0;i<g.vexnum ;i++){
if(g.vertices[i].data ==h)
return i;
}
}

//创建一个邻接表
void createudg(algraph *g,algraph *s){
int ww[MVNum];
printf("请输入顶点个数:\n");
scanf("%d",&g->vexnum);
s->vexnum = g->vexnum ;

printf("请输入边数:\n");
scanf("%d",&g->arcnum);
s->arcnum = g->arcnum ;

printf("请输入%d个顶点的值:\n",g->vexnum);
for(int i = 0;i<g->vexnum;i++){
scanf("%d",&ww[i]);}


for(int i = 0;i< g->vexnum ; i++){
g->vertices[i].data = ww[i];
s->vertices[i].data = ww[i];
g->vertices[i].firstarc = NULL;
s->vertices[i].firstarc = NULL;

}



printf("请输入各个边的两点值:\n");
for(int i=0 ; i < g->arcnum ; i++){
int j,k;
scanf("%d",&j);
scanf("%d",&k);
int w = location(*g,j);
int m = location(*g,k);
//printf("%d,%d",w,m);
arcnode *p1 = new arcnode;
p1->adjvex = m;
p1->nextatc = g->vertices[w].firstarc;
g->vertices[w].firstarc = p1;

arcnode *p2 = new arcnode;
p2->adjvex = w;
p2->nextatc = s->vertices[m].firstarc;
s->vertices[m].firstarc = p2;

}
printf("邻接表,逆邻接表创建成功。\n");
}



//深度优先搜索
void dfs_al(algraph g,int v,int a[]){

a[v] = 1;
arcnode *p1;
p1 = g.vertices[v].firstarc;
printf("%d\t",g.vertices[v].data);
while(p1!=NULL){
int w = p1->adjvex;
if(!a[w])
dfs_al(g,w,a);
p1= p1->nextatc;
}
}
//返回下一个元素
int firstadj(algraph g,int v){
int h;
arcnode *p1 = new arcnode;
p1 = g.vertices[v].firstarc;

if(p1==NULL)
return -1;
h = p1->adjvex;
return h;
}

//返回下下一个
int nextadj(algraph g,int v,int w){
arcnode *p1 = new arcnode;
arcnode *p2 = new arcnode;
p1 = g.vertices[v].firstarc;
while(1){
if(p1->nextatc==NULL)
return -1;
p2=p1->nextatc;
if(p1->adjvex == w)
return p2->adjvex;
p1=p2;
}


}
//广度优先搜索
void bfs_al(algraph g,int v,int a[]){
SqQueue S4;
int u,w;
InitQueue (S4);
a[v] = 1;
EnQueue(S4,v);
while(!pankong(S4)){
DeQueue(S4,u);
printf("%d\t",g.vertices[u].data);
for(w=firstadj(g,u);w>=0;w=nextadj(g,u,w)){
if(!a[w]){
a[w] = 1;
EnQueue(S4,w);
}
}
}
}
//确定度数
int findindeggre(algraph g,int v){
int ww=0;
arcnode *p = new arcnode;
p=g.vertices[v].firstarc;
if(p==NULL)
return 0;
while(p!=NULL){
p=p->nextatc;
ww++;
}
return ww;

}


//拓扑排序
int topolog(algraph g,algraph s,int a[]){
int indegree[MVNum],u,k;
arcnode *p = new arcnode;
for(int i = 0;i<g.vexnum;i++)
indegree[i]=findindeggre(s,i);

LinkStack1 S;
InitStack1(S);
for(int i=0;i<g.vexnum ;i++)
if(!indegree[i])
Push1(S,i);
int m=0;
while(!StackEmpty1(S)){
Pop1(S,u);
a[m]=u;
++m;
p=g.vertices[u].firstarc;
while(p!=NULL){
k=p->adjvex;
--indegree[k];
if(indegree[k]==0)
Push1(S,k);
p=p->nextatc;
}
}
if(m<g.vexnum)
return ERROR;
else
return OK;
}


void main(){
int v;
int visited[MVNum],top[MVNum];
algraph G,SS;
createudg(&G,&SS);


printf("\n----------------------------------------\n\n");
printf("\n请输入从第几个顶点开始遍历:\n");
scanf("%d",&v);
v-=1;
for(int i = 0;i<G.vexnum;i++)
visited[i]=0;
printf("深度优先遍历为:\n");
dfs_al(G,v,visited);

for(int i = 0;i<G.vexnum;i++)
visited[i]=0;
printf("\n广度优先遍历为:\n");
bfs_al(G,v,visited);
printf("\n----------------------------------------\n\n");
printf("\n拓扑排序后顺序为:\n");
int c=topolog(G,SS,top);
if(c==0)
printf("图中有回路存在\n");
else{
for(int i=0;i<G.vexnum ;i++){
int lc,ly;
lc=top[i];
ly=G.vertices[lc].data;
printf("%d\t",ly);
}
}
printf("\n----------------------------------------\n\n");
}

文章作者: 刘同学
文章链接: https://mouhorse.github.io/2019-12-12/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E5%9B%BE-C%E8%AF%AD%E8%A8%80/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Lucky!
数据结构
赞助
  • wechat
    wechat
  • alipay
    alipay
cover of previous post
上一篇
数据结构:块链(C语言)
...
cover of next post
下一篇
pytorch基础
持续补充 1import torch 1.随机相关 123456789a = torch.randn(2,3) 正态分布b = torch.rand(2,3) 0~1范围内随机c = torch.rand_like(a) 模仿a的形状生成随机矩阵d = torch.randint(1,10,[2,3,4]) 在1~10内生成形状为(2,3,4)的矩阵a = a.cuda() 将数据加载到gpu内 2.查看数据形状 12345a.type() 输出数据类型a.size() == a.shape 输出张量形状a.dim() 输出维度 3.生成矩阵 1234567891011121314151617181920212223242526272829303132a = torch.full([3,4,5],6) (3,4,5)形状矩阵,用6填满tensor([[[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.], ...
相关推荐
cover
2019-10-26
数据结构:顺序表(c语言)
...
cover
2019-10-29
数据结构:链表(C语言)
...
cover
2019-11-25
数据结构:块链(C语言)
...
avatar
刘同学
欢迎光临我的博客
文章
23
标签
8
分类
0
Follow Me
公告
欢迎来到我的博客!
可以交换友链
联系方式:485182274@qq.com
最新文章
Hexo本地与云端布局不同处理办法
Hexo本地与云端布局不同处理办法2025-02-25
解决 Hexo 部署到 GitHub Pages 自定义域名失效
解决 Hexo 部署到 GitHub Pages 自定义域名失效2025-02-24
Butterfly 个性化配置教程
Butterfly 个性化配置教程2025-02-23
Hexo安装并修改主题
Hexo安装并修改主题2025-02-23
MNE脑电预处理
MNE脑电预处理2024-10-07
©2018 - 2025 By 刘同学
框架 Hexo 7.3.0|主题 Butterfly 5.3.3
活出个样子给自己看
搜索
数据加载中