合并2个链表

时间:2016-03-25 13:55:27

标签: java linked-list

我尝试将2个链接列表合并在一起,列表1和列表2。在list2中你有一个在list1中不可用的项目,它是键盘,鼠标的数量也已经改变,鼠标的新数量将是3,因为在list1中有1个鼠标数量而在list2中有3个所以基本上你会有一个名为list3的新链接列表。 list3将list1和list2的内容组合在一起,就像list2中的项目键盘而不是list1,以及新的鼠标数量。



import java.util.LinkedList;
import java.util.ListIterator;

public class TestLinkedList extends MyLinkedList {
    
    public static void main(String[] args) {

        LinkedList < String > list1 = new LinkedList < > ();
        Demo demo = new Demo();

        String case1 = demo.setNameandQty(1, "Case");
        String monitor1 = demo.setNameandQty(3, "Monitor");
        String mouse1 = demo.setNameandQty(1, "Mouse");
        String ram1 = demo.setNameandQty(2, "RAM");
        String ssd1 = demo.setNameandQty(4, "SSD");

        int cm = 2;

        list1.add(case1);
        list1.add(monitor1);
        list1.add(mouse1);
        list1.add(ram1);
        list1.add(ssd1);

        System.out.println("Shopping List 1");
        ListIterator < String > it1 = list1.listIterator();

        while (it1.hasNext()) {
            System.out.println(it1.next());
        }

        list1.remove(3);
        System.out.println("\nAfter deleting 4th item");
    
        for (String s: list1) {
            System.out.println(s);
        }

        LinkedList < String > list2 = new LinkedList < > ();
        String keyboard2 = demo.setNameandQty(1, "Keyboard");
        String mouse2 = demo.setNameandQty(2, "Mouse");

        list2.add(keyboard2);
        list2.add(mouse2);

        System.out.println("\nShopping list 2");
        for (String s: list2) {
            System.out.println(s);
        }

        String holdMouse1q = "";
        String holdMouse2q = "";

        String getMouse1q = demo.getIntegers(mouse1, holdMouse1q);
        int mouse1q = Integer.parseInt(getMouse1q);

        String getMouse2q = demo.getIntegers(mouse2, holdMouse2q);
        int mouse2q = Integer.parseInt(getMouse2q);

        System.out.println("\nMouse 1 quantity: " + mouse1q);
        System.out.println("Mouse 2 quantity: " + mouse2q);

        int totalMouseQ = mouse1q + mouse2q;

        // list1.addFirst("Flashdisks : 10");
        // System.out.println("\nAfter adding 10 flash disks");
        // for (String s : list1) {
        //     System.out.println(s);
        // }
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我不确定方法demo.setNameandQty()的返回值是什么。所以我假设返回值应该像<name>-<quality>,例如。 &#34;小鼠-2&#34;

下面是我的合并方法,通过排序项目列表来解决:

private static List<String> merge(List<String> list1, List<String> list2){
Collections.sort(list1);
Collections.sort(list2);
//ensure list always has 1 element
list1.add(null);
list2.add(null);

List<String> list3 = new LinkedList<String>();

Iterator<String> l1 = list1.iterator();
Iterator<String> l2 = list2.iterator();

String item1 = l1.next();
String item2 = l2.next();
while(item1 != null && item2 != null){
    String name1 = getName(item1);
    String name2 = getName(item2);
    if(name1.compareTo(name2) < 0){
        list3.add(item1);
        item1 = l1.next();
    }
    else if(name1.compareTo(name2) > 0){
        list3.add(item2);
        item2 = l2.next();
    }
    else if(name1.compareTo(name2) == 0){
        //sum up the quality then add to the list
        int totalQty = getQuality(item1) + getQuality(item2);
        list3.add(name1 + "-" + totalQty);
        item1 = l1.next();
        item2 = l2.next();
    }
}

//add the rest of the list, in case one of two lists still has items
while(item1 != null){
    list3.add(item1);
    item1 = l2.next();
}
while(item2 != null){
    list3.add(item2);
    item2 = l2.next();
}

return list3;
}    
private static String getName(String s){
    return s.substring(0, s.indexOf('-'));
}
private static int getQuality(String s){
    return Integer.valueOf(s.substring(s.indexOf('-')+1));
}

请注意,如果您在一个列表中有重复项,则此方法将失败,例如。 {&#34;小鼠-1&#34;&#34;小鼠-2&#34;}。为了解决这个问题,我认为我们更好地确保没有重复的项目,而不是让我们的合并方法更复杂。