-
October 5th, 2012, 04:07 AM
#1
Double Linked Structure
Hello, this code right here needs to take advantage of a doubly linked structure, currently its set up to
a single one, it needs to be without the previous variable and I suspect it should somehow use this .
for exmaple the add method taking advantage I believe looks somewhat like this (although I haven't tested it)
public void add (T element)
{
if (!(contains(element)))
{
DoubleNode<T> node = new DoubleNode<T> (element);
node.setNext(contents);
contents = node;
contents.setPrevious(contents.getPrevious());
count++;
}
}
Now the remove method canot have the previous declaration, im assuming it needs to work with a line like the add
does like this " contents.setPrevious(contents.getPrevious());" Thanks.
public T remove (T target) throws EmptySetException,
NoSuchElementException
{
boolean found = false;
DoubleNode<T> previous, current;
T result = null;
if (isEmpty())
throw new EmptySetException();
if (contents.getElement().equals(target))
{
result = contents.getElement();
contents = contents.getNext();
}
else
{
previous = contents;
current = contents.getNext();
for (int look=0; look < count && !found; look++)
if (current.getElement().equals(target))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new NoSuchElementException();
result = current.getElement();
previous.setNext(current.getNext());
}
count--;
return result;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|