saban123

PROGRAM GRAPH

Dec 10th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. class Simpul{`
  2.     String info;
  3.     Simpul next;
  4.     Jalur arc;
  5.     Simpul(){
  6.     }
  7. }
  8. class Jalur{
  9.     String info;
  10.     Jalur next;
  11.     Simpul node;
  12.     Jalur(){
  13.     }
  14. }
  15.  
  16. class Graph{
  17.     Simpul first;
  18.     Graph(){
  19.     }
  20.  
  21.     void createEmpty(){
  22.     first = null;
  23.     }
  24.  
  25.     void insert(String c){
  26.         Simpul baru;
  27.         baru = new Simpul();
  28.         baru.info = c;
  29.         baru.next = null;
  30.         baru.arc = null;
  31.         if(first == null){
  32.        
  33.             first = baru;
  34.         }
  35.         else{
  36.        
  37.             Simpul last = first;
  38.             while(last.next != null){
  39.                 last = last.next;
  40.                 }
  41.             last.next = baru;
  42.             }
  43.     }
  44.  
  45.     void addJalur(Simpul tujuan, String beban, Simpul awal){
  46.         Jalur arc;
  47.         arc = new Jalur();
  48.         arc.info = beban;
  49.         arc.next = null;
  50.         arc.node = tujuan;
  51.         if(awal.arc == null){
  52.        
  53.             awal.arc = arc;
  54.         }
  55.         else{
  56.            
  57.             Jalur last = awal.arc;
  58.             while(last.next != null){
  59.             last = last.next;
  60.             }
  61.             last.next = arc;
  62.         }
  63.     }
  64.     Simpul cari(String c){
  65.         Simpul hasil = null;
  66.         Simpul node = first;
  67.         boolean ketemu = false;
  68.         while((node != null) && (ketemu == false)){
  69.             if(node.info == c){
  70.                 hasil = node;
  71.                 ketemu = true;
  72.             }
  73.             else{
  74.                 node = node.next;
  75.             }
  76.         }
  77.         return hasil;
  78.     }
  79.  
  80.     void tampil(){
  81.         Simpul node = first;
  82.         if(node != null){
  83.             while(node != null){
  84.         System.out.println("asal : " + node.info);
  85.                 Jalur arc = node.arc;
  86.                 while(arc != null){
  87.                     System.out.println(" menuju ke  : " + arc.node.info + " dengan teriakan : " + arc.info);
  88.                     arc = arc.next;
  89.                 }
  90.                 node = node.next;
  91.             }
  92.         }
  93.         else{
  94.             System.out.println("graph kosong");
  95.         }
  96.     }
  97.     public static void main(String[] args) {
  98.         Graph G = new Graph();
  99.         G.createEmpty();
  100.         G.insert("rumah");
  101.         G.insert("timur");
  102.         G.insert("utara");
  103.         G.insert("barat");
  104.         G.insert("barat daya");
  105.         G.insert("rumah");
  106.        
  107.         Simpul begin = G.cari("rumah");
  108.         Simpul end = G.cari("timur");
  109.        
  110.         if((begin != null) && (end != null)){
  111.         G.addJalur(end, "YO YO YO YOngkoro yo", begin);
  112.         }
  113.        
  114.         begin = G.cari("timur");
  115.         end = G.cari("utara");
  116.        
  117.         if((begin != null) && (end != null)){
  118.         G.addJalur(end,"YO YO YO YOngkoro yo", begin);
  119.         }
  120.         begin = G.cari("utara");
  121.         end = G.cari("barat");
  122.        
  123.         if((begin != null) && (end != null)){
  124.         G.addJalur(end,"YO YO YO YOngkoro yo", begin);
  125.         }
  126.         begin = G.cari("barat");
  127.         end = G.cari("barat daya");
  128.        
  129.         if((begin != null) && (end != null)){
  130.         G.addJalur(end,"YO YO YO YOngkoro yo", begin);
  131.         }
  132.        
  133.         begin = G.cari("barat daya");
  134.         end = G.cari("rumah");
  135.        
  136.         if((begin != null) && (end != null)){
  137.         G.addJalur(end,"YO YO YO YOngkoro yo", begin);
  138.         }      
  139.         System.out.println("================");
  140.         G.tampil();
  141.         System.out.println("\n================");
  142.        
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment