如何列出邻接链表中链表的所有成员

时间:2018-01-25 10:11:50

标签: java networking graph linked-list adjacency-list

我一直在研究这个代码,它从边缘的文本文件创建一个邻接列表。它功能齐全,但我现在正试图弄清楚如何获得每个节点的程度:

    import java.io.*;
    import java.util.*;


    class Neighbor {
        public int vertexNum;
        public Neighbor next;
        public Neighbor(int vnum, Neighbor nbr) {
                this.vertexNum = vnum;
                next = nbr;
        }
    }

    class Vertex {
        String name;
        Neighbor adjList;
        Vertex(String name, Neighbor neighbors) {
                this.name = name;
                this.adjList = neighbors;
        }
    }


    public class Graph 
    {

        Vertex[] adjLists;

        public Graph(String file) throws FileNotFoundException 
        {
            Scanner fileScanner = new Scanner(new File(file));
            fileScanner.useDelimiter("[^A-Za-z0-9]");


            ArrayList<String> words = new ArrayList<String>();

            while (fileScanner.hasNext())
            {
                String nextWord = fileScanner.next();
                if (!words.contains(nextWord))
                {
                    words.add(nextWord);
                }
            }

            adjLists = new Vertex[words.size()];

            // read vertices
            for (int v=0; v < adjLists.length; v++) 
            {
                adjLists[v] = new Vertex(words.get(v), null);
            }

            // read edges
            Scanner sc = new Scanner(new File(file));

            while (sc.hasNext()) 
            {
                 // read vertex names and translate to vertex numbers
                int v1 = indexForName(sc.next());
                int v2 = indexForName(sc.next());

                // add v2 to front of v1's adjacency list and
                // add v1 to front of v2's adjacency list
                adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);
                // Doesn't add the node twice if it's connected to itself
                if(v1!=v2) {
             adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);
                }
            }         
        }

        int indexForName(String name) 
        {
            for (int v=0; v < adjLists.length; v++) {
                if (adjLists[v].name.equals(name)) {
                    return v;
                }
            }
            return -1;
        }  

        public void print() 
        {
            System.out.println();
            for (int v=0; v < adjLists.length; v++) {
                System.out.print(adjLists[v].name);
                for (Neighbor nbr=adjLists[v].adjList; nbr != null;nbr=nbr.next) {
                    System.out.print(" --> " + adjLists[nbr.vertexNum].name);
                }
                System.out.println("\n");
            }          

        }

        public static void main(String[] args) 
        throws IOException 
        {
            Scanner br = new Scanner(System.in);
            System.out.print("Enter graph input file name: ");
            String file = br.nextLine();
            Graph graph = new Graph(file);
            graph.print();
            br.close();

        }

    }  

这是文本文件:

Sara Sam
Sara Ajay
Sam Sean
Sam Mira
Mira Jane
Jane Maria
Rahul Sapna
Sapna Rohit
Sapna Sapna
Rahul Sam

这是我得到的输出:

Sara --> Ajay --> Sam

Sam --> Rahul --> Mira --> Sean --> Sara

Ajay --> Sara

Sean --> Sam

Mira --> Jane --> Sam

Jane --> Maria --> Mira

Maria --> Jane

Rahul --> Sam --> Sapna

Sapna --> Sapna --> Rohit --> Rahul

Rohit --> Sapna

我想知道的是,如果有某种方法我可以计算出没有。邻接链表中每个顶点的边缘。例如,如果我看看Sapna:

Sapna --> Sapna --> Rohit --> Rahul

我希望我的输出读取Sapna的度数为3或者她有3条边连接到她。

所以,我一直想创建一个计算边数的方法,但我不确定要添加到我的sum变量中的内容:

int countEdges()
    {
        int sum = 0;

        //traverse all vertex
        for (int v=0; v < adjLists.length; v++)

            // add all edge that are linked to the
            // current vertex
            sum += /*???*/;

        return sum;
    } 

1 个答案:

答案 0 :(得分:0)

print()方法有什么相似之处?像(未经测试)的东西:

    public void printNrOfEdges() 
    {
        System.out.println();
        for (int v=0; v < adjLists.length; v++) {
            int sum = 0;
            for (Neighbor nbr=adjLists[v].adjList; nbr != null;nbr=nbr.next) {
                //Add 1 for each neighbour
                sum++;
            }
            System.out.print(adjLists[v].name + ": " + sum);
            System.out.println("\n");
        }
    }