Hi,

I have some vertices and edges and i have to create an adjacency list. I have taken an array and stored all vertices in the array. Now i need to add the edges to the graph. The input is given as a text file. When i read "a b 5"--> thats the edge between vertex 'a' and 'b' and has a weight 5. I dont know how to link it..... means when i read 'a' it should go to the index where i have stored 'a' and create a link to 'b'. Again if there is an edge from "a-->c" then the predecessor of node 'c' should be 'b'. Example: a-->b-->c-->/(null).

i am pasting the code that i have done till now

public void readInput()
   {
	  Scanner input = null;
	 try
	  { 
	   input = new Scanner(new FileInputStream("Test1.txt"));
		StringTokenizer st = new StringTokenizer("Test1.txt");
		String [] array = new String[10];
	   String vertex = input.next();
		System.out.println("# of Vertexs are "+vertex);
	
		 
		  for(int i=0;i<4;i++)
		   { 
			// while(input.hasNextLine())
			  
			   array[i]=input.next();
			   System.out.println("Vertex " +array[i]);
			  
			}
	
		String edges = input.next();
		System.out.println("edges" +edges);
	   		
		 }	 
	  
	  catch(FileNotFoundException e)
	  { 
	   System.out.println("The file couldnt be found");
		System.exit(0);
	  } 
	 catch(IOException e)
	  { 
	   System.out.println("Error reading from file");
		System.exit(0);
	  }   
	 }

Use LinkedList (see the API docs), or create a Custom Object that contains reference to the previous and next "edge" as instance variables as well as defining the current "edge" itself.

As per what you said(masijade) i tried to create an array of linked list using linkedlist. But i m nt able to read it from a text file. what method should i use to read from a file and store it in an array of type linked list? the line where i get the error is highlighted.
My code is as follows:

public void readInput()
   {
	 Scanner input = null;
	 try
	  { 
	   input = new Scanner(new FileInputStream("Test1.txt"));
		StringTokenizer st = new StringTokenizer("Test1.txt");
		String [] array = new String[10];
		
	        String vertex = input.next();
		System.out.println("# of Vertexs are "+vertex);
		
		LinkedList[] edge = new LinkedList[10];
	        // this for loop is to avoid null references in the array. 
		for ( int i = 0; i < edge.length; i++ ) 
		 {
		  edge[i] = new LinkedList();
                  System.out.println("linked list named edge "+edge[i]);
                }
		 
		  // need to change the condition here. cant hardcode the value!!
		  for(int i=0;i<4;i++)
		   { 
			 array[i]=input.next();
			 System.out.println("Vertex " +array[i]);
			}
			
	   // Reading the edges from the file and linking them to the vertex
		String numEdge = input.next();
		System.out.println("# of edges" +numEdge);
		for(int i=0;i<=5;i++)
		   { 
			 edge[i]=input.nextLine();
			 System.out.println("edges are " +edge[i]);
			  
			}
			
			
	   // here i need to assign edges to the vertices..... using array of linked list.
				
		
		 }	 
	  
	  catch(FileNotFoundException e)
	  { 
	   System.out.println("The file couldnt be found");
		System.exit(0);
	  } 
	 catch(IOException e)
	  { 
	   System.out.println("Error reading from file");
		System.exit(0);
	  }   
	 }

The error that i get is :

AbsGraph.java:144: incompatible types
found   : java.lang.String
required: LinkedList
			 edge[i]=input.nextLine();
			                       ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Edited 7 Years Ago by kool005: n/a

Well, you have an array of LinkedLists, not an array of Strings. You can't just read the next line and put it into the array. You have to put it into the Linked List, which is in the array.

Well, you have an array of LinkedLists, not an array of Strings. You can't just read the next line and put it into the array. You have to put it into the Linked List, which is in the array.

Could you elaborate a bit more please... an example would be more appreciated!

Could you elaborate a bit more please... an example would be more appreciated!

You have LinkedList[] edge = new LinkedList[10] which is an array of linked lists. You can't just do edge=input.nextLine(); since each edge is a LinkedList itself, not a String, which is what input.nextLine() would give you.

You have to put the Strings in the LinkedLists, not the arrays, so instead of looping through the array and trying to put the String in the array, loop through the array and add the string to the LinkedList in that section of the array.

Edited 7 Years Ago by mellowmike: n/a

Thank you for your response. I understand what your saying theoritically... but i m nt able to put it in code. My programming is a bit weak...
If i m understanding correctly... then in my code i m trying to put the strings that i read in an array which is wrong. I should link them (using linked list). But for that also i need to read the string from the file. After reading the string i have to link to the last element. How do i read from the file?? Once i read i can link using single linked list.

You're reading from the file with input.nextLine(); But what you're doing isn't working because you're trying to put the next line into the edge array, which is an array of linkedlists. However, you can put the next line in one of the linkedlists in the array with the add method.

This article has been dead for over six months. Start a new discussion instead.