uopspop

Untitled

Sep 19th, 2019
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. public class GraphAdjacentList {
  4.  
  5.     private Node listMatrix[];
  6.     private int numVertices;
  7.  
  8.     class Node {
  9.         public Node next;
  10.         public int vertice;
  11.         public Node(int v){
  12.             this.vertice = v;
  13.         }
  14.     }
  15.  
  16.     public GraphAdjacentList(int numVertices) {
  17.         this.numVertices = numVertices;
  18.         listMatrix = new Node[numVertices];
  19.         for (int i = 0 ; i < numVertices; i++) {
  20.             this.listMatrix[i] = new Node(i);
  21.         }
  22.     }
  23.  
  24.     public void setEdge(int i, int j) {
  25.         Node lastNode = null;
  26.         lastNode = listMatrix[i];
  27.         while (lastNode.next != null) {
  28.             lastNode = lastNode.next;
  29.         }
  30.         lastNode.next = new Node(j);
  31.  
  32.         lastNode = listMatrix[j];
  33.         while (lastNode.next != null) {
  34.             lastNode = lastNode.next;
  35.         }
  36.         lastNode.next = new Node(i);
  37.     }
  38.  
  39.     public void delEdge(int i, int j) {
  40.         Node currNode = null;
  41.         currNode = listMatrix[i];
  42.         while (true) {
  43.             if (currNode.next.vertice == j) break;
  44.             if (currNode == null) break;
  45.             currNode = currNode.next;
  46.         }
  47.         if (currNode != null) {
  48.             currNode.next = currNode.next.next;
  49.         }
  50.  
  51.         currNode = listMatrix[j];
  52.         while (true) {
  53.             if (currNode.next.vertice == i) break;
  54.             if (currNode == null) break;
  55.             currNode = currNode.next;
  56.         }
  57.         if (currNode != null) {
  58.             currNode.next = currNode.next.next;
  59.         }
  60.     }
  61.  
  62.     public boolean isEdge(int i, int j) {
  63.         Node currNode = null;
  64.         currNode = listMatrix[i];
  65.         while (true) {
  66.             if (currNode == null) break;
  67.             if (currNode.vertice == j) return true;
  68.             currNode = currNode.next;
  69.         }
  70.         return false;
  71.     }
  72.  
  73.     public int first(int v1){
  74.         Node currNode = listMatrix[v1];
  75.         int firstNeighborIndex = this.numVertices; // return n if none
  76.         if (currNode.next != null){
  77.             return currNode.next.vertice;
  78.         }
  79.         return firstNeighborIndex;
  80.     }
  81.  
  82.     public int next(int v1, int v2){
  83.         int firstNeighborIndex = this.numVertices; // return n if none
  84.         Node currNode = listMatrix[v1];
  85.         while (true) {
  86.             if (currNode == null) break;
  87.             if (currNode.vertice == v2 && currNode.next != null) return currNode.next.vertice;
  88.             currNode = currNode.next;
  89.         }
  90.         return firstNeighborIndex;
  91.     }
  92.  
  93.     public static void main(String[] args) {
  94.         GraphAdjacentList gam = new GraphAdjacentList(5);
  95.         boolean isEdge;
  96.         gam.setEdge(0, 1);
  97.         isEdge = gam.isEdge(0, 1);
  98.         gam.delEdge(0, 1);
  99.         isEdge = gam.isEdge(0, 1);
  100.  
  101.         gam.setEdge(0, 1);
  102.         gam.setEdge(0, 2);
  103.         gam.setEdge(0, 4);
  104.  
  105.         int nextNeighbor;
  106.         nextNeighbor = gam.first(0);
  107.         nextNeighbor = gam.next(0, nextNeighbor);
  108.         nextNeighbor = gam.next(0, nextNeighbor);
  109.         nextNeighbor = gam.next(0, nextNeighbor);
  110.  
  111.         System.out.print(gam.toString());
  112.         System.out.println();
  113.     }
  114.  
  115. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×