前向星

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


//链式向前星其实就是有n链表,每条链表存的是所有相同结点的边。


通俗:输入时head向下指,next向上指,输出时就变成head向上,next向下



const int maxn=1100;//点数
const int maxm=11000;//边数
struct xx
{
    int next,to;
}node[maxm];//每个结点存一条边,next表示与当前边起点一样的另一条边在弄的数组中的位置。to表示这条边的终点,n表示这条边的权值。
int head[maxn];//head[i]表示从i出发的点的链表的第一个点在node数组中的位置。
int num=0;//当前已有边的个数。
void Add(int from,int to)
{
    node[num].to=to;


    node[num].next=head[from];//当前结点指向以前的头结点
    //cout<<" to   "<<node[num].to<<"   next  "<<node[num].next<<endl;
    head[from]=num++;//当前结点变为头结点
   // cout<<"head"<<" "<<head[from]<<endl;
}
void Use(int i)//遍历起点为i的链表
{
    int t=head[i];
    while(t!=-1)
    {
       // cout<<"t"<<t<<endl;
       // cout<<"from "<<i<<"to "<<node[t].to<<endl;


        t=node[t].next;
        //cout<<"node[t]"<<t<<endl;
    }
}
int main()
{
    int from,to,n;
    memset(head,-1,sizeof(head));
    memset(node,-1,sizeof(node));
    while(cin>>from>>to,from&&to&&n)
    {
        Add(from,to);
    }
    int i;
    cin>>i;
    Use(i);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值