Java DFS+BFS

reference《图解数据结构java》

error:Type of argument cannot be primitive type

solution:

Every variable in java has a type, which essentially tells Java how that variable should be treated, and how much memory should be allocated for that variable. java has basic types for characters, different kinds of intergers, and different kinds floating of point numbers,and also type for values true and false – char ,byte,short,int,long,float,double,boolean.Java classes, as you probably know, are created to solve object oriented problems. Any object of a class has the type of “class”. A variable of a class type -like a String-stores objects of its class diffrently from how variables of primitive types-like int or char -store their values.Every variable, whether it’s of a primitive type or of a class type, is implemented as a location in computer memory. For a variable of a primitive type, the value of the variable is stored in the memory location assigned to the variable. For example, if an integer variable is declared as “int x = 4”, then when we look at the memory location ,there will be a “4”stored there .However, a variable of a class only stores the memory address of where the object is located-not the values inside the object. Let me make it more clear. If we have a class called “Aclass”, when we create an object like this:”A object”, then we look at the memory, we will find that it does not store any of the variables that belong to that object in memory. Instead, the variable “object” stores an address of another place in memory where all the details of “object” reside. That means it just stores the address of where the object is stored. This memory address is called a reference to the object.

/**************** java dfs+bfs **************************** * 2016年10月8日 author:shabbyju * ********************************************************************/
import java.util.*;
class Node{
    int x;
    Node next;
    public Node(int x){
        this.x = x;
        this.next = null;
    }
}

class GraphLink{
    /*方便起见顶点信息不设置*/;
    public Node first;
    public Node last;
    public boolean isEmpty(){
        return first==null;
    }
    public void print(){
        Node current = first;
        while(current!=null){
            System.out.print("["+current.x+"] ");
            current = current.next;
        }
        System.out.println();
    }
    public void Insert(int x){
        Node newNode = new Node(x);
        if(this.isEmpty()){
            first = newNode;
            last = newNode;
        }else{
            last.next = newNode;
            last = newNode;
        }
    }
}

public class Cat{
    public static int run[]=new int[9];
    public static GraphLink Head[]=new GraphLink[9];
    public static void dfs(int current)             //深度优先遍历子程序
    {
        run[current]=1;
        System.out.print("["+current+"] ");

        while((Head[current].first)!=null)
        {
            if(run[Head[current].first.x]==0) //如果顶点尚未遍历,就进行dfs的递归调用
                dfs(Head[current].first.x);
            Head[current].first=Head[current].first.next;
        }
    }

    public static void bfs(int current){
        LinkedList<Integer> queue = new LinkedList<Integer>();
        Node tempNode;
        queue.offer(current);
        run[current] = 1;
        System.out.print("["+current+"]");
        while(!queue.isEmpty()){
            current  = queue.poll();
            tempNode = Head[current].first;
            while(tempNode!=null){
                if(run[tempNode.x] == 0){
                    queue.offer(tempNode.x);
                    run[tempNode.x] = 1;
                    System.out.print("["+tempNode.x+"]");
                }
                tempNode = tempNode.next;
            }
        }
    }
    public static void main (String args[]) {
        int Data[][] =  //无向图的边

                { {1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},
                        {3,7},{7,3},{4,5},{5,4},{6,7},{7,6},{5,8},{8,5},{6,8},{8,6} };
        int DataNum;
        int i,j;
        System.out.println("图形的邻接表内容:"); //打印图形的邻接表内容
        for ( i=1 ; i<9 ; i++ ){
            run[i]=0;               //设定所有顶点成尚未遍历过
            Head[i]=new GraphLink();
            System.out.print("顶点"+i+"=>");
            for( j=0 ; j<20 ;j++){
                if(Data[j][0]==i) {
                    DataNum = Data[j][1];
                    Head[i].Insert(DataNum);
                }
            }
            Head[i].print();           //打印图形的邻接表内容
        }
        System.out.println("深度优先遍历顶点:");   //打印深度优先遍历的顶点
        dfs(1);
       // bfs(1);
        System.out.println("");
    }
}
    原文作者:DFS
    原文地址: https://blog.csdn.net/sinat_31741803/article/details/52760147
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞