Objective-C 队列实现
Objective-C队列实现
原贴地址:http://blog.csdn.net/cloudhsu/article/details/6589313
Objective-C同样没有提供Queue容器,因此我也自己实践了一个
#import<Foundation/Foundation.h>
@interfaceNSQueue:NSObject{
NSMutableArray*m_array;
}
-(void)enqueue:(id)anObject;
-(id)dequeue;
-(void)clear;
@property(nonatomic,readonly)intcount;
@end
#import"NSQueue.h"
@implementationNSQueue
@synthesizecount;
-(id)init
{
if(self=[superinit])
{
m_array=[[NSMutableArrayalloc]init];
count=0;
}
returnself;
}
-(void)dealloc{
[m_arrayrelease];
//[selfdealloc];--递你妹的归啊
[superdealloc];
}
-(void)enqueue:(id)anObject
{
[m_arrayaddObject:anObject];
count=m_array.count;
}
-(id)dequeue
{
idobj=nil;
if(m_array.count>0)
{
obj=[[[m_arrayobjectAtIndex:0]retain]autorelease];
[m_arrayremoveObjectAtIndex:0];
count=m_array.count;
}
returnobj;
}
-(void)clear
{
[m_arrayremoveAllObjects];
count=0;
}
@end
另一版本:
#import<Foundation/Foundation.h>
@interfaceNSQueue:NSObject{
NSMutableArray*m_array;
}
-(void)enqueue:(id)anObject;
-(id)dequeue;
-(void)clear;
@property(nonatomic,readonly)intcount;
@end
#import"NSQueue.h"
@implementationNSQueue
@synthesizecount;
-(id)init
{
if(self=[superinit])
{
m_array=[[NSMutableArrayalloc]init];
}
returnself;
}
-(void)dealloc{
[m_arrayrelease];
[superdealloc];
}
-(void)enqueue:(id)anObject
{
[m_arrayaddObject:anObject];
}
-(id)dequeue
{
idobj=nil;
if(m_array.count>0)
{
obj=[[[m_arrayobjectAtIndex:0]retain]autorelease];
[m_arrayremoveObjectAtIndex:0];
}
returnobj;
}
-(void)clear
{
[m_arrayremoveAllObjects];
}
-(int)count{
return[m_arraycount];
}
@end