Question

Is there something similar to a circular linked list available in Cocoa?

I know that an NSArray is ordered - but I don't think I can use 'nextItem' or 'previousItem' - correct? Furthermore, I need the nextItem of the last item to be the first item.

I could add my own nextItem and previousItem methods, but I'm surprised if Apple haven't implemented something suitable already. I can't find it though if they have.

Was it helpful?

Solution

While you can certainly use a category to add the behavior to NSArray (as @darren suggests), it's possible that you might actually need a true circular buffer. If that's the case, check out the CHDataStructures framework. Besides a CHCircularBufferStack, there's also a CHCircularBufferQueue and a CHCircularBufferDeque.

OTHER TIPS

I'm not aware of any such circular list data structure. Your idea about implementing it manually seems like a good idea. I would use a category:

@implementation NSArray (myCircularList)

-(id)nextItem;
-(id)previousItem;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top