简体   繁体   English

合并两个未排序的单链接列表

[英]merging two unsorted singly-linked lists

I wrote a function that merges two unsorted singly-linked lists. 我编写了一个合并两个未排序的单链接列表的函数。 I simply add each node from the second list to the front of the original list. 我只是将第二个列表中的每个节点添加到原始列表的前面。 It seems to work except that when i print the original, now merged list, the newly added elements are 'null' 看来可行,除了当我打印原始的,现在合并的列表时,新添加的元素为“ null”

public SLL mergeUnsorted(SLL otherList)
{
    Iterator itr = otherList.iterator() ;
    while (itr.hasNext())
    {
        Object elem  = itr.next() ;
        System.out.println(elem) ; // to make sure the elements are retrieved correctly
        SLLNode ins = new SLLNode(elem, null) ; // make a node out of the element 
        ins.succ = this.first ; // insert the element to the front of the original list
        this.first = ins ;
    }
    return this ;
}

from main i call the function: 从主要我调用函数:

myList = myList.mergeUnsorted(otherList) ;
printIt(myList) ;

output: 输出:

null null null null Hi Hello Salut Ciao

SLLNode contructor: SLLNode建设者:

public SLLNode(Object ObjElem, SLLNode succ)
{
    this.ObjElem = ObjElem ;
    this.succ = succ ;
}

[EDIT] [编辑]

class SLL
{
    SLLNode first ;

    public SLL()
    {
        first = null ;
    }
...

Note1: The exercise states that the SLL class data representation only includes a first node private SLLNode first ; 注意1:练习指出SLL类数据表示仅包括第一个private SLLNode first ;节点private SLLNode first ; hence i cannot use any reference to 'last' node 因此,我无法使用对“最后一个”节点的任何引用

Note2: The exercise contains a method that i most probably will need to use but i can't see how. 注意2:练习包含一种我很可能需要使用的方法,但是我看不到如何使用。

private SLLNode node(int i)
{
    SLLNode curr = first ;
    for(int j=0; j<i; j++){
        curr = curr.succ ;
        }
    return curr ;
}

Note3: i could add the iterator implementation code here but given that i can print the list using the same Iterator it seems all correct so i'd rather not clutter this post too much. 注意3:我可以在此处添加迭代器实现代码,但鉴于我可以使用相同的迭代器打印列表,这似乎都是正确的,因此我宁愿不要使这篇文章过于混乱。 Hope that's ok? 希望没事吗?

[EDIT2] [EDIT2]

public static void main(String[] args)
{
    SLL myList = new SLL() ;
    SLL otherList = new SLL() ;  
    SLLNode a = new SLLNode("xx", null) ;
    SLLNode b = new SLLNode("yy", null) ;
    SLLNode c = new SLLNode("ww", null) ;
    SLLNode d = new SLLNode("aa", null) ;
    SLLNode e = new SLLNode("rr", null) ;

    otherList.addFirst(a) ;
    printIt(otherList) ;
    otherList.addFirst(b) ;
    printIt(otherList) ;
    otherList.addFirst(c) ;
    printIt(otherList) ;
    otherList.addFirst(d) ;
    printIt(otherList) ;

    SLLNode A = new SLLNode("Hello", null) ;
    SLLNode B = new SLLNode("Hi", null) ;
    SLLNode C = new SLLNode("Salut", null) ;
    SLLNode D = new SLLNode("Ciao", null) ;
    SLLNode E = new SLLNode("Moin", null) ;

    myList.addFirst(A) ;
    printIt(myList) ;
    myList.addFirst(B) ;
    printIt(myList) ;
    myList.addFirst(C) ;
    printIt(myList) ;
    myList.addFirst(D) ;
    printIt(myList) ;

    myList = myList.mergeUnsorted(otherList) ;
    printIt(myList) ;
}

[EDIT3] @Paulo, complete output as generated by main included in Edit2 [EDIT3] @Paulo,由Edit2中包含的main生成的完整输出

xx
yy xx
ww yy xx
aa ww yy xx
Hello
Hi Hello
Salut Hi Hello
Ciao Salut Hi Hello
aa
ww
yy
xx
null null null null Ciao Salut Hi Hello

note that line 9-12 are from the print statement inside the merge function 请注意,第9-12行来自merge函数中的print语句

From your fragment it looks right (but I would use this.first = new SLLNode(elem, this.first) and ommit the next two statements). 从您的片段来看,它看起来this.first = new SLLNode(elem, this.first) (但是我将使用this.first = new SLLNode(elem, this.first)并省略接下来的两个语句)。 What did your mergeUnsorted method print? 您的mergeUnsorted方法打印了什么?


Edit: I have no idea what is wrong, but obviously your addFirst method works, while directly adding does not work correctly. 编辑:我不知道这是什么问题,但显然您的addFirst方法有效,而直接添加则无法正常工作。 So use this: 所以使用这个:

public SLL mergeUnsorted(SLL otherList)
{
    Iterator itr = otherList.iterator() ;
    while (itr.hasNext())
    {
        Object elem  = itr.next() ;
        System.out.println(elem) ; // to make sure the elements are retrieved correctly
        SLLNode ins = new SLLNode(elem, null) ; // make a node out of the element 
        this.addFirst(ins);
    }
    return this ;
}

Of course, this will add the elements in reverse order, but the same is happening now, too (it just does not work). 当然,这将以相反的顺序添加元素,但是现在也发生了同样的事情(只是不起作用)。

why don't you change implementation so succesor of your last node in the first list points to first node in the other list. 您为什么不更改实现,因此第一个列表中最后一个节点的后继指向另一个列表中的第一个节点。

public SLL mergeUnsorted(SLL otherList)
{
    if (otherList != null && otherList.first != null) {
        SLLNode last = null;
        Iterator itr = iterator() ;
        for (itr.hasNext())
        {
            Object elem  = itr.next() ;
            if (elem.succ == null) {
                last = elem;
            }
        }
        if (last != null) {
            last.succ = otherList.first;
        }
    }    
    return this;
}

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

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