custurd122000 0 Unverified User

I'm trying to read a text file of a graph and print information about the graph including the order and size of the graph, rather it is a directed or undirected graph, if it is directed the in and out degree, and the and a list of all vertices for which it is adjacent. The problem is the adjacency list is just printing out the list of vertices instead of the adjacency list. Is there something in my code that is problematic? Also I'm unsure of how to go about the in and out degree do you guys have any ideas?

"graphs.txt"
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class AdjList {
    private Node first;        // beginning of list
    private int N;             // size of list

    // helper linked list data type
    private static class Node {
        String name;
        Node next;
        Node(String name, Node next) {
            this.name = name;
            this.next = next;
        }
    }

    public boolean isEmpty() { return (first == null); }
    public int size()        { return N; }

    // add s to the adjacency list
    public void insert(String s) {
        first = new Node(s, first);
        N++;
    }

    // return string representation of list (in reverse order of list)
    public String toString() {
        String s = "";
        for (Node x = first; x != null; x = x.next)
            s = x.name + "-> " + s;
        return "{ " + s+"->" + "}";
    }

    // return array of strings comprising this adjacency list
    public String[] toArray() {
        String[] names = new String[N];
        int i = N;
        for (Node x = first; x != null; x = x.next)
            names[--i] = x.name;
        System.out.println(names);
        return names;
    }



    public static void main(String[] args) throws FileNotFoundException {

        AdjList adjlist = new AdjList();
        Scanner sc = new Scanner(new File("graphs.txt"));




        String line = sc.nextLine();
        String[] data=line.split("[\\,]");
        int order=Integer.parseInt(data[0]);

        int typeOfGraph=Integer.parseInt(data[1]);
      //Prints results of the order
        System.out.println("The order of the graph = " + order);
        while(sc.hasNext())
        {
            line=sc.nextLine();
            int index =0;
            int count=0;
            char edges = ',';
            while(index<line.length()) {

                if(line.charAt(index)==edges){
                    count++;
                }
                index++;
            }
            /* PRINTS the result for each line!!! */
            System.out.println("The size of graph = "+count);


            //Determines if graph is zero or one

            String[] data2=line.split("[\\|]");
            String[][] vertices = new String[ data2.length ][];
            for (int i = 0; i < data2.length; i++){
                vertices[i] = data2[i].split("[ ]");
            }

            count=0;
            index=0;
            System.out.println("Graph: ");
            int i=0;
            int j=0;

            for ( j = 0; j < vertices.length; j++){
                for ( i = 0; i < vertices[j].length; i++){

                    System.out.print("Index "+i+": "+vertices[j][i]+" \n");



                }
            }
            if(typeOfGraph==1)
            {
                System.out.println("Graph is a directed graph");

            }
            if(typeOfGraph==0)
            {
                System.out.println("Graph is a undirected graph");
            }
    System.out.println("Adjacency List");
        adjlist.insert(line);
        System.out.println(adjlist);
    }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.