forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.m
47 lines (40 loc) · 926 Bytes
/
LinkedStack.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
//
// LinkedStack.m
// algo
//
// Created by Wenru Dong on 2018/10/8.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//
#import "LinkedStack.h"
#import "ListNode.h"
@implementation LinkedStack
{
@private
ListNode* _top;
}
- (BOOL)isEmpty {
return _top == nil;
}
- (void)push:(int)value {
ListNode *newTop = [ListNode nodeWithValue:value];
newTop.next = _top;
_top = newTop;
}
- (int)pop {
if ([self isEmpty]) {
[NSException raise:NSRangeException format:@"The stack is empty."];
}
int value = _top.value;
_top = _top.next;
return value;
}
- (NSString *)debugDescription {
NSMutableString *info = [[NSMutableString alloc] init];
ListNode *current = _top;
while (current) {
[info appendString:[NSString stringWithFormat:@"%d]", current.value]];
current = current.next;
}
return [NSString stringWithString:info];
}
@end