合并两个未排序的单链表

发布于 2024-10-26 17:25:25 字数 2781 浏览 1 评论 0原文

我编写了一个函数来合并两个未排序的单链表。我只需将第二个列表中的每个节点添加到原始列表的前面。它似乎有效,除了当我打印原始的、现在合并的列表时,新添加的元素是

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

主函数中的“null”,我调用该函数:

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 ; 因此我无法使用对“最后一个”节点的任何引用

注 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 行来自合并函数内的 print 语句

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'

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:

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

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. Hope that's ok?

[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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-11-02 17:25:25

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

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

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;
}
吃颗糖壮壮胆 2024-11-02 17:25:25

从您的片段来看,它看起来是正确的(但我会使用 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 ;
}

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

From your fragment it looks right (but I would use this.first = new SLLNode(elem, this.first) and ommit the next two statements). What did your mergeUnsorted method print?


Edit: I have no idea what is wrong, but obviously your addFirst method works, while directly adding does not work correctly. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文