9 Replies - 862 Views - Last Post: 10 December 2013 - 04:37 AM Rate Topic: -----

#1 blindchicken11   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 93
  • Joined: 19-September 12

Remove a node in linkedlist AFTER specified

Posted 09 December 2013 - 02:41 PM

I'm trying to make a linkedlist method that removes the node AFTER the one specified by the parameter.
I know how to remove the one in the parameter like this:
private AnyType remove( Node<AnyType> p )

    {

        p.next.prev = p.prev;

        p.prev.next = p.next;

        theSize--;

        modCount++;

       

        return p.data;

    } 


Since I'm doing the one after, it would be p.next.next.prev = p.next.prev and then p.prev.next.next = p.next.next no?

Is This A Good Question/Topic? 0
  • +

Replies To: Remove a node in linkedlist AFTER specified

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3578
  • View blog
  • Posts: 16,342
  • Joined: 20-September 08

Re: Remove a node in linkedlist AFTER specified

Posted 09 December 2013 - 03:41 PM

You probably need something like

        // Find the matching node ('found')
        if (found.next == null) {
            // Last node - nothing to do
        } else {
            Node nextNode = found.next;

            if (nextNode.next == null) {
                // Easy - next node is last - abandon it
                found.next = null;
            } else {
                found.next = nextNode.next;
                nextNode = null;
            }
        }


This post has been edited by g00se: 09 December 2013 - 03:47 PM
Reason for edit:: indent

Was This Post Helpful? 1
  • +
  • -

#3 blindchicken11   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 93
  • Joined: 19-September 12

Re: Remove a node in linkedlist AFTER specified

Posted 09 December 2013 - 04:50 PM

Yup tested it out and that seems to work fine. I was going about it the wrong way. Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,015
  • Joined: 11-December 07

Re: Remove a node in linkedlist AFTER specified

Posted 09 December 2013 - 05:41 PM

If your remove method above works properly then removeNext() could be a s simple as this:

public AnyData removeNext(Node<AnyType> p) {
    return remove(p.next);
}


Quote

if (found.next == null) {
    // Last node - nothing to do
}


Code that fails silently causes subtle bugs that are difficult to find. Better to throw an exception if the client tries to remove a node beyond the end of the list. In this case, the code above throws a NullPointerException. Maybe a better one could be chosen but I don't think it's worth the effort. The developer will know something is wrong anyway.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3578
  • View blog
  • Posts: 16,342
  • Joined: 20-September 08

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 02:41 AM

Quote

return remove(p.next);
That assumes p.next is HEAD, which is not necessarily the case
Was This Post Helpful? 0
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,015
  • Joined: 11-December 07

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 03:06 AM

I don't understand what you mean by HEAD.

In his original post, it's clear that the node class has a next field which is the one after this one. Since that's the one he wants to remove, just call remove with that one. If it doesn't exist, you will get a NullPointerException.
Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3578
  • View blog
  • Posts: 16,342
  • Joined: 20-September 08

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 03:17 AM

Quote

Since that's the one he wants to remove, just call remove with that one.
And what about the node(s) after the one that should be removed, where it/they exist?
Was This Post Helpful? 0
  • +
  • -

#8 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,015
  • Joined: 11-December 07

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 04:02 AM

Then the remove() method should deal with them.

I think we are talking at cross purposes. This is simple stuff that we both know inside out. Here is how I think the two methods should work. I've not tried any unit tests but can you spot any problems?

public T removeNext(Node<T> n) {
	return remove(n.next);
}

public T remove(Node<T> n) {
	if (n.next != null) {
		n.next.previous = n.previous;
	}
	if (n.previous != null) {
		n.previous.next = n.next;
	}
	return n.value;
}

This post has been edited by cfoley: 10 December 2013 - 04:12 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3578
  • View blog
  • Posts: 16,342
  • Joined: 20-September 08

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 04:32 AM

Yes sorry, that's fine. I don't think i read the original closely enough - not even noticing it was doubly-linked - hence the amount of code :">
Was This Post Helpful? 0
  • +
  • -

#10 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,015
  • Joined: 11-December 07

Re: Remove a node in linkedlist AFTER specified

Posted 10 December 2013 - 04:37 AM

Haha. No worries.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1