Results 1 to 4 of 4
  1. #1
    chrisdb89 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default 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:

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

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

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  3. #3
    chrisdb89 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    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.
    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.

  4. #4
    chrisdb89 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    I received help at the other forum, which solved my problem.

    Thank you for your help, though :)

    Christopher

Similar Threads

  1. Linked List removeAll Help
    By unc123w in forum New To Java
    Replies: 13
    Last Post: 09-30-2008, 03:41 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM
  3. going from vectors to linked list?
    By cbrown08 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 01:55 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •