Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple Adjacent List implementation

  1. #1
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Adjacent List implementation

    I'm starting to learn graphs and I want to implement via adjacent list so I thought I'd use an array of linked lists, specifically an ArrayList of LinkedList. I want to read in adjacenct vertices from a file, so why does compiler complain about: void type not allowed here

    For my file, the first line is the number of vertices in graph, so that's size of the array (ArrayList), and -999 is indicator in file of when to stop reading into current array index and move to next one
    e.g.
    10
    0 1 2 -999
    1 2 3 5 -999
    2 4 -999
    4 3 -999
    5 6 -999
    6 8 -999
    7 3 8 -999
    8 10 -999
    9 4 7 10 -999


    public class Graphs_runner
    {	
    	public static void main(String[] args)
    	{
    		ArrayList< LinkedList<Integer> > adj_ls = new ArrayList< LinkedList<Integer> >();
    		Scanner sc = new Scanner( System.in );
    		int num_vertex = sc.nextInt();
     
    		//now read adjacent vertices into each index
    		for ( int i = 0; i < adj_ls.size(); ++i )
    			while ( sc.hasNextInt() && sc.nextInt() != -999 )
    				adj_ls.add(i, new LinkedList<Integer>().addLast( sc.nextInt() ));//line 62 in my source
    	}
    }

    EDIT: I figured it out, it's b/c ArrayList::add() expects 2nd param to be Obj but LinkedList::addLast is void type
    Last edited by IHeartProgramming; October 1st, 2012 at 12:23 AM.


  2. #2
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Adjacent List implementation

    Here's a new problem, I'm trying to read into each index all the adjacent vertices for a given starting vertex. For the data I'm using, the first line is the number of vertices which is used to create length of ArrayList. The -999 is a marker to indicate end of adjacent vertices for current starting vertex, so to move on to next index in ArrayList. Just reminder, I'm building array of linked list (via Java's generic containers: so ArrayList of LinkedList<Integer>.

    This is the data I'm reading from:

    10
    0 1 5 -999
    1 2 3 5 -999
    2 4 -999
    3 -999
    4 3 -999
    5 6 -999
    6 8 -999
    7 3 8 -999
    8 10 -999
    9 4 7 10 -999


    public class Graphs
    {	
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner( System.in );
    		int num_vertex = sc.nextInt();//size of adjacency list (so arr length)
    		ArrayList< LinkedList<Integer> > adj_ls = new ArrayList< LinkedList<Integer> >(num_vertex);
    		LinkedList<Integer> l = new LinkedList<Integer>();
     
    		for ( int i = 0; i < num_vertex; ++i )//store into each index (size: num_vertex)
    		{
    			while ( sc.hasNextInt() && sc.nextInt() != -999 )
    			{
    				adj_ls.add(i, new LinkedList< Integer>() );
    				adj_ls.get(i).addLast( sc.nextInt() );//keeps inserting new node @ end of current LL, once we exit while loop to go to nxt index, a
    				//	another new LL obj created for cur index
    			}
    		}
     
    		//now print each indice's adjacent vertices
    		Iterator< LinkedList<Integer> > iter = adj_ls.iterator();
     
    		while ( iter.hasNext() )
    			System.out.print(iter.next() );
     
    	}
    }

    I'm getting funny output of:

    [5][4][3][6][8][10][10][4][-999][3][-999][2][-999][1]

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Simple Adjacent List implementation

    Quote Originally Posted by IHeartProgramming View Post
    ...I'm getting funny output...
    For starters, how about adding some print statements to make the program show you exactly what it is adding to the Lists? Maybe something like
            for (int i = 0; i < num_vertex; ++i)
            {
                System.out.printf("Begin adding for i = %d\n", i);
                while (sc.hasNextInt() && sc.nextInt() != -999)
                {
                    adj_ls.add(i, new LinkedList< Integer>()); 
     
                    // For debugging: Use a temporary variable so that you can print it out
                    int temp = sc.nextInt();
                    System.out.printf("vertex %d: Adding %d to adj_ls\n", i, temp);
                    adj_ls.get(i).addLast(temp);
                }
                // Make sure you got the correct number in each list
                System.out.printf("List for vertex %d has size = %d\n",
                        i, adj_ls.get(i).size());
     
            }

    See how it goes? Make the debug print statements verbose enough to let you know exactly what the printed stuff means at each step.


    Cheers!

    Z
    Last edited by Zaphod_b; October 1st, 2012 at 04:28 PM.

Similar Threads

  1. Please Help: Simple Double Linked List (remove, addBefore, )
    By yeohv in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 12:58 PM
  2. Reading characters from a simple output to list words.
    By adwodon in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 16th, 2012, 03:34 PM
  3. Simple linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2011, 10:40 AM
  4. java newbie..simple mail server implementation
    By saurabh4dudes in forum Java Networking
    Replies: 0
    Last Post: March 12th, 2010, 08:53 AM
  5. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM