-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSStack.m
84 lines (52 loc) · 1.06 KB
/
NSStack.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
73
74
75
76
77
78
79
80
81
82
83
84
//
// NSStack.m
//
// Licensed by ruralcoder.com under the
// Creative Commons Attribution-ShareAlike 3.0 Unported License
#import "NSStack.h"
@implementation NSStack
@synthesize objects = _objects;
#pragma mark - Lifecycle
- (id)init
{
self = [super init];
if (self)
_objects = [[NSMutableArray alloc] init];
return self;
}
- (void) dealloc
{
[_objects release];
[super dealloc];
}
#pragma mark - Stack Methods
- (NSArray*) toArray
{
return [NSArray arrayWithArray:_objects];
}
- (void)push:(id)object
{
[_objects insertObject:object atIndex:0];
}
- (id)top
{
if ([_objects count] == 0) return nil;
id object = [[[_objects objectAtIndex:0] retain] autorelease];
return object;
}
- (id)pop
{
if ([_objects count] == 0) return nil;
id object = [[[_objects objectAtIndex:0] retain] autorelease];
[_objects removeObjectAtIndex:0];
return object;
}
- (void) popAll
{
[_objects removeAllObjects];
}
- (NSInteger) count
{
return [_objects count];
}
@end