Data structure Java version of the adjacency Table implementation diagram (10)

Source: Internet
Author: User

The adjacency table implementation diagram, in fact, is in an array to store the linked list, the linked list is the other nodes connected to the current node.


 Packagemygraph;Importjava.util.ArrayList;Importjava.util.List; Public classTable {PrivateList<list<character>>list; PrivateList<character>Headnodes; Private intN; Private intNverts; //List of the starting lists     PublicTable () {Super();  This. List =NewArraylist<list<character>>();  This. Headnodes =NewArraylist<character>(); List.add (NewArraylist<character>()); List.add (NewArraylist<character>()); List.add (NewArraylist<character>()); List.add (NewArraylist<character>()); }        //Add a node     Public voidAddvertx (Charx) {headnodes.add (x); }        //Add an edge     Public voidAddedge (intStartintend)        {list.get (start). Add (Headnodes.get (end));    List.get (end). Add (Headnodes.get (start)); }        //Print Node     Public  voidPrintvertice () { for(List ls:list) { for(Object i:ls) {System.out.print (i+ "\ T");        } System.out.println (); }    }    }    

Test procedure:

     Public Static voidMain (string[] args) {Table T=NewTable (); T.addvertx (' A ');//0T.addvertx (' B ');//1T.addvertx (' C ');//2T.addvertx (' d ');//3T.addedge (0, 2); T.addedge (0, 1);    T.printvertice (); //0-b C//1-a//2-a}

Test results:

C    B    //a-c  a-ba    //b-aa    //c-a

Data structure Java version of the adjacency Table implementation diagram (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.