简体   繁体   English

合并2个链表

[英]Merging 2 linked lists

I'm trying merge the 2 linked lists together, list1 and list2. 我正在尝试将2个链表合并在一起,即list1和list2。 In list2 you have an item that isn't available in list1 which is keyboard, also quantity of mouse has been changed, new quantity of mouse is going to be 3 because in list1 there was 1 mouse quantity and in list2 there are 3. So essentially you're going to have a new linked list called, let's say list3. 在list2中,您有一个在list1中不可用的项目,它是键盘,鼠标的数量也已更改,新的鼠标数量将为3,因为list1中的鼠标数量为1,list2中的鼠标数量为3。本质上,您将有一个名为链表3的新链表。 list3 will combine the contents of list1 and list2 toghether, like the item keyboard which was in list2 but not list1 and also the new quantity of mouse. 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); // } } } 

I'm not sure what the return value of method demo.setNameandQty() is. 我不确定demo.setNameandQty()方法的返回值是什么。 So I just assume the return value should be like <name>-<quality> , eg. 所以我只是假设返回值应该像<name>-<quality> "Mouse-2". “鼠标2”。

Below is my merge method, solve by sort the list of items: 下面是我的合并方法,通过对项目列表进行排序来解决:

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

One note here that this method would fail if you have duplicate item in one list, eg. 请注意,如果您在一个列表中有重复的项目,则此方法将失败,例如。 {"Mouse-1","Mouse-2"}. {“鼠标1”,“鼠标2”}。 To fix that, I think we better insure there is no duplicate item instead of making our merge method more complicated. 为了解决这个问题,我认为我们最好确保没有重复的项目,而不是使我们的合并方法更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM