(学习java)邻接表 图的简单创建

//定义顶点结点
public class Vertex {
	private char vertex;
	private Side side;
	public Vertex() {
		super();
		this.vertex = '\0';
		this.side = null;
		// TODO Auto-generated constructor stub
	}
	public Vertex(char vertex, Side first) {
		super();
		this.vertex = vertex;
		this.side = first;
	}
	
	public char getVertex() {
		return vertex;
	}
	public void setVertex(char vertex) {
		this.vertex = vertex;
	}
	public Side getSide() {
		return side;
	}
	public void setSide(Side first) {
		this.side = first;
	}
	
	
}

//边结点
public class Side {
	private char name;
	//定义权重
	private int weight;
	//指向后继结点
	private Side next;
	public Side() {
		super();	
	}
	
	public Side(char name, int weight) {
		super();
		this.name = name;
		this.weight = weight;
		this.next = null;
	
	}
	
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public Side getNext() {
		return next;
	}
	public void setNext(Side next) {
		this.next = next;
	}	
}

import java.util.Scanner;

//邻接表
public class MyGraph {
	// 一维数组储存所有顶点
	private Vertex[] graph;
	// 边的数量
	int numSide;
	// 顶点的数量
	int numVertex;

	public MyGraph() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void createGraph() {
		// 输入边和顶点的数量
		Scanner sc = new Scanner(System.in);
		Scanner sc2 = new Scanner(System.in);
		System.out.println("请输入边的数量");
		numSide = sc.nextInt();
		System.out.println("请输入顶点的数量");
		numVertex = sc.nextInt();

		// 输入顶点
		getVertex();

		// 输入边
		for (int i = 0; i < graph.length; i++) {
			getfirst(graph, i);
		}
	}

	// 定义边
	private void getfirst(Vertex[] graph, int n) {
		Scanner sc = new Scanner(System.in);
		Scanner sc2 = new Scanner(System.in);
		System.out.println("请输入顶点" + graph[n].getVertex() + "的邻结点的数量 :");
		int num = sc.nextInt();
		System.out.println("请输入邻结点");
		String str = sc2.nextLine();
		// 将第一个结点和他的所有邻结点放入字符数组中
		char[] newStr = (graph[n].getVertex() + str.replaceAll("[^1-9a-zA-Z]*",
				"").substring(0, num)).toCharArray();
		// 定义graph的头结点 值为null
		Side pre = new Side();
		graph[n].setSide(pre);
		// 邻结点数量为0
		if (num == 0) {
		} else {
			for (int j = 1; j < newStr.length; j++) {
				System.out.println("请输入" + newStr[0] + "到" + newStr[j] + "的权重");
				int weight = sc.nextInt();
				Side side = new Side(newStr[j], weight);
				pre.setNext(side);
				pre = side;
			}
		}
	}

	// 获取顶点
	public void getVertex() {
		Scanner sc = new Scanner(System.in);
		this.graph = new Vertex[numVertex];
		System.out.println("请输入顶点:");
		String str = sc.nextLine();
		char[] newStr = str.replaceAll("[^1-9a-zA-Z]*", "")
				.substring(0, numSide).toCharArray();
		for (int i = 0; i < newStr.length; i++) {
			// 创建graph[]中每个vertex对象
			graph[i] = new Vertex(newStr[i], null);
		}
	}
}
测试类
public class Test02 {
	public static void main(String[] args) {
		MyGraph m = new MyGraph();
		m.createGraph();
		System.out.println("hello");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值