Results 1 to 4 of 4
Thread: Linked List Manipulation
- 11-21-2008, 05:41 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
Linked List Manipulation
I have already posted this at forums.sun.com, however as of yet nobody has been able to help me. If someone replies, I shall let you know so that the same things aren't being said more than once!
I'm having trouble with a final method which I'm trying to implement. I am using the following interface:
Java Code: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:
andJava Code: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; } }
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.Java Code: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; } }
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
- 11-21-2008, 05:56 PM #2
I think you may be over complicating this(or I'm way under complicating it). Why don't you just the replace method of StringBuffer to replace "]" with ", "+l2.getHead().toString() and when l2.getTail() == null s.append("]")
Like I said I may be way over simplifying this and this might not even be possible but it's an easy solution that logically would work.
- 11-21-2008, 05:59 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
Thank you for replying.
I was given a list of methods which I had to implement, and so from that I think that l2 must only come into the equation when dealing with the append method. I am also unable to change the interface, or the LinkedList Class, as these were supplied to us.
- 11-21-2008, 06:07 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Linked List removeAll Help
By unc123w in forum New To JavaReplies: 13Last Post: 09-30-2008, 02:41 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM -
going from vectors to linked list?
By cbrown08 in forum New To JavaReplies: 3Last Post: 12-01-2007, 12:55 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks