implementation of linked-list-based stack in objective-c
This commit is contained in:
parent
50c35b796c
commit
f74488c31f
19
object-c/08_stack/LinkedStack.h
Normal file
19
object-c/08_stack/LinkedStack.h
Normal file
@ -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 <Foundation/Foundation.h>
|
||||
|
||||
@interface LinkedStack : NSObject
|
||||
|
||||
- (BOOL)isEmpty;
|
||||
- (void)push:(int)value;
|
||||
- (int)pop;
|
||||
|
||||
@end
|
47
object-c/08_stack/LinkedStack.m
Normal file
47
object-c/08_stack/LinkedStack.m
Normal file
@ -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
|
56
object-c/08_stack/LinkedStackTests.m
Normal file
56
object-c/08_stack/LinkedStackTests.m
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// LinkedStackTests.m
|
||||
// LinkedStackTests
|
||||
//
|
||||
// Created by Wenru Dong on 2018/10/8.
|
||||
// Copyright © 2018年 Wenru Dong. All rights reserved.
|
||||
//
|
||||
|
||||
#import <XCTest/XCTest.h>
|
||||
#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
|
19
object-c/08_stack/ListNode.h
Normal file
19
object-c/08_stack/ListNode.h
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// ListNode.h
|
||||
// algo
|
||||
//
|
||||
// Created by Wenru Dong on 2018/10/6.
|
||||
// Copyright © 2018年 Wenru Dong. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface ListNode : NSObject
|
||||
|
||||
@property int value;
|
||||
@property ListNode *next;
|
||||
|
||||
- (instancetype)initWithValue:(int)value;
|
||||
+ (instancetype)nodeWithValue:(int)value;
|
||||
|
||||
@end
|
28
object-c/08_stack/ListNode.m
Normal file
28
object-c/08_stack/ListNode.m
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user