Java中的通用链表的联合

时间:2016-11-17 20:20:49

标签: java linked-list

在这个程序中,我创建了一个通用链表.Insert和Display函数工作正常但是union函数没有工作。我实际上不确定我写的函数是否正确,我该如何执行两个给定清单的联合。

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);
    }
}

1 个答案:

答案 0 :(得分:-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;
}