Hi there.
I'm having trouble with a final method which I'm trying to implement. I am using the following interface:
public interface ILinkedList {
public abstract Object getHead();
public abstract ILinkedList getTail();
public abstract void setHead(Object head);
public abstract void setTail(ILinkedList tail);
}
And have the following two classes:
public class LinkedList implements ILinkedList {
private Object head;
private ILinkedList tail;
public Object getHead() {
return head;
}
public ILinkedList getTail() {
return tail;
}
public void setHead(Object head) {
this.head = head;
}
public void setTail(ILinkedList tail) {
this.tail = tail;
}
public LinkedList(Object head, ILinkedList tail) {
this.head = head;
this.tail = tail;
}
}
and
public class ListManipulation {
/*
* Return a string representing the list as a comma-separated list of values
* enclosed in [...] . Use the toString() method of the individual list
* entries to print the values.
*
* An example of an iterative method. The complexity of this method comes
* from the fact that a comma-separated list of n objects has n-1 commas
* unless n = 0. So there needs to be a special case.
*/
public static String convertToString(ILinkedList l) {
/*
* build up the result in a StringBuffer rather than a String for
* efficiency
*/
StringBuffer s = new StringBuffer("[");
if (l != null) {
/*
* deal with the first element specially, as there is no comma
* before it
*/
s.append(l.getHead().toString());
l = l.getTail();
/*
* Now things are regular and we go into a loop to run down the rest
* of the LL printing each item preceded by a comma
*/
while (l != null) {
s.append(", " + l.getHead().toString());
l = l.getTail();
}
}
/* Finally close the bracket and convert to a String */
s.append("]");
return s.toString();
}
/*
* return a new list consisting of the elements of l1, followed by those of
* l2. The result may share nodes with l2 if this is useful
*
* implement recursively
*/
public static ILinkedList append(ILinkedList l1, ILinkedList l2) {
while (l2 != null) {
l1.setTail(l2);
l2 = l2.getTail();
}
// l2 is null and so new list is just l1
return l1;
}
}
It is the
append method which I'm unsure of. I understand that if I have two linked lists (written in shorthand as) [1,2,3] & [4,5], that I want to take the head of the second list (4) and append it to the tail of the first list (2,3) - I have to implement this recursively. What I have at the moment in the append method, I believe will make the first list [1,4], as I think it will replace the current tail with the head of the second list.
Does anybody have any suggestions as to how I would otherwise implement this to append the second list onto the first? I tried l1.setTail(l2.getHead()); but the interface doesn't allow this (and the interface cannot be changed).
Thank you to anybody who is able to suggest anything :)
Christopher
edit: i've taken out some extraneous code that I realised would just be cluttering up the place, and might be putting off people helping. I hope someone is able to offer some advice :)
I have also posted the same question at another help forum, to see if someone there is able to help. If they do, I shall let you know so that the same things aren't being said more than once.
http://www.java-forums.org/new-java/13550-linked-list-manipulation.html#post44543
Edited by: chrisdb89 on Nov 21, 2008 8:41 AM