有向边邻接链表存储

#include<iostream>
#include<cstring>
#include<cstdio>
using  namespace std;
const  int nMax= 1000;
class edge{
     public:
     int v,nex;
};edge e[nMax];
int n,k,head[nMax];       // head[i]是以点i为起点的链表头部
void addedge( int a, int b){                            // 向图中加有向边的算法,注意加上的是有向边 // b为a的后续节点既是a---->b
    e[k].v=b;
    e[k].nex=head[a];
    head[a]=k;
    k++;
}
int main(){
     int i,a,b,w,k= 1;
    scanf( " %d ",&n);
    memset(head, 0, sizeof(head));
     for(i= 0;i<n;i++){
        scanf( " %d%d ",&a,&b);
        addedge(a,b);                                  // 添加一条由a指向b的边
    }
    cin>>w;                                            // 输出w点所指向的点
     for(i=head[w];i;i=e[i].nex){
        cout<<e[i].v<<endl;
    }
    cout<< " end\n ";
     return  0;
}

转载于:https://www.cnblogs.com/xiaolongchase/archive/2012/01/27/2330191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值