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 开发者_StackOverflowlist. 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
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;
}
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).
精彩评论