Created
December 3, 2014 02:14
-
-
Save ksksue/db3e27718bc846e16c8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Queue : NSObject { | |
NSMutableArray *queue; | |
int maxSize; | |
} | |
- (id)initWithSize:(int)maxSize; | |
- (id)dequeue; | |
- (void)enqueue:(id)anObject ; | |
- (int)count; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Queue.h" | |
@implementation Queue | |
- (id)initWithSize:(int)aMaxSize{ | |
self = [super init]; | |
if (self != nil) { | |
queue = [[NSMutableArray alloc] init]; | |
maxSize = aMaxSize; | |
} | |
return self; | |
} | |
- (id)dequeue { | |
id headObject; | |
@synchronized(queue){ | |
if ([queue count] == 0) return nil; | |
headObject = [queue objectAtIndex:0]; | |
if (headObject != nil) { | |
[queue removeObjectAtIndex:0]; | |
} | |
} | |
return headObject; | |
} | |
- (void)enqueue:(id)anObject { | |
@synchronized(queue){ | |
if (anObject == nil) { | |
return; | |
} | |
if ([queue count] >= maxSize) { | |
[queue removeObjectAtIndex:0]; | |
} | |
[queue addObject:anObject]; | |
} | |
} | |
- (int)count { | |
int c = 0; | |
@synchronized(queue) { | |
c = [queue count]; | |
} | |
return c; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment