-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDKQueue.m
executable file
·72 lines (57 loc) · 1011 Bytes
/
DKQueue.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// DKQueue.m
//
// Created by Dominik Krejčík on 25/09/2011.
// Feel free to use and distribute as long as you mention me. Or buy me a beer.
//
#import "DKQueue.h"
@implementation DKQueue
-(id)init
{
if ( (self = [super init]) ) {
array = [[NSMutableArray alloc] init];
}
return self;
}
-(id)dequeue
{
if ([array count] > 0) {
id object = [self peek];
[array removeObjectAtIndex:0];
return object;
}
return nil;
}
-(void)enqueue:(id)element
{
[array addObject:element];
}
-(void)enqueueElementsFromArray:(NSArray*)arr
{
[array addObjectsFromArray:arr];
}
-(void)enqueueElementsFromQueue:(DKQueue*)queue
{
while (![queue isEmpty]) {
[self enqueue:[queue dequeue]];
}
}
-(id)peek
{
if ([array count] > 0)
return [array objectAtIndex:0];
return nil;
}
-(NSInteger)size
{
return [array count];
}
-(BOOL)isEmpty
{
return [array count] == 0;
}
-(void)clear
{
[array removeAllObjects];
}
@end