数据结构(四)图(一)图的表示

public class ImageGraph1 {
    private final int V;//顶点数
    private  int E ;//边数
    private Bag<Integer>[] adj;//邻接表
    public ImageGraph1(int v){
        this.V = v;
        this.E = 0;
        this.adj =(Bag<Integer>[])new Bag[v];
        for (int i = 0; i < v; i++) {
            adj[v] = new Bag<Integer>();
        }

    }
    public ImageGraph1(In in){
        this(in.readInt());
        int E = in.readInt();
        for (int i = 0; i < E; i++) {
            int v = in.readInt();
            int w = in.readInt();
            addEdge(v,w);
        }
    }
    public int V(){return V;}
    public int E(){return E;}
    public void addEdge(int v,int w){
        adj[v].add(w);
        adj[w].add(v);
        E++;
    }
    public Iterable<Integer> adj(int v){
        return adj[v];
    }
}

Bag类

public class Bag<Item> implements Iterable<Item> {
    private Item[] items = (Item[])new Object[1];
    private int N = 0;
    public Bag() {
    }
    public void add(Item item){
        if (N==items.length){resize(2*N);}
        items[N++] = item;
    }
    private void resize(int max){
        Item[] temps = (Item[])new Object[max];
        for (int i = 0; i < N; i++) {
            temps[i] = items[i];
        }
        items = temps;
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int size(){
        return items.length;
    }
    @Override
    public Iterator<Item> iterator() {
        return null;
    }
}

In控制台输入

public class In {
    public int readInt(){
        int in = 0;
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        try{
           String inStr  = reader.readLine();
           Integer num = Integer.parseInt(inStr);
            in = num.intValue();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return in;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值