用双向链表实现Object-c 数组结构
@interface MyDataArrayLinkNode : NSObject
{
@package
id _data;
MyDataArrayLinkNode *_prev;
MyDataArrayLinkNode *_next;
}
@end
@implementation MyDataArrayLinkNode
@end
@interface MyDataArray ()
{
MyDataArrayLinkNode *_headerNode;
}
@end
@implementation MyDataArray
- (instancetype)init
{
if(self = [super init])
{
_headerNode = [self createNode];
}
return self;
}
- (MyDataArrayLinkNode *)createNode
{
MyDataArrayLinkNode *node = [[MyDataArrayLinkNode alloc] init];
node->_next = nil;
node->_prev = nil;
node->_data = nil;
return node;
}
- (void)freeNode:(MyDataArrayLinkNode *)node
{
node->_data = nil;
node->_next = nil;
node->_prev = nil;
}
- (void)addObject:(id)anObject
{
if(anObject)
{
MyDataArrayLinkNode *node = [self createNode];
node->_data = anObject;
MyDataArrayLinkNode *pNode = _headerNode;
while (pNode && pNode->_next) {
pNode = pNode->_next;
}
pNode->_next = node;
node->_prev = pNode;
_count ++;
}
}
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
if(anObject != nil && index <= self.count - 1)
{
MyDataArrayLinkNode *currentNode = [self createNode];
currentNode->_data = anObject;
MyDataArrayLinkNode *pNode = _headerNode;
for (int i = 0; i < index; i ++) {
pNode = pNode->_next;
}
MyDataArrayLinkNode *nextNode = pNode->_next;
pNode->_next = currentNode;
currentNode->_prev = pNode;
currentNode->_next = nextNode;
nextNode->_prev = currentNode;
_count ++;
}
}
- (void)removeFirstObject
{
[self removeObjectAtIndex:0];
}
- (void)removeLastObject
{
[self removeObjectAtIndex:self.count - 1];
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
if(self.count > 0)
{
MyDataArrayLinkNode *pNode = _headerNode;
for (int i = 0; i <= index; i ++) {
pNode = pNode->_next;
}
MyDataArrayLinkNode *deleteNode = pNode;
MyDataArrayLinkNode *preNode = pNode->_prev;
preNode->_next = deleteNode->_next;
deleteNode->_prev = preNode;
[self freeNode:deleteNode];
deleteNode = nil;
_count --;
}
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
if(anObject && self.count > 0 && index <= self.count - 1)
{
MyDataArrayLinkNode *pNode = _headerNode;
for (int i = 0; i <= index; i ++) {
pNode = pNode->_next;
}
pNode->_data = anObject;
}
}
- (NSUInteger)indexOfObject:(id)anObject
{
if(self.count > 0)
{
MyDataArrayLinkNode *pNode = _headerNode->_next;
for (int i = 0; i < self.count; i ++) {
if(anObject && [anObject isKindOfClass:[NSString class]] && [anObject isEqualToString:pNode->_data])
{
return i;
}
if(pNode->_data == anObject)
{
return i;
}
}
}
return -1;
}
- (id)objectAtIndex:(NSUInteger)index
{
id obj = nil;
MyDataArrayLinkNode *pNode = _headerNode;
for (int i = 0; i <= index; i ++) {
pNode = pNode->_next;
}
obj = pNode->_data;
return obj;
}
+ (void)test
{
MyDataArray *dtArray = [[MyDataArray alloc] init];
[dtArray addObject:@"1"];
[dtArray addObject:@"2"];
[dtArray addObject:@"3"];
[dtArray addObject:@"4"];
[dtArray addObject:@"5"];
// [dtArray removeObjectAtIndex:10];
[dtArray removeFirstObject];
[dtArray removeLastObject];
[dtArray removeObjectAtIndex:1];
[dtArray insertObject:@"0" atIndex:0];
[dtArray replaceObjectAtIndex:0 withObject:@"100"];
NSInteger index = [dtArray indexOfObject:@"100"];
}
@end