栈的基本操作: push, pop, peek
代码来自appledoc的开源项目
用拓展来处理,非常方便,不用专门定义一个Stack.
@implementation NSArray (GBArray)
- (id)firstObject {
if ([self count] == 0) return nil;
return self[0];
}
- (BOOL)isEmpty {
return ([self count] == 0);
}
#pragma mark -
@implementation NSMutableArray (GBMutableArray)
- (void)push:(id)object {
[self addObject:object];
}
- (id)pop {
id result = [self peek];
[self removeLastObject];
return result;
}
- (id)peek {
return [self lastObject];
}
@end
网友评论