-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.m
More file actions
73 lines (55 loc) · 1.19 KB
/
Stack.m
File metadata and controls
73 lines (55 loc) · 1.19 KB
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
//
// Stack.m
// instatext
//
// Created by Varun Jain on 14/03/13.
// Copyright (c) 2013 Varun Jain. All rights reserved.
//
#import "Stack.h"
@interface Stack ()
@property (nonatomic, strong) NSMutableArray *objects;
@end
@implementation Stack
@synthesize objects = _objects;
- (id)init {
if ((self = [self initWithArray:nil])) {
}
return self;
}
- (id)initWithArray:(NSArray*)array {
if ((self = [super init])) {
_objects = [[NSMutableArray alloc] initWithArray:array];
}
return self;
}
#pragma mark - Custom accessors
- (NSUInteger)count {
return _objects.count;
}
#pragma mark -
- (void)pushObject:(id)object {
if (object) {
[_objects addObject:object];
}
}
- (void)pushObjects:(NSArray*)objects {
for (id object in objects) {
[self pushObject:object];
}
}
- (id)popObject {
if (_objects.count > 0) {
id object = [_objects objectAtIndex:(_objects.count - 1)];
[_objects removeLastObject];
return object;
}
return nil;
}
- (id)peekObject {
if (_objects.count > 0) {
id object = [_objects objectAtIndex:(_objects.count - 1)];
return object;
}
return nil;
}
@end