Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Simpul{`
- String info;
- Simpul next;
- Jalur arc;
- Simpul(){
- }
- }
- class Jalur{
- String info;
- Jalur next;
- Simpul node;
- Jalur(){
- }
- }
- class Graph{
- Simpul first;
- Graph(){
- }
- void createEmpty(){
- first = null;
- }
- void insert(String c){
- Simpul baru;
- baru = new Simpul();
- baru.info = c;
- baru.next = null;
- baru.arc = null;
- if(first == null){
- first = baru;
- }
- else{
- Simpul last = first;
- while(last.next != null){
- last = last.next;
- }
- last.next = baru;
- }
- }
- void addJalur(Simpul tujuan, String beban, Simpul awal){
- Jalur arc;
- arc = new Jalur();
- arc.info = beban;
- arc.next = null;
- arc.node = tujuan;
- if(awal.arc == null){
- awal.arc = arc;
- }
- else{
- Jalur last = awal.arc;
- while(last.next != null){
- last = last.next;
- }
- last.next = arc;
- }
- }
- Simpul cari(String c){
- Simpul hasil = null;
- Simpul node = first;
- boolean ketemu = false;
- while((node != null) && (ketemu == false)){
- if(node.info == c){
- hasil = node;
- ketemu = true;
- }
- else{
- node = node.next;
- }
- }
- return hasil;
- }
- void tampil(){
- Simpul node = first;
- if(node != null){
- while(node != null){
- System.out.println("asal : " + node.info);
- Jalur arc = node.arc;
- while(arc != null){
- System.out.println(" menuju ke : " + arc.node.info + " dengan teriakan : " + arc.info);
- arc = arc.next;
- }
- node = node.next;
- }
- }
- else{
- System.out.println("graph kosong");
- }
- }
- public static void main(String[] args) {
- Graph G = new Graph();
- G.createEmpty();
- G.insert("rumah");
- G.insert("timur");
- G.insert("utara");
- G.insert("barat");
- G.insert("barat daya");
- G.insert("rumah");
- Simpul begin = G.cari("rumah");
- Simpul end = G.cari("timur");
- if((begin != null) && (end != null)){
- G.addJalur(end, "YO YO YO YOngkoro yo", begin);
- }
- begin = G.cari("timur");
- end = G.cari("utara");
- if((begin != null) && (end != null)){
- G.addJalur(end,"YO YO YO YOngkoro yo", begin);
- }
- begin = G.cari("utara");
- end = G.cari("barat");
- if((begin != null) && (end != null)){
- G.addJalur(end,"YO YO YO YOngkoro yo", begin);
- }
- begin = G.cari("barat");
- end = G.cari("barat daya");
- if((begin != null) && (end != null)){
- G.addJalur(end,"YO YO YO YOngkoro yo", begin);
- }
- begin = G.cari("barat daya");
- end = G.cari("rumah");
- if((begin != null) && (end != null)){
- G.addJalur(end,"YO YO YO YOngkoro yo", begin);
- }
- System.out.println("================");
- G.tampil();
- System.out.println("\n================");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment