1
import java.util.*;

public class q48 {
        public static LinkedList<Integer> adjacency[];
        public q48(int v)
        {
            adjacency = new LinkedList[v];
            for(int i=0; i<v; i++)
            {
                adjacency[i]=new LinkedList<Integer>();
            }
        }
        public static void implementgraph(int s,int d)
        {
            adjacency[s].add(d);
            adjacency[d].add(s);
        }
        public static void bfs(int source)
        {
            boolean visited_nodes[] = new boolean[adjacency.length];
            int parent_nodes[] =  new int[adjacency.length];
            Queue<Integer> q= new LinkedList<>();
            q.add(source);
            visited_nodes[source]=true;
            parent_nodes[source]=-1;
            while(!q.isEmpty())
            {
                int p = q.poll();
                System.out.println(p);
                for(int i:adjacency[p])
                {
                    if(visited_nodes[i] != true)
                    {
                        visited_nodes[i] = true;
                        q.add(i);
                        parent_nodes[i]=p;
                    }
                }
            }
        }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter vertice and edges : ");
        int v = sc.nextInt();
        int e = sc.nextInt();
        q48 g= new q48(v);
        System.out.println("Enter the edges : ");
        for(int i=0; i<e; i++)
        {
            int s = sc.nextInt();
            int d = sc.nextInt();
            implementgraph(s,d);
        }
        System.out.println("Enter source node : ");
        int source = sc.nextInt();
        bfs(source);
    }
}

when I compile same code in VSCode then its working perfectly but when I compile this code with Command prompt gave error

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 15 '23 at 10:36
  • You can do how it suggests and it will tell. It wants `adjacency = new LinkedList[v];` to be `adjacency = new LinkedList[v];`. But then it won't work out of the box, see https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java for details. Actually it looks very suspicious that you have a `static` field of an array of linked lists, it may not be what you really want. – tevemadar Apr 15 '23 at 10:53
  • That is only a warning? Did .class file get updated? Usually adding some well placed ‘<>’ will fix. Sometimes difficult to fix. Ignore. – John Williams Apr 15 '23 at 11:02
  • It says: "`Recompile with -Xlint:unchecked for details`". Do that! – Stephen C Apr 15 '23 at 13:16

0 Answers0