现在的位置: 首页 > 综合 > 正文

[obj-c] Object-C Stack 容器

2013年12月01日 ⁄ 综合 ⁄ 共 731字 ⁄ 字号 评论关闭
由于Object-C中没有提供Stack容器,因此自己实践了一个简单的stack容器

#import <Foundation/Foundation.h>

@interface NSStack : NSObject {
	NSMutableArray* m_array;
	int count;
}

- (void)push:(id)anObject;
- (id)pop;
- (void)clear;

@property (nonatomic, readonly) int count;

@end

#import "NSStack.h"

@implementation NSStack

@synthesize count;

- (id)init
{
	if( self=[super init] )
	{
		m_array = [[NSMutableArray alloc] init];
		count = 0;
	}
	return self;
}

- (void)dealloc {
	[m_array release];
	[self dealloc];
    [super dealloc];
}

- (void)push:(id)anObject
{
	[m_array addObject:anObject];
	count = m_array.count;
}
- (id)pop
{
    id obj = nil;
    if(m_array.count > 0)
    {
        obj = [[[m_array lastObject]retain]autorelease];
        [m_array removeLastObject];
        count = m_array.count;
    }
    return obj;
}

- (void)clear
{
	[m_array removeAllObjects];
        count = 0;
}

@end

抱歉!评论已关闭.