簡體   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