2016-11-17 25 views
-2

在这个程序中,我做了一个通用的链表。插入和显示功能工作正常,但联合功能不工作。我实际上不确定我写的功能是正确的还是不是这样,我如何执行两个给定列表的联合。Java中的通用链接列表联盟

import java.util.*; 

class Node<T> { 
    T data; 
    Node<T> next; 

    Node(T d) { 
     data = d; 
    } 

    void display() { 
     System.out.println(data + " "); 
    } 
} 

class List<T> { 
    Node<T> first; 

    List() { 
     first = null; 
    } 

    void insert(T data) { 
     Node<T> newnode = new Node<T>(data); 
     if (first == null) 
      first = newnode; 
     else { 
      Node<T> temp = first; 
      while (temp.next != null) 
       temp = temp.next; 
      temp.next = newnode; 
     } 
    } 

    void display() { 
     if (first == null) 
      System.out.println("EmpTY"); 
     else { 
      Node<T> temp = first; 
      while (temp != null) { 
       temp.display(); 
       temp = temp.next; 
      } 
     } 
    } 

    public void union(Node<Double> head1, Node<Double> head2) { 
     Node<T> t = head1; 
     Node<T> t1 = head2; 
     while (t != null && t1 != null) { 
      if (t.data > t1.data) { 
       System.out.println(t.data + "\n"); 
       t = t.next; 
      } else if (t.data < t1.data) { 
       System.out.println(t1.data + "\n"); 
       t1 = t1.next; 
      } else { 
       System.out.println(t.data + "\n"); 
       System.out.println(t.data + "\n"); 
       t = t.next; 
       t1 = t1.next; 
      } 
     } 
    } 
} 

class DEMO { 
    public static void main(String args[]) { 
     List<Double> l1 = new List<Double>(); 
     for (double i = 0; i < 5; i++) { 
      l1.insert(i); 
     } 
     l1.display(); 
     List<Double> l2 = new List<Double>(); 
     List<Double> l3 = new List<Double>(); 
     for (double j = 0; j < 5; j++) { 
      l2.insert(j); 
     } 
     l3.union(l1, l2); 
    } 
} 
+0

您的意思是在另一末端附加列表?这很简单,只需在第一个列表的尾部和第二个列表的头部之间建立一个链接即可。 –

+0

“union”有不同的用途....您是否想要“加入”或“合并”列表? –

+0

为什么不只是把第一个列表中的最后一个节点,并使其下一个节点指向第二个列表的根?如果你想要它,只要做到这一点,然后排序整个事情? – user123

回答

-1

你可以做这样的事情:

/* Function to get Union of 2 Linked Lists */ 
void doUnion(Node head1, Node head2) { 
    Node t1 = head1, t2 = head2; 
    //insert all elements of list1 in the result 
    while (t1 != null) { 
     push(t1.data); 
     t1 = t1.next; 
    } 
    // insert those elements of list2 that are not present 
    while (t2 != null) { 
     if (!isPresent(head, t2.data)) { 
      push(t2.data); 
     } 
     t2 = t2.next; 
    } 
} 

/* Inserts a node at start of linked list */ 
void push(int new_data) { 
    /* 1. Allocate the Node & Put in the data*/ 
    Node new_node = new Node(new_data); 
    /* 2. Make next of new Node as head */ 
    new_node.next = head; 
    /* 3. Move the head to point to new Node */ 
    head = new_node; 
} 

/* A utilty function that returns true if data is present in linked list else return false */ 
boolean isPresent(Node head, int data) { 
    Node t = head; 
    while (t != null) { 
     if (t.data == data) { 
      return true; 
     } 
     t = t.next; 
    } 
    return false; 
}