Code:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class IncidentList
{
private Map
private int numberOfVertices;
public IncidentList(int numberOfVertices)
{
this.numberOfVertices = numberOfVertices;
incidentList = new HashMap
for (int vertex = 1; vertex <= numberOfVertices; vertex++)
incidentList.put(vertex, new LinkedList
}
public void setEdge(int sourcevertex, int destinationvertex, int edgeNumber)
{
List
slist.add(edgeNumber);
return;
}
public List
{
return incidentList.get(vertex);
}
public void printIncidentList()
{
System.out.println("Vertex EdgeNumber");
for (int vertex = 1; vertex <= numberOfVertices; vertex++)
{
System.out.print(vertex + ":");
List
for (int j = 1; ; j++)
{
if (j != edgeList.size())
System.out.print(edgeList.get(j - 1) + "\t");
else
{
System.out.print(edgeList.get(j - 1));
break;
}
}
System.out.println();
}
}
public static void main(String... arg)
{
int numberOfVertices, numberOfEdges;
int source, destination, edgeNumber;
int edgeCount = 1;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberOfVertices = scanner.nextInt();
IncidentList incidentList = new IncidentList(numberOfVertices);
System.out.println("Enter the number of edges");
numberOfEdges = scanner.nextInt();
System.out.println("Enter the edges format :
while (edgeCount <= numberOfEdges)
{
edgeNumber = scanner.nextInt();
source = scanner.nextInt();
destination = scanner.nextInt();
incidentList.setEdge(source, destination, edgeNumber);
edgeCount++;
}
System.out.println("\nThe Incident List is ");
incidentList.printIncidentList();
scanner.close();
}
}
Output:
Enter the number of vertices
5
Enter the number of edges
5
Enter the edges format :
1 1 2
2 2 4
3 5 4
4 4 3
5 5 1
The Incident List is
Vertex EdgeNumber
1 : 1 5
2 : 1 2
3 : 4
4 : 2 3 4
5 : 3 5
More Java Programs: