views:

33

answers:

1

I'm creating a doubly-linked list in Objective-C. I would like my node class to include an instance method that allows me to add another node after the target node, like so:

// start <-> nodeA <-> nodeB <-> nodeC <-> end

Node * newNode = [Node node];
[NodeB thisMethodNeedsAGoodName:newNode];

// start <-> nodeA <-> nodeB <-> newNode <-> nodeC <-> end

What would be a good name for this method? insertAfter: sounds wrong, since that implies that the target is being inserted, not the new node. insertNode: would be OK, except that I'd like to write another method to insert a node before the target node, and then I'd have to come up with a different name for that method.

Any ideas?

+4  A: 

I'd probably call it appendNode:, which would allow you to do an "insert before" method called prependNode:. Then you just need to make sure it's clear that:

[aNode appendNode:newNode];

Is equivalent to:

[[aNode nextNode] prependNode:newNode];

(And if this is just a regular doubly-linked list, I'd recommend checking out CHDataStructures. Quinn Taylor has done an excellent job of building a comprehensive framework of common data structures in Cocoa.)

Dave DeLong
e.James