模块依赖关系分析-Java实现

题目描述:

1.一个系统的若干模块间存在相互依赖的关系,如果A模块调用了B模块的接口,那么成为A依赖B,记为A->B
如果A->B,B->A,那么称A和B模块都存在循环依赖。
如果A->B,B->C,C->D,D->B,那么BCD存在循环依赖,A不存在循环依赖,依次类推。
先输入若干模块之间的关系,判断某模块是存在循环依赖。

 输入:
       {0x00, 0x01},
       {0x02, 0x03},
       {0x03, 0x04}
 输出:
       {0x00, false},
       {0x01, false},
       {0x02, false},
       {0x03, false},
       {0x04, false}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
    static ArrayList> arrayList = new ArrayList>();//存储模块间依赖关系的数据结构
    static HashSet hashSet = new HashSet(); //存储具有循环依赖的点
    static Set set = new LinkedHashSet();//用来保证按输入顺序输出
    //static Set set = new HashSet(); //如果不需要保证输出顺序用HashSet
    public static void main(String[] args){
        Boolean isCrileNode = false;
        Scanner s = new Scanner(System.in);
        while(s.hasNext()){
            //输入=====
            String temp;
            do{
                String ss = s.nextLine();
                temp = ss;
                ss = ss.replace("{", "");
                ss = ss.replaceAll("[^a-z^0-9]", " ");
                String[] strings = ss.split("\\s+");
                for(int i=0; i< strings.length; i++){
                    set.add(strings[i]);
                }
                AddDependency(strings[0], strings[1]);
            }while(temp.charAt(temp.length()-1) == ',');

            CricleNode();

            //输出=====
            int count = 0;
            for(String str : set){
                isCrileNode = MouduleIsCycularDependency(str);
                count++;
                if(count != set.size()){
                    System.out.println("{"+str+", "+isCrileNode+"},");
                }else {
                    System.out.println("{"+str+", "+isCrileNode+"}");
                }   
            }
            clear();
        }
    }
    //添加依赖关系
    public static void AddDependency(String Modeled, String DependModuled){
        LinkedList linkedList = new LinkedList();
        linkedList.add(Modeled);
        linkedList.add(DependModuled);
        arrayList.add(linkedList);
        Boolean change = true;
        while(change){
            change = ConnectList();
        }

    }
    //判断其中的链表是否有可以相连的
    public static Boolean ConnectList(){
        if(arrayList.size() > 1){
            for(int i = 0; i < arrayList.size()-1; i++){
                for(int j= i+1; j < arrayList.size(); j++){
                    if(arrayList.get(i).getFirst().equals(arrayList.get(j).getLast())){
                        LinkedList link1 = arrayList.get(j);
                        link1.removeLast();
                        link1.addAll(arrayList.get(i));
                        arrayList.remove(j);
                        arrayList.remove(i);
                        arrayList.add(link1);
                        return true;
                    }else if(arrayList.get(i).getLast().equals(arrayList.get(j).getFirst())){
                        LinkedList link1 = arrayList.get(i);
                        link1.removeLast();
                        link1.addAll(arrayList.get(j));
                        arrayList.remove(j);
                        arrayList.remove(i);
                        arrayList.add(link1);
                        return true;
                    }
                }
            }
        }
        return false;
    }
    //获取依赖点集合
    public static void CricleNode(){

        for(int i = 0; i < arrayList.size(); i++){
            int start = arrayList.get(i).indexOf(arrayList.get(i).getLast());
            int end = arrayList.get(i).lastIndexOf(arrayList.get(i).getLast());
            if(start != end){
                for(int j = start; j < end; j++){
                    hashSet.add(arrayList.get(i).get(j));
                }
            }
            int start1 = arrayList.get(i).indexOf(arrayList.get(i).getFirst());
            int end1 = arrayList.get(i).lastIndexOf(arrayList.get(i).getFirst());
            if(start1 != end1){
                for(int j = start1; j < end1; j++){
                    hashSet.add(arrayList.get(i).get(j));
                }
            }
        }
    }
    //判断模块是否存在依赖关系
    public static boolean MouduleIsCycularDependency(String ModuleId){
        if(hashSet.contains(ModuleId))
            return true;
        return false;
    }
    //清空模块数据
    public static void clear(){
        arrayList.clear();
        hashSet.clear();
        set.clear();
    }
}

你可能感兴趣的:(Java,java,模块依赖关系分析,华为笔试)