繁体   English   中英

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

[英]merging two unsorted singly-linked lists

我编写了一个合并两个未排序的单链接列表的函数。 我只是将第二个列表中的每个节点添加到原始列表的前面。 看来可行,除了当我打印原始的,现在合并的列表时,新添加的元素为“ 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 ;
}

从主要我调用函数:

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

输出:

null null null null Hi Hello Salut Ciao

SLLNode建设者:

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

[编辑]

class SLL
{
    SLLNode first ;

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

注意1:练习指出SLL类数据表示仅包括第一个private SLLNode first ;节点private SLLNode first ; 因此,我无法使用对“最后一个”节点的任何引用

注意2:练习包含一种我很可能需要使用的方法,但是我看不到如何使用。

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

注意3:我可以在此处添加迭代器实现代码,但鉴于我可以使用相同的迭代器打印列表,这似乎都是正确的,因此我宁愿不要使这篇文章过于混乱。 希望没事吗?

[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,由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

请注意,第9-12行来自merge函数中的print语句

从您的片段来看,它看起来this.first = new SLLNode(elem, this.first) (但是我将使用this.first = new SLLNode(elem, this.first)并省略接下来的两个语句)。 您的mergeUnsorted方法打印了什么?


编辑:我不知道这是什么问题,但显然您的addFirst方法有效,而直接添加则无法正常工作。 所以使用这个:

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

当然,这将以相反的顺序添加元素,但是现在也发生了同样的事情(只是不起作用)。

您为什么不更改实现,因此第一个列表中最后一个节点的后继指向另一个列表中的第一个节点。

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