Advertisement
kamasazi99

aiz lab5

Nov 11th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package tgraph;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. /**
  13. *
  14. * @author Hubert
  15. */
  16. public class LGraph extends AGraph{
  17.  
  18.  
  19. ArrayList<ArrayList<Integer> > lista;
  20. //
  21. public static void main(String[] args) {
  22. LGraph L=new LGraph(5);
  23. L.connect(1, 2);
  24. L.connect(4, 3);
  25. L.writeMatrix();
  26.  
  27. }
  28.  
  29.  
  30. public LGraph(int vertexCount) {
  31. super(vertexCount);
  32. lista = new ArrayList<ArrayList<Integer> >(size);
  33. for (int i = 0; i < size; i++)
  34. lista.add(new ArrayList<Integer>());
  35. }
  36.  
  37. @Override
  38. public void writeMatrix() {
  39.  
  40. for(int i=0;i<lista.size();i++){
  41. for(int j=0;j<lista.get(i).size();j++)
  42. {
  43.  
  44. System.out.print(lista.get(i).get(j)+" ");
  45. if(j==lista.get(i).size()-1)
  46. System.out.println();
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. }
  54.  
  55. @Override
  56. public boolean check(int i, int j) throws IllegalArgumentException {
  57. if(lista.get(i).contains(j))
  58. return true;
  59. else
  60. return false;
  61.  
  62. }
  63.  
  64. @Override
  65. public void connect(int i, int j) throws IllegalArgumentException {
  66. lista.get(i).add(j);
  67. lista.get(j).add(i);
  68.  
  69. }
  70.  
  71. @Override
  72. public void writeList() {
  73. for(int i=0;i<lista.size();i++){
  74. System.out.println("Wartosci dla "+i);
  75. for(int j=0;j<lista.get(i).size();j++)
  76. {
  77. System.out.println(lista.get(i).get(j)+" ");
  78. }
  79.  
  80. }
  81. }
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /*
  98. * To change this license header, choose License Headers in Project Properties.
  99. * To change this template file, choose Tools | Templates
  100. * and open the template in the editor.
  101. */
  102. package tgraph;
  103.  
  104. /**
  105. *
  106. * @author Hubert
  107. */
  108. public class TGraph extends AGraph {
  109.  
  110. int [] [] tab=new int [size][size];
  111. public static void main(String[] args) {
  112. TGraph t= new TGraph(5);
  113. t.connect(1, 4);
  114. t.connect(1, 3);
  115. t.writeMatrix();
  116. t.writeList();
  117. }
  118.  
  119. public TGraph(int vertexCount) {
  120. super(vertexCount);
  121. }
  122.  
  123. @Override
  124. public void writeMatrix() {
  125. for(int i=0;i<size;i++)
  126. for(int j=0;j<size;j++)
  127. {
  128. System.out.print(tab[i][j]);
  129. if(j==size-1)
  130. System.out.println();
  131. }
  132. }
  133.  
  134. @Override
  135. public boolean check(int i, int j) throws IllegalArgumentException {
  136. if(tab[i][j]==1)
  137. return true;
  138. else
  139. return false;
  140. }
  141.  
  142. @Override
  143. public void connect(int i, int j) throws IllegalArgumentException {
  144. tab[i][j]=1;
  145.  
  146. }
  147.  
  148. @Override
  149. public void writeList() {
  150. int pomoc_i=0, pomoc_j=0;
  151. for(int i=0;i<size;i++){
  152. pomoc_i=i;
  153. System.out.println(pomoc_i+": ");
  154. for(int j=0;j<size;j++)
  155. {
  156. pomoc_j=j;
  157. if(tab[i][j]!=0)
  158. System.out.print(" "+j);
  159. }
  160. System.out.println();
  161. }
  162.  
  163.  
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement