图的储存(数组模拟临街表)

图的储存(数组模拟临街表)

储存:树的储存可以用图来存储,图的数组模拟邻接矩阵:小兵应该先武装好自己,然后再加入队伍,队长负责周转(管最前面一个)。

遍历:遍历的话也好遍历,管好头尾,头就是队长,尾就是-1。

 

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 struct edge_node{
 5     int to;
 6     int next;
 7 }edge[100];
 8 int main(){
 9     freopen("edge.txt","r",stdin);
10     int vertex[10];
11     memset(vertex,-1,sizeof(vertex));
12     int n;
13     cin>>n;
14     int cnt=1; 
15     for(int i=1;i<=n;i++){
16         int a,b;
17         cin>>a>>b;
18         //武装自己
19         edge[cnt].to=b;
20         edge[cnt].next=vertex[a];
21         vertex[a]=cnt++; 
22     }
23     cout<<"---------------输出每个节点的存储情况---------------"<<endl;
24     cout<<"edge[i].to"<<" "<<"edge[i].next"<<endl;
25     for(int i=1;i<=6;i++){
26         cout<<i<<" "<<edge[i].to<<" "<<edge[i].next<<endl;
27     }
28     //输出每个节点的边
29     cout<<"---------------输出每个节点的边---------------"<<endl;
30     for(int i=1;i<=6;i++){//顶点 
31         cout<<i<<": ";
32         for(int j=vertex[i];j!=-1;j=edge[j].next){//遍历边 
33             cout<<edge[j].to<<" ";
34         }
35         cout<<endl; 
36     } 
37     return 0;
38 } 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值