import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class List{
int node;
List next;
}
class Node {
int node;
List adjList;
boolean visit;
}
public class Adjacency {
void insertVertices() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Graph taki düğüm sayısını girin: ");
int n=(Integer.parseInt(br.readLine()));
Node g[]=new Node[n];
for (int i = 0; i < n; i++) {
g[i]=new Node();
g[i].node=i+1;
g[i].visit=false;
while(true)
{
System.out.println("Diğer dugume geçmek için 0(Sıfır) girin");
System.out.println((i+1)+". Düğümün bitişik düğümlerini girin");
int adjVertex=Integer.parseInt((br.readLine()));
if(adjVertex==0)
break;
else
{
List l=new List();
l.node=adjVertex;
l.next=null;
if(g[i].adjList==null)
{
g[i].adjList=l;
}
else
{
List p=g[i].adjList;
while(p.next!=null){
p=p.next;
}
p.next=l;
}
}
}
}
System.out.println("Adjacency matrix representation");
for (int i = 0; i < n; i++) {
System.out.print(g[i].node+" --> ");
List l=g[i].adjList;
while(l!=null)
{
System.out.print(l.node+" --> ");
l=l.next;
}
System.out.println("");
}
}
public static void main(String[] args) throws IOException {
new Adjacency().insertVertices();
}
}
Hiç yorum yok:
Yorum Gönder