- import java.util.Arrays;
- /*
- * 圖形串列表示法
- */
- public class GraphLink {
- Node frist ;
- Node last ;
- public static void main(String[] args) {
- int[][] am = {{1,2},{2,1},{3,2},{2,3},
- {4,2},{2,4},{1,5},{5,1},
- {5,3},{3,5},{5,4},{4,5},
- {3,4},{4,3}};//每個邊
- int[][] am2 = {{1,2},{2,3},{2,4},{4,3}};
- GraphLink[] g = new GraphLink[6];
- System.out.println("無向圖形");
- for(int i = 1 ; i < 6 ; i++){
- g[i] = new GraphLink();
- System.out.print("頂點"+i+"=>");
- for(int j = 0 ; j < 14 ; j++){
- if(i == am[j][0])
- g[i].Insert(am[j][1]);
- }
- g[i].print();
- }
- System.out.println("有向圖形");
- for(int i = 1 ; i < 6 ; i++){
- g[i] = new GraphLink();
- System.out.print("頂點"+i+"=>");
- for(int j = 0 ; j < 4 ; j++){
- if(i == am2[j][0])
- g[i].Insert(am2[j][1]);
- }
- g[i].print();
- }
- }
- public void Insert(int vertice){
- Node node = new Node(vertice);
- if(frist == null){
- frist = node ;
- last = frist ;
- }else{
- last.next = node ;
- last = node ;
- }
- }
- public void print(){
- Node curr = frist ;
- if(curr == null) System.out.print("空串列");
- while(curr != null){
- System.out.print(curr.vertice+" ");
- curr = curr.next ;
- }
- System.out.println();
- }
- }
- class Node{
- String j ;
- int vertice ;
- Node next ;
- Node(String j){
- this.j = j ;
- }
- Node(int v){
- vertice = v ;
- }
- }
2017年7月29日 星期六
(Java) 圖形串列表示法
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。