Graph using adjacency list in Java This is the main code.  Here I'm joining vertices by edges.

node[] gra=new node[5];
{
    for (int i = 0; i &lt; 5; i++)
        gra[i] = new node(i);  //I'm allocating memory for all the vertices.
}
void add_edge(int to,int from)  //to represent the vertice the edge is going and from is now self explanatory
{
    node new_vert=new node(to); 
    node temp=gra[from];
    while(temp.next!=null)
        temp=temp.next;
    temp.next=new_vert;
}


Now to do this operation I'm using this loop in main method.

for(int i=0;i&lt;5;i++)  //For simplicity I have taken 5 vertices
    {
        int n=sc.nextInt();                     //N is Number of edges from the vertices, for example in figure for vertex 0 the number of edges are 2.
        for(int j=0;j&lt;n;j++)
        {
            int to=sc.nextInt();
            obj.add_edge(to,i);
        }
    }




Is my implementation correct or efficient enough so that I can use this code for further use?  

Feel free to ask if you don't understand the code; I will try to explain.  I know I'm bad at explaining.  And please forgive my English.

Here is my node class

    class node
{
    int data;
    node next;
    node(int data)
    {
        this.data=data;
        this.next=null;
    }
}
Use Java LinkedList class

Almost always prefer built-in data structures to hand-rolled. For instance, your insertion routine takes O(n) time while LinkedList::addLast takes O(1) times.

Also, use an ArrayList instead of an array to hold the lists due to problems with generic arrays in Java.

Write a Graph class

A class offers a great opportunity to re-organize your code. It makes it easy to separate initialization and adding edges.

Correctness issues

Validate the input to addEdge. Throw an exception if either to or from is not in the appropriate range.

Style issues


indent and capitalize properly
write fewer comments: only when something need explaining
write shorter comments: use sentence fragments


import java.util.ArrayList;
import java.util.LinkedList;

public class Graph {
    private ArrayList&lt;LinkedList&lt;Integer&gt;&gt; adjacencyList;

    public Graph(int numberNodes) {
        adjacencyList = new ArrayList&lt;LinkedList&lt;Integer&gt;&gt;(numberNodes);
        for (int i = 0; i &lt; numberNodes; i++) {
            adjacencyList.add(new LinkedList&lt;Integer&gt;());
        }
    }

    private boolean isValidNode(int index) {
        return index &gt;= 0 &amp;&amp; index &lt; adjacencyList.size();
    }

    public void addEdge(int to, int from) {
        if (!isValidNode(to) || !isValidNode(from)) {
            throw new IndexOutOfBoundsException();
        }

        adjacencyList.get(from).addLast(to);
    }
}


import java.util.Scanner;

public class GraphRunner {    
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Graph g = new Graph(5);

        for(int from=0; from &lt; 5; from++) {
            int n=sc.nextInt();
            for(int j=0; j &lt; n; j++) {
                int to=sc.nextInt();
                g.addEdge(to, from);
            }
        }
    }
}