diff --git a/object-c/08_stack/LinkedStack.h b/object-c/08_stack/LinkedStack.h new file mode 100644 index 00000000..e68320a6 --- /dev/null +++ b/object-c/08_stack/LinkedStack.h @@ -0,0 +1,19 @@ +// +// LinkedStack.h +// algo +// +// Created by Wenru Dong on 2018/10/8. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// +// Stack based upon linked list +// 基于链表实现的栈 + +#import + +@interface LinkedStack : NSObject + +- (BOOL)isEmpty; +- (void)push:(int)value; +- (int)pop; + +@end diff --git a/object-c/08_stack/LinkedStack.m b/object-c/08_stack/LinkedStack.m new file mode 100644 index 00000000..2f381c9c --- /dev/null +++ b/object-c/08_stack/LinkedStack.m @@ -0,0 +1,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 diff --git a/object-c/08_stack/LinkedStackTests.m b/object-c/08_stack/LinkedStackTests.m new file mode 100644 index 00000000..e23474f8 --- /dev/null +++ b/object-c/08_stack/LinkedStackTests.m @@ -0,0 +1,56 @@ +// +// LinkedStackTests.m +// LinkedStackTests +// +// Created by Wenru Dong on 2018/10/8. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import +#import "LinkedStack.h" + +@interface LinkedStackTests : XCTestCase + +@end + +@implementation LinkedStackTests + +- (void)setUp { + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testPush { + LinkedStack *stack = [[LinkedStack alloc] init]; + for (int i = 0; i < 10; i++) { + [stack push:i]; + } + XCTAssertEqualObjects([stack debugDescription], @"9]8]7]6]5]4]3]2]1]0]"); +} + +- (void)testPop { + LinkedStack *stack = [[LinkedStack alloc] init]; + for (int i = 0; i < 10; i++) { + [stack push:i]; + } + [stack pop]; + XCTAssertEqualObjects([stack debugDescription], @"8]7]6]5]4]3]2]1]0]"); + for (int i = 0; i < 9; i++) { + [stack pop]; + } + XCTAssertThrowsSpecificNamed([stack pop], NSException, NSRangeException, @"The stack is empty."); +} + +//- (void)testPerformanceExample { +// // This is an example of a performance test case. +// [self measureBlock:^{ +// // Put the code you want to measure the time of here. +// }]; +//} + +@end diff --git a/object-c/08_stack/ListNode.h b/object-c/08_stack/ListNode.h new file mode 100644 index 00000000..2e5418b7 --- /dev/null +++ b/object-c/08_stack/ListNode.h @@ -0,0 +1,19 @@ +// +// ListNode.h +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import + +@interface ListNode : NSObject + +@property int value; +@property ListNode *next; + +- (instancetype)initWithValue:(int)value; ++ (instancetype)nodeWithValue:(int)value; + +@end diff --git a/object-c/08_stack/ListNode.m b/object-c/08_stack/ListNode.m new file mode 100644 index 00000000..93d562d2 --- /dev/null +++ b/object-c/08_stack/ListNode.m @@ -0,0 +1,28 @@ +// +// ListNode.m +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import "ListNode.h" + +@implementation ListNode + +- (instancetype)initWithValue:(int)value { + if (self = [super init]) { + _value = value; + } + return self; +} + ++ (instancetype)nodeWithValue:(int)value { + return [[self alloc] initWithValue:value]; +} + +- (NSString*)debugDescription { + return [NSString stringWithFormat:@"%d", _value]; +} + +@end