示例#1
0
  public static void main(String args[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    LinkedList6 l1 = new LinkedList6();

    System.out.println("populate linkedlist1 with integers");
    int i = 0;
    while (true) {
      i++;
      System.out.println("enter integer value of node" + i + " or enter -1 to exit");
      int num = sc.nextInt();
      if (num == -1) break;
      l1.insert(num);
    }
    LinkedList6 l2 = new LinkedList6();

    System.out.println("populate linkedlist2 with integers");
    i = 0;
    while (true) {
      i++;
      System.out.println("enter integer value of node" + i + " or enter -1 to exit");
      int num = sc.nextInt();
      if (num == -1) break;
      l2.insert(num);
    }
    System.out.println("when L1 and L2 do not intersect:");
    l1.display();
    l2.display();
    if (l1.isIntersecting(l2)) System.out.println("L1 and L2 intersect!");
    else System.out.println("L1 and L2 do not intersect!");
    // creating an intersection to check whether the isIntersecting() function works.
    Random r = new Random();
    // appending l2 to l1 randomly
    int ref = r.nextInt(l1.numNodes() + 1);
    Node6 reference = l1.getReference(ref);
    l2.makeIntersect(
        reference); // l2 is appended to l1 at the reference node returned by getReference()
    l2.display();
    if (l1.isIntersecting(l2)) System.out.println("L1 and L2 intersect!");
    else System.out.println("L1 and L2 do not intersect!");
  }