先创建一个堆栈类
#import <Foundation/Foundation.h>
@interface XMStack : NSObject
- (void)push:(id) obj;
- (id)pop;
- (id)top;
- (BOOL) isEmpty;
- (NSInteger) count;
@end
#import "XMStack.h"
@interface XMStack()
@property NSMutableArray *array;
@property NSInteger currentIndex;
@end
@implementation XMStack
- (instancetype)init
{
self = [super init];
if (self) {
self.array = [[NSMutableArray alloc] initWithCapacity:32];
}
return self;
}
- (void)push:(id) obj{
if (self.currentIndex >= self.array.count-1) {
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:self.array.count*2];
for (id obj in self.array) {
[newArray addObject:obj];
}
self.array = newArray;
}
[self.array addObject:obj];
self.currentIndex++;
}
- (id)pop{
if ([