开发者

Circular Linked List in Cocoa

开发者 https://www.devze.com 2022-12-16 20:20 出处:网络
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 nee

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 alread开发者_StackOverflowy. I can't find it though if they have.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消