Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. package prog3;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. class Node {
  7.     public int data;
  8.     public Node next;
  9.  
  10.     // class can't be compiled without constructor, empty constructor
  11.     Node() {
  12.     }
  13.  
  14.     // Function to set value
  15.     void set(int x) {
  16.         data = x;
  17.     }
  18. }
  19.  
  20. class List {
  21.     public Node first, last;
  22.     public List next;
  23.  
  24.     List() {
  25.     }
  26.  
  27.     void add(int data) {
  28.         if (first != null) {
  29.             Node search;
  30.             search = first;
  31.             while (search.next != null) {
  32.                 search = search.next;
  33.             }
  34.             last = new Node();
  35.             last.set(data);
  36.             last.next = null;
  37.             search.next = last;
  38.         } else {
  39.             first = new Node();
  40.             first.set(data);
  41.             first.next = null;
  42.         }
  43.     }
  44.  
  45.     void afisare() {
  46.         Node parcurgere = first;
  47.         while (parcurgere.next != null) {
  48.             System.out.print(parcurgere.data + " ");
  49.             parcurgere = parcurgere.next;
  50.         }
  51.         System.out.print(parcurgere.data + " ");
  52.     }
  53. }
  54.  
  55. class Que {
  56.     List coada;
  57.  
  58.     Que() {
  59.         coada = new List();
  60.         coada.first = new Node();
  61.         coada.first = null;
  62.         coada.last = new Node();
  63.         coada.last = null;
  64.     }
  65.  
  66.     void add(int x) {
  67.         Node dummy;
  68.         if (coada.first == null) {
  69.             coada.first = new Node();
  70.             coada.first.set(x);
  71.             coada.first.next = null;
  72.             coada.last = coada.first;
  73.         } else {
  74.             dummy = new Node();
  75.             dummy.set(x);
  76.             dummy.next = null;
  77.             coada.last.next = dummy;
  78.             coada.last = dummy;
  79.         }
  80.     }
  81.  
  82.     int remove() {
  83.         Node dummy;
  84.         dummy = coada.first;
  85.         while (dummy.next != coada.last) {
  86.             dummy = dummy.next;
  87.         }
  88.         dummy.next = null;
  89.         int val = coada.last.data;
  90.         System.out.print("!");
  91.         coada.last = dummy;
  92.         return val;
  93.     }
  94.  
  95.     boolean isEmpty() {
  96.         if (coada.first == null)
  97.             return true;
  98.         else
  99.             return false;
  100.     }
  101. }
  102.  
  103. class Graf {
  104.     List lfirst, llast;
  105.     int varf;
  106.     int vec[];
  107.     Que queue;
  108.  
  109.     Graf(int n) {
  110.         varf = n - 1;
  111.         List dummy;
  112.         lfirst = null;
  113.         vec = new int[n - 1];
  114.         int i = 0;
  115.         while (i < n) {
  116.             if (lfirst == null) {
  117.                 lfirst = new List();
  118.                 lfirst.next = null;
  119.                 lfirst.add(i);
  120.                 llast = lfirst;
  121.                 i++;
  122.             } else {
  123.                 dummy = new List();
  124.                 dummy.add(i);
  125.                 llast.next = dummy;
  126.                 llast = dummy;
  127.                 llast.next = null;
  128.                 i++;
  129.             }
  130.         }
  131.         for (i = 0; i < n - 1; i++) {
  132.             vec[i] = 0;
  133.         }
  134.     }
  135.  
  136.     int conected(int a, int b) {
  137.         List dummy;
  138.         Node finder;
  139.         dummy = lfirst;
  140.         while (dummy != null) {
  141.             if (dummy.first.data == a) {
  142.                 finder = dummy.first;
  143.                 while (finder != null) {
  144.                     if (finder.data == b) {
  145.                         return 1;
  146.                     }
  147.                     finder = finder.next;
  148.                 }
  149.             }
  150.             dummy = dummy.next;
  151.         }
  152.         return 0;
  153.     }
  154.  
  155.     void parcurgere() {
  156.         queue = new Que();
  157.         int tag = 0;
  158.         vec[tag] = 1;
  159.         queue.add(0);
  160.         List dummy;
  161.         Node caller;
  162.         while (!queue.isEmpty()) {
  163.             tag = queue.remove();
  164.             System.out.print(">" + tag + " ");
  165.             dummy = this.lfirst;
  166.             while (tag != dummy.first.data) {
  167.                 dummy = dummy.next;
  168.                 if (dummy.next == null)
  169.                     return;
  170.             }
  171.             caller = dummy.first;
  172.             while (caller != null) {
  173.                 if (vec[caller.data] == 0) {
  174.                     queue.add(caller.data);
  175.                     vec[caller.data] = 1;
  176.                 }
  177.                 caller = caller.next;
  178.             }
  179.         }
  180.     }
  181.  
  182.     void add(int a, int b) {
  183.         List carry;
  184.         carry = lfirst;
  185.         while (carry.first.data != a) {
  186.             carry = carry.next;
  187.         }
  188.         carry.add(b);
  189.     }
  190.  
  191.     void afisare() {
  192.         List carry;
  193.         carry = lfirst;
  194.         while (carry.next != null) {
  195.             System.out.println();
  196.             carry.afisare();
  197.             carry = carry.next;
  198.         }
  199.     }
  200. }
  201.  
  202. public class Lab3 {
  203.  
  204.     public static void main(String e[]) {
  205.         Graf listaGraf;
  206.         Scanner reader = null;
  207.         // try to read file if file isn't created throws exception
  208.         try {
  209.             reader = new Scanner(new java.io.File("input.txt"));
  210.             int n = reader.nextInt();
  211.             listaGraf = new Graf(n);
  212.             while (reader.hasNextInt()) {
  213.                 int a = reader.nextInt();
  214.                 int b = reader.nextInt();
  215.                 listaGraf.add(a, b);
  216.                 listaGraf.add(b, a);
  217.             }
  218.             listaGraf.afisare();
  219.             System.out.println();
  220.             listaGraf.parcurgere();
  221.             System.out.println();
  222.         } catch (FileNotFoundException exception) {
  223.             System.out.print("Nu exista fisierul");
  224.         }
  225.         // try over, exception or not
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement