-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.m
More file actions
executable file
·102 lines (83 loc) · 1.45 KB
/
Stack.m
File metadata and controls
executable file
·102 lines (83 loc) · 1.45 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Stack.m
//
// Created by Chad Armstrong on 5 February 2019.
// Copyright (c) 2002-2019 Edenwaith. All rights reserved.
//
#import "Stack.h"
@implementation Stack
- (id)init {
if (self = [super init]) {
array = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[array release];
[super dealloc];
}
/*
* Push on a new item onto the stack
*/
- (void)push:(id)obj {
if (obj != nil) {
[array addObject:obj];
}
}
/*
* Pop off the top item from the stack and return it.
*/
- (id)pop {
if ([self isEmpty] == NO) {
id obj = [[array lastObject] retain];
[array removeLastObject];
return [obj autorelease];;
} else {
return nil;
}
}
/*
* Return the top item off the stack, but do not pop
*/
- (id)peek {
id obj = nil;
if ([self count] > 0) {
obj = [array lastObject];
}
return obj;
}
/*
* top is another name for the peek function
*/
- (id)top {
return [self peek];
}
/*
* Check if the stack is empty or not
*/
- (BOOL)isEmpty {
return [array count] == 0;
}
/*
* Return the number of elements in the stack
*/
- (long)count {
return [array count];
}
/*
* Clear the contents of the stack
*/
- (void)clear {
[array removeAllObjects];
}
/*
* Construct and return the description of the stack
*/
- (NSString *)description {
NSMutableString *result = [[NSMutableString alloc] init];
for (NSString *str in array) {
[result appendString:str];
}
return [result autorelease];
}
@end