-
Notifications
You must be signed in to change notification settings - Fork 13
/
Stack.m
57 lines (45 loc) · 831 Bytes
/
Stack.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
//
// Stack.m
// KeePass2
//
// Created by Qiang Yu on 2/11/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "Stack.h"
@implementation Stack
#pragma mark alloc/dealloc
-(id)init{
if(self=[super init]){
_stack = [[NSMutableArray alloc]initWithCapacity:8];
_length = 0;
}
return self;
}
-(void)dealloc{
[_stack release];
[super dealloc];
}
#pragma mark Stack Operations
-(void)push:(id)obj{
[_stack addObject:obj];
_length++;
}
-(id)pop{
if(!_length) return nil;
id value = [[[_stack objectAtIndex:(_length-1)] retain] autorelease];
[_stack removeObjectAtIndex:(_length-1)];
_length--;
return value;
}
-(id)peek{
if(!_length) return nil;
return [_stack objectAtIndex:(_length-1)];
}
-(BOOL)isEmpty{
return !_length;
}
-(void)clear{
[_stack removeAllObjects];
_length = 0;
}
@end